1
0

quickControl.tsx 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. locale: any
  20. }
  21. class QuickControl extends PureComponent<QuickControlProps> {
  22. static propTypes = {
  23. presets: PropTypes.array,
  24. presetPosition: PropTypes.oneOf(strings.PRESET_POSITION_SET),
  25. onPresetClick: PropTypes.func,
  26. type: PropTypes.string,
  27. insetInput: PropTypes.oneOfType([PropTypes.bool, PropTypes.object]),
  28. locale: PropTypes.object,
  29. };
  30. static defaultProps = {
  31. presets: [] as PresetsType,
  32. presetPosition: 'bottom',
  33. onPresetClick: noop,
  34. };
  35. render() {
  36. const { presets, onPresetClick, type, presetPosition, insetInput, locale } = this.props;
  37. const isTypeRange = type === 'dateRange' || type === 'dateTimeRange';
  38. const isPanelTopAndBottom = presetPosition === 'top' || presetPosition === 'bottom';
  39. const isMonth = type === 'month';
  40. const isTopAndBottomRange = isPanelTopAndBottom && isTypeRange;
  41. const isTopAndBottomMonth = isPanelTopAndBottom && isMonth;
  42. const wrapperCls = classNames(`${prefixCls}-quick-control`, {
  43. [`${prefixCls}-quick-control-${type}`]: type,
  44. [`${prefixCls}-quick-control-${presetPosition}`]: true,
  45. });
  46. const headerCls = classNames({
  47. [`${prefixCls}-quick-control-header`]: true,
  48. });
  49. const contentWrapperCls = classNames({
  50. [`${prefixCls}-quick-control-${presetPosition}-content-wrapper`]: true,
  51. });
  52. const contentCls = classNames({
  53. [`${prefixCls}-quick-control-${presetPosition}-content`]: !isTopAndBottomRange && !isTopAndBottomMonth,
  54. [`${prefixCls}-quick-control-${presetPosition}-range-content`]: isTopAndBottomRange,
  55. [`${prefixCls}-quick-control-${presetPosition}-month-content`]: isTopAndBottomMonth,
  56. });
  57. const itemCls = classNames({
  58. [`${prefixCls}-quick-control-${presetPosition}-content-item`]: !isTopAndBottomRange && !isTopAndBottomMonth,
  59. [`${prefixCls}-quick-control-${presetPosition}-range-content-item`]: isTopAndBottomRange,
  60. [`${prefixCls}-quick-control-${presetPosition}-month-content-item`]: isTopAndBottomMonth,
  61. });
  62. const ellipsisCls = classNames({
  63. [`${prefixCls}-quick-control-${presetPosition}-content-item-ellipsis`]: !isTopAndBottomRange && !isTopAndBottomMonth,
  64. [`${prefixCls}-quick-control-${presetPosition}-range-content-item-ellipsis`]: isTopAndBottomRange,
  65. [`${prefixCls}-quick-control-${presetPosition}-month-content-item-ellipsis`]: isTopAndBottomMonth,
  66. });
  67. if (!presets.length) {
  68. return null;
  69. }
  70. return (
  71. <div className={wrapperCls} x-insetinput={insetInput ? "true" : "false"}>
  72. {!isPanelTopAndBottom && <div className={headerCls}>{locale.presets}</div>}
  73. <div className={contentWrapperCls}>
  74. <div className={contentCls}>
  75. {presets.map((item, index) => {
  76. const _item: PresetType = typeof item === 'function' ? item() : item;
  77. return (
  78. <Button size="small" type="primary" onClick={e => onPresetClick(_item, e)} key={index}>
  79. <div className={itemCls}>
  80. <Text
  81. ellipsis={{ showTooltip: true }}
  82. className={ellipsisCls}
  83. >
  84. {_item.text}
  85. </Text>
  86. </div>
  87. </Button>
  88. );
  89. })}
  90. </div>
  91. </div>
  92. </div>
  93. );
  94. }
  95. }
  96. export default QuickControl;