navigation.tsx 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. import React, { PureComponent } from 'react';
  2. // import cls from 'classnames';
  3. import PropTypes from 'prop-types';
  4. import { noop } from 'lodash';
  5. import IconButton from '../iconButton';
  6. import Button from '../button';
  7. import { cssClasses, strings } from '@douyinfe/semi-foundation/datePicker/constants';
  8. import { IconChevronLeft, IconChevronRight, IconDoubleChevronLeft, IconDoubleChevronRight } 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. onPrevYear,
  64. onNextYear,
  65. density,
  66. shouldBimonthSwitch,
  67. panelType
  68. } = this.props;
  69. const btnTheme = 'borderless';
  70. const iconBtnSize = density === 'compact' ? 'default' : 'large';
  71. const btnNoHorizontalPadding = true;
  72. const buttonSize = density === 'compact' ? 'small' : 'default';
  73. const isLeftPanel = panelType === strings.PANEL_TYPE_LEFT;
  74. const isRightPanel = panelType === strings.PANEL_TYPE_RIGHT;
  75. // syncSwitchMonth and the current panel is the left
  76. const hiddenLeftPanelRightButtons = shouldBimonthSwitch && isLeftPanel;
  77. // syncSwitchMonth and the current panel is the right
  78. const hiddenRightPanelLeftButtons = shouldBimonthSwitch && isRightPanel;
  79. // `visibility: hidden` will keep the icon in position
  80. const leftButtonStyle: React.CSSProperties = {};
  81. const rightButtonStyle: React.CSSProperties = {};
  82. if (hiddenRightPanelLeftButtons) {
  83. leftButtonStyle.visibility = 'hidden';
  84. }
  85. if (hiddenLeftPanelRightButtons) {
  86. rightButtonStyle.visibility = 'hidden';
  87. }
  88. const ref = forwardRef || this.navRef;
  89. return (
  90. <div className={prefixCls} ref={ref}>
  91. <IconButton
  92. key="double-chevron-left"
  93. icon={<IconDoubleChevronLeft size={iconBtnSize}/>}
  94. size={buttonSize}
  95. theme={btnTheme}
  96. noHorizontalPadding={btnNoHorizontalPadding}
  97. onClick={onPrevYear}
  98. style={leftButtonStyle}
  99. />
  100. <IconButton
  101. key="chevron-left"
  102. icon={<IconChevronLeft size={iconBtnSize} />}
  103. size={buttonSize}
  104. onClick={onPrevMonth}
  105. theme={btnTheme}
  106. noHorizontalPadding={btnNoHorizontalPadding}
  107. style={leftButtonStyle}
  108. />
  109. <div className={`${prefixCls}-month`}>
  110. <Button onClick={onMonthClick} theme={btnTheme} size={buttonSize}>
  111. <span>{monthText}</span>
  112. </Button>
  113. </div>
  114. <IconButton
  115. key="chevron-right"
  116. icon={<IconChevronRight size={iconBtnSize} />}
  117. size={buttonSize}
  118. onClick={onNextMonth}
  119. theme={btnTheme}
  120. noHorizontalPadding={btnNoHorizontalPadding}
  121. style={rightButtonStyle}
  122. />
  123. <IconButton
  124. key="double-chevron-right"
  125. icon={<IconDoubleChevronRight size={iconBtnSize}/>}
  126. size={buttonSize}
  127. theme={btnTheme}
  128. noHorizontalPadding={btnNoHorizontalPadding}
  129. onClick={onNextYear}
  130. style={rightButtonStyle}
  131. />
  132. </div>
  133. );
  134. }
  135. }