|
| 1 | +import React, { Children, cloneElement, PureComponent } from 'react'; |
| 2 | +import PropTypes from 'prop-types'; |
| 3 | +import classnames from 'classnames'; |
| 4 | +import StepItem from './item'; |
| 5 | + |
| 6 | +import { HORIZONTAL, VERTICAL, INLINE, CIRCLE, DOT, PROCESS, WAIT, FINISH } from './constants'; |
| 7 | + |
| 8 | +import './index.less'; |
| 9 | + |
| 10 | +export default class Step extends PureComponent { |
| 11 | + |
| 12 | + static propTypes = { |
| 13 | + current: PropTypes.number, |
| 14 | + direction: PropTypes.oneOf([HORIZONTAL, VERTICAL, INLINE]), |
| 15 | + type: PropTypes.oneOf([CIRCLE, DOT]), |
| 16 | + children: PropTypes.any, |
| 17 | + onClick: PropTypes.func, |
| 18 | + className: PropTypes.string |
| 19 | + }; |
| 20 | + |
| 21 | + static defaultProps = { |
| 22 | + current: 0, |
| 23 | + direction: HORIZONTAL, |
| 24 | + type: CIRCLE, |
| 25 | + children: null, |
| 26 | + className: '', |
| 27 | + onClick: () => {} |
| 28 | + } |
| 29 | + |
| 30 | + static Item = StepItem; |
| 31 | + |
| 32 | + getStatus(index) { |
| 33 | + const { current } = this.props; |
| 34 | + |
| 35 | + if (current === index) { |
| 36 | + return PROCESS; |
| 37 | + } |
| 38 | + |
| 39 | + if (index < current) { |
| 40 | + return FINISH; |
| 41 | + } |
| 42 | + |
| 43 | + return WAIT; |
| 44 | + } |
| 45 | + |
| 46 | + render() { |
| 47 | + |
| 48 | + const { direction, type, children, className, onClick: rootClick, ...stepProps } = this.props; |
| 49 | + const classNames = classnames('step', direction, type, className); |
| 50 | + const elements = Children.map(children, (Child, index) => { |
| 51 | + const { status, content, onClick, ...props } = Child.props; |
| 52 | + |
| 53 | + return cloneElement(Child, { |
| 54 | + ...props, |
| 55 | + index, |
| 56 | + status: status || this.getStatus(index), |
| 57 | + onClick: () => { |
| 58 | + onClick(index); |
| 59 | + rootClick(index); |
| 60 | + }, |
| 61 | + content: direction === HORIZONTAL ? null : content, |
| 62 | + |
| 63 | + }); |
| 64 | + }); |
| 65 | + |
| 66 | + return <div className={classNames} {...stepProps}>{elements}</div>; |
| 67 | + } |
| 68 | +} |
| 69 | + |
0 commit comments