fillStep.tsx 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. import React from 'react';
  2. import { isFunction } from 'lodash';
  3. import PropTypes from 'prop-types';
  4. import classnames from 'classnames';
  5. import { stepsClasses as css } from '@douyinfe/semi-foundation/steps/constants';
  6. import { IconTickCircle, IconAlertCircle, IconAlertTriangle } from '@douyinfe/semi-icons';
  7. export type Status = 'wait' | 'process' | 'finish' | 'error' | 'warning';
  8. export interface FillStepProps {
  9. description?: React.ReactNode;
  10. icon?: React.ReactNode;
  11. status?: Status;
  12. title?: React.ReactNode;
  13. className?: string;
  14. style?: React.CSSProperties;
  15. prefixCls?: string;
  16. stepNumber?: string;
  17. onChange?: () => void;
  18. onClick?: React.MouseEventHandler<HTMLDivElement>;
  19. onKeyDown?: React.KeyboardEventHandler<HTMLDivElement>;
  20. "role"?: React.AriaRole;
  21. "aria-label"?: React.AriaAttributes["aria-label"];
  22. }
  23. const FillStep = (props: FillStepProps) => {
  24. const { prefixCls, className, title, description, status, style, onClick, icon, onChange, stepNumber, onKeyDown } = props;
  25. const renderIcon = () => {
  26. let inner, progress;
  27. if ('icon' in props) {
  28. inner = icon;
  29. } else if ('status' in props) {
  30. switch (status) {
  31. case 'error':
  32. inner = <IconAlertCircle size="extra-large" />;
  33. break;
  34. case 'wait':
  35. inner = stepNumber;
  36. break;
  37. case 'process':
  38. inner = stepNumber;
  39. progress = true;
  40. break;
  41. case 'finish':
  42. inner = <IconTickCircle size="extra-large" />;
  43. break;
  44. case 'warning':
  45. inner = <IconAlertTriangle size="extra-large" />;
  46. break;
  47. default:
  48. inner = null;
  49. break;
  50. }
  51. }
  52. const cls = classnames({
  53. [`${prefixCls}-left`]: true,
  54. [`${prefixCls}-icon`]: 'icon' in props,
  55. [`${prefixCls}-plain`]: !('icon' in props),
  56. [`${prefixCls}-icon-process`]: progress,
  57. });
  58. return inner ? <div className={cls}>{inner}</div> : null;
  59. };
  60. const handleClick = (e: React.MouseEvent) => {
  61. if (isFunction(onClick)) {
  62. onClick(e);
  63. }
  64. onChange();
  65. };
  66. const handleKeyDown = (e) => {
  67. if (e.key === 'Enter') {
  68. if (isFunction(onKeyDown)) {
  69. onKeyDown(e);
  70. }
  71. onChange();
  72. }
  73. };
  74. return (
  75. <div
  76. role={props["role"]}
  77. aria-label={props["aria-label"]}
  78. aria-current="step"
  79. tabIndex={0}
  80. className={classnames({
  81. [prefixCls]: true,
  82. [`${prefixCls}-${status}`]: Boolean(status),
  83. [`${prefixCls}-clickable`]: onClick,
  84. }, className)}
  85. style={style}
  86. onClick={e => {
  87. handleClick(e);
  88. }}
  89. onKeyDown={handleKeyDown}
  90. >
  91. {renderIcon()}
  92. <div className={`${prefixCls}-content`}>
  93. <div className={`${prefixCls}-title`} title={typeof title === 'string' ? title : null}>
  94. <span className={`${prefixCls}-title-text`}>{title}</span>
  95. </div>
  96. <div
  97. className={`${prefixCls}-description`}
  98. title={typeof description === 'string' ? description : null}
  99. >
  100. {description}
  101. </div>
  102. </div>
  103. </div>
  104. );
  105. };
  106. FillStep.propTypes = {
  107. prefixCls: PropTypes.string,
  108. description: PropTypes.node,
  109. icon: PropTypes.node,
  110. status: PropTypes.oneOf(['wait', 'process', 'finish', 'error', 'warning']),
  111. title: PropTypes.node,
  112. className: PropTypes.string,
  113. style: PropTypes.object,
  114. onClick: PropTypes.func,
  115. };
  116. FillStep.defaultProps = {
  117. prefixCls: css.ITEM,
  118. status: 'wait',
  119. className: '',
  120. };
  121. export default FillStep;