quickControl.tsx 4.6 KB

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