navStep.tsx 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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) => {
  27. if (isFunction(onClick)) {
  28. onClick(e);
  29. }
  30. onChange();
  31. };
  32. const handleKeyDown = (e) => {
  33. if (e.key === 'Enter') {
  34. if (isFunction(onKeyDown)) {
  35. onKeyDown(e);
  36. }
  37. onChange();
  38. }
  39. };
  40. return (
  41. <div role={props["role"]} aria-label={props["aria-label"]} aria-current="step" tabIndex={0} className={classString} style={style} onClick={e => handleClick(e)} onKeyDown={handleKeyDown}>
  42. <div className={`${prefixCls}-container`}>
  43. <div className={`${prefixCls}-content`}>
  44. <div className={`${prefixCls}-title`}>
  45. {title}
  46. </div>
  47. </div>
  48. {index !== total - 1 && (
  49. <div className={`${prefixCls}-icon`}>
  50. <IconChevronRight size="small" />
  51. </div>
  52. )}
  53. </div>
  54. </div>
  55. );
  56. };
  57. NavStep.propTypes = {
  58. prefixCls: PropTypes.string,
  59. title: PropTypes.node,
  60. className: PropTypes.string,
  61. style: PropTypes.object,
  62. onClick: PropTypes.func,
  63. active: PropTypes.bool,
  64. };
  65. NavStep.defaultProps = {
  66. prefixCls: css.ITEM,
  67. active: false,
  68. className: '',
  69. };
  70. export default NavStep;