1
0

quickControl.tsx 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /* eslint-disable jsx-a11y/no-static-element-interactions,jsx-a11y/click-events-have-key-events */
  2. import React, { PureComponent } from 'react';
  3. import classNames from 'classnames';
  4. import PropTypes from 'prop-types';
  5. import { cssClasses, strings } from '@douyinfe/semi-foundation/datePicker/constants';
  6. import Button from '../button/index';
  7. import Typography from '../typography/index';
  8. import { noop } from '@douyinfe/semi-foundation/utils/function';
  9. import { PresetsType, PresetType } from '@douyinfe/semi-foundation/datePicker/foundation';
  10. import { DateInputFoundationProps } from '@douyinfe/semi-foundation/datePicker/inputFoundation';
  11. const prefixCls = cssClasses.PREFIX;
  12. const { Text } = Typography;
  13. export interface QuickControlProps {
  14. presets: PresetsType;
  15. presetPosition: typeof strings.PRESET_POSITION_SET[number];
  16. onPresetClick: (preset: PresetType, e: React.MouseEvent) => void;
  17. type: string;
  18. insetInput: DateInputFoundationProps['insetInput']
  19. }
  20. class QuickControl extends PureComponent<QuickControlProps> {
  21. static propTypes = {
  22. presets: PropTypes.array,
  23. presetPosition: PropTypes.oneOf(strings.PRESET_POSITION_SET),
  24. onPresetClick: PropTypes.func,
  25. type: PropTypes.string,
  26. insetInput: PropTypes.oneOfType([PropTypes.bool, PropTypes.object]),
  27. };
  28. static defaultProps = {
  29. presets: [] as PresetsType,
  30. presetPosition: 'bottom',
  31. onPresetClick: noop,
  32. };
  33. render() {
  34. const { presets, onPresetClick, type, presetPosition, insetInput } = this.props;
  35. const isTypeRange = type === 'dateRange' || type === 'dateTimeRange';
  36. const isPanelTopAndBottom = presetPosition === 'top' || presetPosition === 'bottom';
  37. const isMonth = type === 'month';
  38. const isTopAndBottomRange = isPanelTopAndBottom && isTypeRange;
  39. const isTopAndBottomMonth = isPanelTopAndBottom && isMonth;
  40. const wrapperCls = classNames(`${prefixCls}-quick-control`, {
  41. [`${prefixCls}-quick-control-${type}`]: type,
  42. [`${prefixCls}-quick-control-${presetPosition}`]: true,
  43. });
  44. const headerCls = classNames({
  45. [`${prefixCls}-quick-control-header`]: true,
  46. });
  47. const contentWrapperCls = classNames({
  48. [`${prefixCls}-quick-control-${presetPosition}-content-wrapper`]: true,
  49. });
  50. const contentCls = classNames({
  51. [`${prefixCls}-quick-control-${presetPosition}-content`]: !isTopAndBottomRange && !isTopAndBottomMonth,
  52. [`${prefixCls}-quick-control-${presetPosition}-range-content`]: isTopAndBottomRange,
  53. [`${prefixCls}-quick-control-${presetPosition}-month-content`]: isTopAndBottomMonth,
  54. });
  55. const itemCls = classNames({
  56. [`${prefixCls}-quick-control-${presetPosition}-content-item`]: !isTopAndBottomRange && !isTopAndBottomMonth,
  57. [`${prefixCls}-quick-control-${presetPosition}-range-content-item`]: isTopAndBottomRange,
  58. [`${prefixCls}-quick-control-${presetPosition}-month-content-item`]: isTopAndBottomMonth,
  59. });
  60. const ellipsisCls = classNames({
  61. [`${prefixCls}-quick-control-${presetPosition}-content-item-ellipsis`]: !isTopAndBottomRange && !isTopAndBottomMonth,
  62. [`${prefixCls}-quick-control-${presetPosition}-range-content-item-ellipsis`]: isTopAndBottomRange,
  63. [`${prefixCls}-quick-control-${presetPosition}-month-content-item-ellipsis`]: isTopAndBottomMonth,
  64. });
  65. if (!presets.length) {
  66. return null;
  67. }
  68. return (
  69. <div className={wrapperCls} x-insetinput={insetInput ? "true" : "false"}>
  70. {!isPanelTopAndBottom && <div className={headerCls}>快捷选择</div>}
  71. <div className={contentWrapperCls}>
  72. <div className={contentCls}>
  73. {presets.map((item, index) => {
  74. const _item: PresetType = typeof item === 'function' ? item() : item;
  75. return (
  76. <Button size="small" type="primary" onClick={e => onPresetClick(_item, e)} key={index}>
  77. <div className={itemCls}>
  78. <Text
  79. ellipsis={{ showTooltip: true }}
  80. className={ellipsisCls}
  81. >
  82. {_item.text}
  83. </Text>
  84. </div>
  85. </Button>
  86. );
  87. })}
  88. </div>
  89. </div>
  90. </div>
  91. );
  92. }
  93. }
  94. export default QuickControl;