inputFoundation.ts 13 KB

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