inputFoundation.ts 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /* eslint-disable max-len */
  2. import BaseFoundation, { DefaultAdapter } from '../base/foundation';
  3. import { BaseValueType, ValidateStatus, ValueType } from './foundation';
  4. import { formatDateValues } from './_utils/formatter';
  5. import { getDefaultFormatTokenByType } from './_utils/getDefaultFormatToken';
  6. const KEY_CODE_ENTER = 'Enter';
  7. const KEY_CODE_TAB = 'Tab';
  8. export type Type = 'date' | 'dateRange' | 'year' | 'month' | 'dateTime' | 'dateTimeRange';
  9. export type RangeType = 'rangeStart' | 'rangeEnd';
  10. export interface DateInputEventHandlerProps {
  11. onClick?: (e: any) => void;
  12. onChange?: (value: string, e: any) => void;
  13. onEnterPress?: (e: any) => void;
  14. onBlur?: (e: any) => void;
  15. onFocus?: (e: any, rangeType: RangeType) => void;
  16. onClear?: (e: any) => void;
  17. onRangeInputClear?: (e: any) => void;
  18. onRangeEndTabPress?: (e: any) => void;
  19. }
  20. export interface DateInputElementProps {
  21. insetLabel?: any;
  22. prefix?: any;
  23. }
  24. export interface DateInputFoundationProps extends DateInputElementProps, DateInputEventHandlerProps {
  25. [x: string]: any;
  26. value?: ValueType;
  27. disabled?: boolean;
  28. type?: Type;
  29. multiple?: boolean;
  30. showClear?: boolean;
  31. format?: string;
  32. inputStyle?: React.CSSProperties;
  33. inputReadOnly?: boolean;
  34. validateStatus?: ValidateStatus;
  35. prefixCls?: string;
  36. rangeSeparator?: string;
  37. }
  38. export interface DateInputAdapter extends DefaultAdapter {
  39. updateIsFocusing: (isFocusing: boolean) => void;
  40. notifyClick: DateInputFoundationProps['onClick'];
  41. notifyChange: DateInputFoundationProps['onChange'];
  42. notifyEnter: DateInputFoundationProps['onEnterPress'];
  43. notifyBlur: DateInputFoundationProps['onBlur'];
  44. notifyClear: DateInputFoundationProps['onClear'];
  45. notifyFocus: DateInputFoundationProps['onFocus'];
  46. notifyRangeInputClear: DateInputFoundationProps['onRangeInputClear'];
  47. notifyRangeInputFocus: DateInputFoundationProps['onFocus'];
  48. notifyTabPress: DateInputFoundationProps['onRangeEndTabPress'];
  49. }
  50. export default class InputFoundation extends BaseFoundation<DateInputAdapter> {
  51. constructor(adapter: DateInputAdapter) {
  52. super({ ...adapter });
  53. }
  54. // eslint-disable-next-line @typescript-eslint/no-empty-function
  55. init() {}
  56. // eslint-disable-next-line @typescript-eslint/no-empty-function
  57. destroy() {}
  58. handleClick(e: any) {
  59. this._adapter.notifyClick(e);
  60. }
  61. handleChange(value: string, e: any) {
  62. this._adapter.notifyChange(value, e);
  63. }
  64. handleInputComplete(e: any) {
  65. /**
  66. * onKeyPress, e.key Code gets a value of 0 instead of 13
  67. * Here key is used to judge the button
  68. */
  69. if (e.key === KEY_CODE_ENTER) {
  70. this._adapter.notifyEnter(e.target.value);
  71. }
  72. }
  73. handleInputClear(e: any) {
  74. this._adapter.notifyClear(e);
  75. }
  76. handleRangeInputClear(e: any) {
  77. // prevent trigger click outside
  78. this.stopPropagation(e);
  79. this._adapter.notifyRangeInputClear(e);
  80. }
  81. handleRangeInputEnterPress(e: any, rangeInputValue: string) {
  82. if (e.key === KEY_CODE_ENTER) {
  83. this._adapter.notifyEnter(rangeInputValue);
  84. }
  85. }
  86. handleRangeInputEndKeyPress(e: any) {
  87. if (e.key === KEY_CODE_TAB) {
  88. this._adapter.notifyTabPress(e);
  89. }
  90. }
  91. handleRangeInputFocus(e: any, rangeType: RangeType) {
  92. this._adapter.notifyRangeInputFocus(e, rangeType);
  93. }
  94. formatShowText(value: BaseValueType[]) {
  95. const { type, dateFnsLocale, format, rangeSeparator } = this._adapter.getProps();
  96. const formatToken = format || getDefaultFormatTokenByType(type);
  97. let text = '';
  98. switch (type) {
  99. case 'date':
  100. text = formatDateValues(value, formatToken, undefined, dateFnsLocale);
  101. break;
  102. case 'dateRange':
  103. text = formatDateValues(value, formatToken, { groupSize: 2, groupInnerSeparator: rangeSeparator }, dateFnsLocale);
  104. break;
  105. case 'dateTime':
  106. text = formatDateValues(value, formatToken, undefined, dateFnsLocale);
  107. break;
  108. case 'dateTimeRange':
  109. text = formatDateValues(value, formatToken, { groupSize: 2, groupInnerSeparator: rangeSeparator }, dateFnsLocale);
  110. break;
  111. case 'month':
  112. text = formatDateValues(value, formatToken, undefined, dateFnsLocale);
  113. break;
  114. default:
  115. break;
  116. }
  117. return text;
  118. }
  119. }