inputFoundation.ts 13 KB

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