|
1 | | -import React, { HTMLAttributes } from 'react'; |
| 1 | +// import noop from '@jswork/noop'; |
2 | 2 | import cx from 'classnames'; |
| 3 | +import React, { Component, HTMLAttributes } from 'react'; |
| 4 | +import ReactList, { ReactListProps } from '@jswork/react-list'; |
3 | 5 |
|
4 | 6 | const CLASS_NAME = 'react-block-steps'; |
5 | | - |
6 | | -export type IProps = { |
7 | | - /** |
8 | | - * Additional CSS class names to apply to the component. |
9 | | - * @default '' |
10 | | - */ |
11 | | - className?: string; |
12 | | - /** |
13 | | - * The content of the component. |
14 | | - * @default null |
15 | | - */ |
16 | | - children?: React.ReactNode; |
| 7 | +// const uuid = () => Math.random().toString(36).substring(2, 9); |
| 8 | +export type ReactBlockStepsProps = { |
| 9 | + value: number; |
| 10 | + size: number; |
| 11 | + withIndex?: boolean; |
| 12 | + itemClassName?: string; |
| 13 | + activeClassName?: string; |
| 14 | + listProps?: Omit<ReactListProps, 'items' | 'template'>; |
17 | 15 | } & HTMLAttributes<HTMLDivElement>; |
18 | 16 |
|
19 | | -export default function ReactBlockSteps(props: IProps) { |
20 | | - const { className, ...rest } = props; |
21 | | - return <div data-component={CLASS_NAME} className={cx(CLASS_NAME, className)} {...rest} />; |
| 17 | +export default class ReactBlockSteps extends Component<ReactBlockStepsProps> { |
| 18 | + static displayName = CLASS_NAME; |
| 19 | + static version = '__VERSION__'; |
| 20 | + static defaultProps = { |
| 21 | + value: 0, |
| 22 | + size: 5, |
| 23 | + withIndex: false, |
| 24 | + activeClassName: 'is-active', |
| 25 | + }; |
| 26 | + |
| 27 | + get items() { |
| 28 | + const { size } = this.props; |
| 29 | + return Array.from({ length: size }, (_, index) => index + 1); |
| 30 | + } |
| 31 | + |
| 32 | + handleTemplate = ({ item, index }) => { |
| 33 | + const { value, itemClassName, withIndex, activeClassName } = this.props; |
| 34 | + const isActive = index < value; |
| 35 | + const children = withIndex ? item : null; |
| 36 | + return <span key={item} className={cx({ [activeClassName!]: isActive }, itemClassName)}>{children}</span>; |
| 37 | + }; |
| 38 | + |
| 39 | + render() { |
| 40 | + const { className, value, listProps, activeClassName, itemClassName, withIndex, ...rest } = this.props; |
| 41 | + return ( |
| 42 | + <div date-value={value} data-component={CLASS_NAME} className={cx(CLASS_NAME, className)} {...rest}> |
| 43 | + <ReactList items={this.items} template={this.handleTemplate} {...listProps} /> |
| 44 | + </div> |
| 45 | + ); |
| 46 | + } |
22 | 47 | } |
0 commit comments