navigation.tsx 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. import React, { PureComponent } from 'react';
  2. // import cls from 'classnames';
  3. import PropTypes from 'prop-types';
  4. import { noop } from 'lodash-es';
  5. import IconButton from '../iconButton';
  6. import Button from '../button';
  7. import { cssClasses, strings } from '@douyinfe/semi-foundation/datePicker/constants';
  8. import { IconChevronLeft, IconChevronRight } from '@douyinfe/semi-icons';
  9. import { PanelType } from '@douyinfe/semi-foundation/datePicker/monthsGridFoundation';
  10. const prefixCls = cssClasses.NAVIGATION;
  11. interface NavigationProps {
  12. forwardRef?: React.Ref<HTMLDivElement>;
  13. monthText?: string;
  14. density?: string;
  15. onMonthClick?: (e: React.MouseEvent) => void;
  16. onNextMonth?: () => void;
  17. onPrevMonth?: () => void;
  18. onNextYear?: () => void;
  19. onPrevYear?: () => void;
  20. navPrev?: React.ReactNode;
  21. navNext?: React.ReactNode;
  22. // Whether to switch synchronously for two panels
  23. shouldBimonthSwitch?: boolean;
  24. // Panel type, divided into left panel and right panel
  25. panelType?: PanelType;
  26. }
  27. export default class Navigation extends PureComponent<NavigationProps> {
  28. static propTypes = {
  29. monthText: PropTypes.string,
  30. density: PropTypes.string,
  31. onMonthClick: PropTypes.func,
  32. onNextMonth: PropTypes.func,
  33. onPrevMonth: PropTypes.func,
  34. onNextYear: PropTypes.func,
  35. onPrevYear: PropTypes.func,
  36. navPrev: PropTypes.node,
  37. navNext: PropTypes.node,
  38. // Whether to switch synchronously for two panels
  39. shouldBimonthSwitch: PropTypes.bool,
  40. // Panel type, divided into left panel and right panel
  41. panelType: PropTypes.oneOf([strings.PANEL_TYPE_LEFT, strings.PANEL_TYPE_RIGHT])
  42. };
  43. static defaultProps = {
  44. monthText: '',
  45. onMonthClick: noop,
  46. onNextMonth: noop,
  47. onPrevMonth: noop,
  48. onNextYear: noop,
  49. onPrevYear: noop,
  50. };
  51. navRef: React.RefObject<HTMLDivElement>;
  52. constructor(props: NavigationProps) {
  53. super(props);
  54. this.navRef = React.createRef();
  55. }
  56. render() {
  57. const {
  58. forwardRef,
  59. monthText,
  60. onMonthClick,
  61. onNextMonth,
  62. onPrevMonth,
  63. density,
  64. shouldBimonthSwitch,
  65. panelType
  66. } = this.props;
  67. const btnTheme = 'borderless';
  68. const iconBtnSize = density === 'compact' ? 'default' : 'large';
  69. const btnNoHorizontalPadding = true;
  70. const buttonSize = density === 'compact' ? 'small' : 'default';
  71. // Enable dual-panel synchronous switching, and the current panel is the left panel
  72. const bimonthSwitchWithLeftPanel = shouldBimonthSwitch && panelType === strings.PANEL_TYPE_LEFT;
  73. // Enable dual-panel synchronous switching, and the current panel is the right panel
  74. const bimonthSwitchWithRightPanel = shouldBimonthSwitch && panelType === strings.PANEL_TYPE_RIGHT;
  75. const ref = forwardRef || this.navRef;
  76. return (
  77. <div className={prefixCls} ref={ref}>
  78. {
  79. !bimonthSwitchWithRightPanel &&
  80. (
  81. <IconButton
  82. icon={<IconChevronLeft size={iconBtnSize} />}
  83. size={buttonSize}
  84. onClick={onPrevMonth}
  85. theme={btnTheme}
  86. noHorizontalPadding={btnNoHorizontalPadding}
  87. />
  88. )
  89. }
  90. <div className={`${prefixCls}-month`}>
  91. <Button onClick={onMonthClick} theme={btnTheme} size={buttonSize}>
  92. <span>{monthText}</span>
  93. </Button>
  94. </div>
  95. {
  96. !bimonthSwitchWithLeftPanel &&
  97. (
  98. <IconButton
  99. icon={<IconChevronRight size={iconBtnSize} />}
  100. size={buttonSize}
  101. onClick={onNextMonth}
  102. theme={btnTheme}
  103. noHorizontalPadding={btnNoHorizontalPadding}
  104. />
  105. )
  106. }
  107. </div>
  108. );
  109. }
  110. }