inputFoundation.ts 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. /* eslint-disable max-len */
  2. import { cloneDeep, isObject, set, get } from 'lodash';
  3. import { format as formatFn } from 'date-fns';
  4. import BaseFoundation, { DefaultAdapter } from '../base/foundation';
  5. import { BaseValueType, ValidateStatus, ValueType } from './foundation';
  6. import { formatDateValues } from './_utils/formatter';
  7. import { getDefaultFormatTokenByType } from './_utils/getDefaultFormatToken';
  8. import getInsetInputFormatToken from './_utils/getInsetInputFormatToken';
  9. import getInsetInputValueFromInsetInputStr from './_utils/getInsetInputValueFromInsetInputStr';
  10. import { strings } from './constants';
  11. import getDefaultPickerDate from './_utils/getDefaultPickerDate';
  12. import { compatibleParse } from './_utils/parser';
  13. import { isValidDate } from './_utils';
  14. const KEY_CODE_ENTER = 'Enter';
  15. const KEY_CODE_TAB = 'Tab';
  16. export type Type = 'date' | 'dateRange' | 'year' | 'month' | 'dateTime' | 'dateTimeRange';
  17. export type RangeType = 'rangeStart' | 'rangeEnd';
  18. export type PanelType = 'left' | 'right';
  19. export interface DateInputEventHandlerProps {
  20. onClick?: (e: any) => void;
  21. onChange?: (value: string, e: any) => void;
  22. onEnterPress?: (e: any) => void;
  23. onBlur?: (e: any) => void;
  24. onFocus?: (e: any, rangeType: RangeType) => void;
  25. onClear?: (e: any) => void;
  26. onRangeInputClear?: (e: any) => void;
  27. onRangeEndTabPress?: (e: any) => void;
  28. onInsetInputChange?: (options: InsetInputChangeProps) => void;
  29. }
  30. export interface DateInputElementProps {
  31. insetLabel?: any;
  32. prefix?: any;
  33. }
  34. export interface DateInputFoundationProps extends DateInputElementProps, DateInputEventHandlerProps {
  35. [x: string]: any;
  36. value?: BaseValueType[];
  37. disabled?: boolean;
  38. type?: Type;
  39. showClear?: boolean;
  40. format?: string;
  41. inputStyle?: Record<string, any>;
  42. inputReadOnly?: boolean;
  43. validateStatus?: ValidateStatus;
  44. prefixCls?: string;
  45. rangeSeparator?: string;
  46. panelType?: PanelType;
  47. insetInput?: boolean;
  48. insetInputValue?: InsetInputValue;
  49. density?: typeof strings.DENSITY_SET[number];
  50. defaultPickerValue?: ValueType;
  51. }
  52. export interface InsetInputValue {
  53. monthLeft: {
  54. dateInput: string;
  55. timeInput: string;
  56. },
  57. monthRight: {
  58. dateInput: string;
  59. timeInput: string;
  60. }
  61. }
  62. export interface InsetInputChangeFoundationProps {
  63. value: string;
  64. insetInputValue: InsetInputValue;
  65. event: any;
  66. valuePath: string;
  67. }
  68. export interface InsetInputChangeProps {
  69. insetInputStr: string;
  70. format: string;
  71. insetInputValue: InsetInputValue;
  72. }
  73. export interface DateInputAdapter extends DefaultAdapter {
  74. updateIsFocusing: (isFocusing: boolean) => void;
  75. notifyClick: DateInputFoundationProps['onClick'];
  76. notifyChange: DateInputFoundationProps['onChange'];
  77. notifyInsetInputChange: DateInputFoundationProps['onInsetInputChange'];
  78. notifyEnter: DateInputFoundationProps['onEnterPress'];
  79. notifyBlur: DateInputFoundationProps['onBlur'];
  80. notifyClear: DateInputFoundationProps['onClear'];
  81. notifyFocus: DateInputFoundationProps['onFocus'];
  82. notifyRangeInputClear: DateInputFoundationProps['onRangeInputClear'];
  83. notifyRangeInputFocus: DateInputFoundationProps['onFocus'];
  84. notifyTabPress: DateInputFoundationProps['onRangeEndTabPress'];
  85. }
  86. export default class InputFoundation extends BaseFoundation<DateInputAdapter> {
  87. constructor(adapter: DateInputAdapter) {
  88. super({ ...adapter });
  89. }
  90. // eslint-disable-next-line @typescript-eslint/no-empty-function
  91. init() {}
  92. // eslint-disable-next-line @typescript-eslint/no-empty-function
  93. destroy() {}
  94. handleClick(e: any) {
  95. this._adapter.notifyClick(e);
  96. }
  97. handleChange(value: string, e: any) {
  98. this._adapter.notifyChange(value, e);
  99. }
  100. handleInputComplete(e: any) {
  101. /**
  102. * onKeyPress, e.key Code gets a value of 0 instead of 13
  103. * Here key is used to judge the button
  104. */
  105. if (e.key === KEY_CODE_ENTER) {
  106. this._adapter.notifyEnter(e.target.value);
  107. }
  108. }
  109. handleInputClear(e: any) {
  110. this._adapter.notifyClear(e);
  111. }
  112. handleRangeInputClear(e: any) {
  113. // prevent trigger click outside
  114. this.stopPropagation(e);
  115. this._adapter.notifyRangeInputClear(e);
  116. }
  117. handleRangeInputEnterPress(e: any, rangeInputValue: string) {
  118. if (e.key === KEY_CODE_ENTER) {
  119. this._adapter.notifyEnter(rangeInputValue);
  120. }
  121. }
  122. handleRangeInputEndKeyPress(e: any) {
  123. if (e.key === KEY_CODE_TAB) {
  124. this._adapter.notifyTabPress(e);
  125. }
  126. }
  127. handleRangeInputFocus(e: any, rangeType: RangeType) {
  128. this._adapter.notifyRangeInputFocus(e, rangeType);
  129. }
  130. formatShowText(value: BaseValueType[], customFormat?: string) {
  131. const { type, dateFnsLocale, format, rangeSeparator } = this._adapter.getProps();
  132. const formatToken = customFormat || format || getDefaultFormatTokenByType(type);
  133. let text = '';
  134. switch (type) {
  135. case 'date':
  136. text = formatDateValues(value, formatToken, undefined, dateFnsLocale);
  137. break;
  138. case 'dateRange':
  139. text = formatDateValues(value, formatToken, { groupSize: 2, groupInnerSeparator: rangeSeparator }, dateFnsLocale);
  140. break;
  141. case 'dateTime':
  142. text = formatDateValues(value, formatToken, undefined, dateFnsLocale);
  143. break;
  144. case 'dateTimeRange':
  145. text = formatDateValues(value, formatToken, { groupSize: 2, groupInnerSeparator: rangeSeparator }, dateFnsLocale);
  146. break;
  147. case 'month':
  148. text = formatDateValues(value, formatToken, undefined, dateFnsLocale);
  149. break;
  150. default:
  151. break;
  152. }
  153. return text;
  154. }
  155. handleInsetInputChange(options: InsetInputChangeFoundationProps) {
  156. const { value, valuePath, insetInputValue } = options;
  157. const { format, type } = this._adapter.getProps();
  158. const insetFormatToken = getInsetInputFormatToken({ type, format });
  159. let newInsetInputValue = set(cloneDeep(insetInputValue), valuePath, value);
  160. newInsetInputValue = this._autoFillTimeToInsetInputValue({ insetInputValue: newInsetInputValue, valuePath, format: insetFormatToken });
  161. const newInputValue = this.concatInsetInputValue({ insetInputValue: newInsetInputValue });
  162. this._adapter.notifyInsetInputChange({ insetInputValue: newInsetInputValue, format: insetFormatToken, insetInputStr: newInputValue });
  163. }
  164. _autoFillTimeToInsetInputValue(options: { insetInputValue: InsetInputValue; format: string; valuePath: string;}) {
  165. const { valuePath, insetInputValue, format } = options;
  166. const { type, defaultPickerValue, dateFnsLocale } = this._adapter.getProps();
  167. const insetInputValueWithTime = cloneDeep(insetInputValue);
  168. const { nowDate, nextDate } = getDefaultPickerDate({ defaultPickerValue, format, dateFnsLocale });
  169. if (type.includes('Time')) {
  170. let timeStr = '';
  171. const dateFormatToken = get(format.split(' '), '0', strings.FORMAT_FULL_DATE);
  172. const timeFormatToken = get(format.split(' '), '1', strings.FORMAT_TIME_PICKER);
  173. switch (valuePath) {
  174. case 'monthLeft.dateInput':
  175. const dateLeftStr = insetInputValueWithTime.monthLeft.dateInput;
  176. if (!insetInputValueWithTime.monthLeft.timeInput && dateLeftStr.length === dateFormatToken.length) {
  177. const dateLeftParsed = compatibleParse(insetInputValueWithTime.monthLeft.dateInput, dateFormatToken);
  178. if (isValidDate(dateLeftParsed)) {
  179. timeStr = formatFn(nowDate, timeFormatToken);
  180. insetInputValueWithTime.monthLeft.timeInput = timeStr;
  181. }
  182. }
  183. break;
  184. case 'monthRight.dateInput':
  185. const dateRightStr = insetInputValueWithTime.monthRight.dateInput;
  186. if (!insetInputValueWithTime.monthRight.timeInput && dateRightStr.length === dateFormatToken.length) {
  187. const dateRightParsed = compatibleParse(dateRightStr, dateFormatToken);
  188. if (isValidDate(dateRightParsed)) {
  189. timeStr = formatFn(nextDate, timeFormatToken);
  190. insetInputValueWithTime.monthRight.timeInput = timeStr;
  191. }
  192. }
  193. break;
  194. default:
  195. break;
  196. }
  197. }
  198. return insetInputValueWithTime;
  199. }
  200. /**
  201. * 只有传入的 format 符合 formatReg 时,才会使用用户传入的 format
  202. * 否则会使用默认的 format 作为 placeholder
  203. *
  204. * The format passed in by the user will be used only if the incoming format conforms to formatReg
  205. * Otherwise the default format will be used as placeholder
  206. */
  207. getInsetInputPlaceholder() {
  208. const { type, format } = this._adapter.getProps();
  209. const insetInputFormat = getInsetInputFormatToken({ type, format });
  210. let datePlaceholder, timePlaceholder;
  211. switch (type) {
  212. case 'date':
  213. case 'month':
  214. case 'dateRange':
  215. datePlaceholder = insetInputFormat;
  216. break;
  217. case 'dateTime':
  218. case 'dateTimeRange':
  219. [datePlaceholder, timePlaceholder] = insetInputFormat.split(' ');
  220. break;
  221. }
  222. return ({
  223. datePlaceholder,
  224. timePlaceholder,
  225. });
  226. }
  227. /**
  228. * 从当前日期值或 inputValue 中解析出 insetInputValue
  229. *
  230. * Parse out insetInputValue from current date value or inputValue
  231. */
  232. getInsetInputValue({ value, insetInputValue } : { value: BaseValueType[]; insetInputValue: InsetInputValue }) {
  233. const { type, rangeSeparator, format } = this._adapter.getProps();
  234. let inputValueStr = '';
  235. if (isObject(insetInputValue)) {
  236. inputValueStr = this.concatInsetInputValue({ insetInputValue });
  237. } else {
  238. const insetInputFormat = getInsetInputFormatToken({ format, type });
  239. inputValueStr = this.formatShowText(value, insetInputFormat);
  240. }
  241. const newInsetInputValue = getInsetInputValueFromInsetInputStr({ inputValue: inputValueStr, type, rangeSeparator });
  242. return newInsetInputValue;
  243. }
  244. concatInsetDateAndTime({ date, time }) {
  245. return `${date} ${time}`;
  246. }
  247. concatInsetDateRange({ rangeStart, rangeEnd }) {
  248. const { rangeSeparator } = this._adapter.getProps();
  249. return `${rangeStart}${rangeSeparator}${rangeEnd}`;
  250. }
  251. concatInsetInputValue({ insetInputValue }: { insetInputValue: InsetInputValue }) {
  252. const { type } = this._adapter.getProps();
  253. let inputValue = '';
  254. switch (type) {
  255. case 'date':
  256. case 'month':
  257. inputValue = insetInputValue.monthLeft.dateInput;
  258. break;
  259. case 'dateRange':
  260. inputValue = this.concatInsetDateRange({ rangeStart: insetInputValue.monthLeft.dateInput, rangeEnd: insetInputValue.monthRight.dateInput });
  261. break;
  262. case 'dateTime':
  263. inputValue = this.concatInsetDateAndTime({ date: insetInputValue.monthLeft.dateInput, time: insetInputValue.monthLeft.timeInput });
  264. break;
  265. case 'dateTimeRange':
  266. const rangeStart = this.concatInsetDateAndTime({ date: insetInputValue.monthLeft.dateInput, time: insetInputValue.monthLeft.timeInput });
  267. const rangeEnd = this.concatInsetDateAndTime({ date: insetInputValue.monthRight.dateInput, time: insetInputValue.monthRight.timeInput });
  268. inputValue = this.concatInsetDateRange({ rangeStart, rangeEnd });
  269. break;
  270. }
  271. return inputValue;
  272. }
  273. }