navStep.tsx 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. import React from 'react';
  2. import PropTypes from 'prop-types';
  3. import { isFunction } from 'lodash';
  4. import classnames from 'classnames';
  5. import { stepsClasses as css } from '@douyinfe/semi-foundation/steps/constants';
  6. import { IconChevronRight } from '@douyinfe/semi-icons';
  7. export interface NavStepProps {
  8. title?: React.ReactNode;
  9. className?: string;
  10. style?: React.CSSProperties;
  11. index?: number;
  12. active?: boolean;
  13. total?: number;
  14. prefixCls?: string;
  15. onChange?: () => void;
  16. onClick?: React.MouseEventHandler<HTMLDivElement>;
  17. onKeyDown?: React.KeyboardEventHandler<HTMLDivElement>;
  18. "role"?: React.AriaRole;
  19. "aria-label"?: React.AriaAttributes["aria-label"]
  20. }
  21. const NavStep = (props: NavStepProps) => {
  22. const { prefixCls, className, title, style, active, index, total, onClick, onKeyDown, onChange } = props;
  23. const classString = classnames(prefixCls, {
  24. [`${prefixCls}-active`]: active
  25. }, className);
  26. const handleClick = (e: React.MouseEvent<HTMLDivElement>) => {
  27. onClick?.(e);
  28. onChange?.();
  29. };
  30. const handleKeyDown = (e) => {
  31. if (e.key === 'Enter') {
  32. onKeyDown?.(e);
  33. onChange?.();
  34. }
  35. };
  36. return (
  37. <div role={props["role"]} aria-label={props["aria-label"]} aria-current="step" tabIndex={0} className={classString} style={style} onClick={e => handleClick(e)} onKeyDown={handleKeyDown}>
  38. <div className={`${prefixCls}-container`}>
  39. <div className={`${prefixCls}-content`}>
  40. <div className={`${prefixCls}-title`}>
  41. {title}
  42. </div>
  43. </div>
  44. {index !== total - 1 && (
  45. <div className={`${prefixCls}-icon`}>
  46. <IconChevronRight size="small" />
  47. </div>
  48. )}
  49. </div>
  50. </div>
  51. );
  52. };
  53. NavStep.propTypes = {
  54. prefixCls: PropTypes.string,
  55. title: PropTypes.node,
  56. className: PropTypes.string,
  57. style: PropTypes.object,
  58. onClick: PropTypes.func,
  59. active: PropTypes.bool,
  60. };
  61. NavStep.defaultProps = {
  62. prefixCls: css.ITEM,
  63. active: false,
  64. className: '',
  65. };
  66. export default NavStep;