basicSteps.tsx 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. import React, { cloneElement, Children, useMemo, isValidElement, ReactElement } from 'react';
  2. import PropTypes from 'prop-types';
  3. import cls from 'classnames';
  4. import { stepsClasses as css } from '@douyinfe/semi-foundation/steps/constants';
  5. export type Direction = 'horizontal' | 'vertical';
  6. export type Status = 'wait' | 'process' | 'finish' | 'error' | 'warning';
  7. export type Size = 'default' | 'small';
  8. export interface BasicStepsProps {
  9. prefixCls?: string;
  10. className?: string;
  11. direction?: Direction;
  12. current?: number;
  13. initial?: number;
  14. status?: Status;
  15. style?: React.CSSProperties;
  16. size?: Size;
  17. hasLine?: boolean;
  18. children?: React.ReactNode;
  19. onChange?: (current: number) => void;
  20. }
  21. const Steps = (props: BasicStepsProps) => {
  22. const {
  23. size,
  24. current,
  25. status,
  26. children,
  27. prefixCls,
  28. initial,
  29. direction,
  30. className,
  31. style,
  32. hasLine,
  33. onChange,
  34. } = props;
  35. const inner = useMemo(() => {
  36. const filteredChildren = Children.toArray(children).filter(c => isValidElement(c)) as Array<ReactElement>;
  37. const content = Children.map(filteredChildren, (child: React.ReactElement, index) => {
  38. if (!child) {
  39. return null;
  40. }
  41. const stepNumber = initial + index;
  42. const childProps = {
  43. stepNumber: `${stepNumber + 1}`,
  44. size,
  45. ...child.props,
  46. };
  47. if (status === 'error' && index === current - 1) {
  48. childProps.className = `${prefixCls}-next-error`;
  49. }
  50. if (!child.props.status) {
  51. if (stepNumber === current) {
  52. childProps.status = status;
  53. } else if (stepNumber < current) {
  54. childProps.status = 'finish';
  55. } else {
  56. childProps.status = 'wait';
  57. }
  58. }
  59. childProps.active = stepNumber === current;
  60. childProps.done = stepNumber < current;
  61. childProps.onChange = () => {
  62. if (index !== current) {
  63. onChange(index + initial);
  64. }
  65. };
  66. return cloneElement(child, { ...childProps });
  67. });
  68. return content;
  69. }, [children, initial, prefixCls, direction, status, current, size]);
  70. const wrapperCls = cls(className, {
  71. [`${prefixCls}-basic`]: true,
  72. [`${prefixCls}-${direction}`]: true,
  73. [`${prefixCls}-${size}`]: size !== 'default',
  74. [`${prefixCls}-hasline`]: hasLine,
  75. });
  76. return (
  77. <div className={wrapperCls} style={style}>
  78. {inner}
  79. </div>
  80. );
  81. };
  82. Steps.propTypes = {
  83. prefixCls: PropTypes.string,
  84. className: PropTypes.string,
  85. style: PropTypes.object,
  86. current: PropTypes.number,
  87. initial: PropTypes.number,
  88. direction: PropTypes.oneOf(['horizontal', 'vertical']),
  89. status: PropTypes.oneOf(['wait', 'process', 'finish', 'error', 'warning']),
  90. hasLine: PropTypes.bool,
  91. };
  92. Steps.defaultProps = {
  93. prefixCls: css.PREFIX,
  94. current: 0,
  95. direction: 'horizontal',
  96. size: '',
  97. initial: 0,
  98. hasLine: true,
  99. status: 'process',
  100. };
  101. export default Steps;