1
0

quickControl.tsx 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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 } from '@douyinfe/semi-foundation/datePicker/constants';
  6. import Button from '../button/index';
  7. import { noop } from '@douyinfe/semi-foundation/utils/function';
  8. import { PresetsType, PresetType } from '@douyinfe/semi-foundation/datePicker/foundation';
  9. const prefixCls = cssClasses.PREFIX;
  10. export interface QuickControlProps {
  11. presets: PresetsType;
  12. onPresetClick: (preset: PresetType, e: React.MouseEvent) => void;
  13. type: string;
  14. }
  15. class QuickControl extends PureComponent<QuickControlProps> {
  16. static propTypes = {
  17. presets: PropTypes.array,
  18. onPresetClick: PropTypes.func,
  19. type: PropTypes.string
  20. };
  21. static defaultProps = {
  22. presets: [] as PresetsType,
  23. onPresetClick: noop,
  24. };
  25. render() {
  26. const { presets, onPresetClick, type } = this.props;
  27. const wrapperCls = classNames(`${prefixCls}-quick-control`, {
  28. [`${prefixCls}-quick-control-${type}`]: type
  29. });
  30. const itemCls = classNames(`${prefixCls}-quick-control-item`);
  31. if (!presets.length) {
  32. return null;
  33. }
  34. return (
  35. <div className={wrapperCls}>
  36. {presets.map((item, index) => {
  37. const _item: PresetType = typeof item === 'function' ? item() : item;
  38. return (
  39. <div className={itemCls} onClick={e => onPresetClick(_item, e)} key={index}>
  40. <Button size="small" theme="borderless" type="primary">
  41. <span>{_item.text}</span>
  42. </Button>
  43. </div>
  44. );
  45. })}
  46. </div>
  47. );
  48. }
  49. }
  50. export default QuickControl;