inputFoundation.ts 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. this._adapter.notifyRangeInputClear(e);
  78. }
  79. handleRangeInputEnterPress(e: any, rangeInputValue: string) {
  80. if (e.key === KEY_CODE_ENTER) {
  81. this._adapter.notifyEnter(rangeInputValue);
  82. }
  83. }
  84. handleRangeInputEndKeyPress(e: any) {
  85. if (e.key === KEY_CODE_TAB) {
  86. this._adapter.notifyTabPress(e);
  87. }
  88. }
  89. handleRangeInputFocus(e: any, rangeType: RangeType) {
  90. this._adapter.notifyRangeInputFocus(e, rangeType);
  91. }
  92. formatShowText(value: BaseValueType[]) {
  93. const { type, dateFnsLocale, format, rangeSeparator } = this._adapter.getProps();
  94. const formatToken = format || getDefaultFormatTokenByType(type);
  95. let text = '';
  96. switch (type) {
  97. case 'date':
  98. text = formatDateValues(value, formatToken, undefined, dateFnsLocale);
  99. break;
  100. case 'dateRange':
  101. text = formatDateValues(value, formatToken, { groupSize: 2, groupInnerSeparator: rangeSeparator }, dateFnsLocale);
  102. break;
  103. case 'dateTime':
  104. text = formatDateValues(value, formatToken, undefined, dateFnsLocale);
  105. break;
  106. case 'dateTimeRange':
  107. text = formatDateValues(value, formatToken, { groupSize: 2, groupInnerSeparator: rangeSeparator }, dateFnsLocale);
  108. break;
  109. case 'month':
  110. text = formatDateValues(value, formatToken, undefined, dateFnsLocale);
  111. break;
  112. default:
  113. break;
  114. }
  115. return text;
  116. }
  117. }