inputFoundation.ts 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. import { isObject, set, get } from 'lodash';
  2. import { format as formatFn } from 'date-fns';
  3. import BaseFoundation, { DefaultAdapter } from '../base/foundation';
  4. import { BaseValueType, ValidateStatus, ValueType } from './foundation';
  5. import { formatDateValues } from './_utils/formatter';
  6. import { getDefaultFormatTokenByType } from './_utils/getDefaultFormatToken';
  7. import getInsetInputFormatToken from './_utils/getInsetInputFormatToken';
  8. import getInsetInputValueFromInsetInputStr from './_utils/getInsetInputValueFromInsetInputStr';
  9. import { strings } from './constants';
  10. import getDefaultPickerDate from './_utils/getDefaultPickerDate';
  11. import { compatibleParse } from './_utils/parser';
  12. import { isValidDate } from './_utils';
  13. import copy from 'fast-copy';
  14. import { TZDate } from '@date-fns/tz';
  15. const KEY_CODE_ENTER = 'Enter';
  16. const KEY_CODE_TAB = 'Tab';
  17. export type Type = 'date' | 'dateRange' | 'year' | 'month' | 'dateTime' | 'dateTimeRange' | 'monthRange';
  18. export type RangeType = 'rangeStart' | 'rangeEnd';
  19. export type PanelType = 'left' | 'right';
  20. export interface DateInputEventHandlerProps {
  21. onClick?: (e: any) => void;
  22. onChange?: (value: string, e: any) => void;
  23. onEnterPress?: (e: any) => void;
  24. onBlur?: (e: any) => void;
  25. onFocus?: (e: any, rangeType: RangeType) => void;
  26. onClear?: (e: any) => void;
  27. onRangeInputClear?: (e: any) => void;
  28. onRangeEndTabPress?: (e: any) => void;
  29. onInsetInputChange?: (options: InsetInputChangeProps) => void
  30. }
  31. export interface DateInputElementProps {
  32. borderless?: boolean;
  33. insetLabel?: any;
  34. prefix?: any
  35. }
  36. export interface InsetInputProps {
  37. placeholder?: {
  38. dateStart?: string;
  39. dateEnd?: string;
  40. timeStart?: string;
  41. timeEnd?: string
  42. }
  43. // showClear?: boolean
  44. }
  45. export interface DateInputFoundationProps extends DateInputElementProps, DateInputEventHandlerProps {
  46. [x: string]: any;
  47. value?: TZDate[];
  48. disabled?: boolean;
  49. type?: Type;
  50. showClear?: boolean;
  51. format?: string;
  52. inputStyle?: Record<string, any>;
  53. inputReadOnly?: boolean;
  54. validateStatus?: ValidateStatus;
  55. prefixCls?: string;
  56. rangeSeparator?: string;
  57. panelType?: PanelType;
  58. insetInput?: boolean | InsetInputProps;
  59. insetInputValue?: InsetInputValue;
  60. density?: typeof strings.DENSITY_SET[number];
  61. defaultPickerValue?: ValueType;
  62. timeZone?: string | number
  63. }
  64. export interface InsetInputValue {
  65. monthLeft: {
  66. dateInput: string;
  67. timeInput: string
  68. };
  69. monthRight: {
  70. dateInput: string;
  71. timeInput: string
  72. }
  73. }
  74. export interface InsetInputChangeFoundationProps {
  75. value: string;
  76. insetInputValue: InsetInputValue;
  77. event: any;
  78. valuePath: string
  79. }
  80. export interface InsetInputChangeProps {
  81. insetInputStr: string;
  82. format: string;
  83. insetInputValue: InsetInputValue
  84. }
  85. export interface DateInputAdapter extends DefaultAdapter<DateInputFoundationProps, Record<string, any>> {
  86. updateIsFocusing: (isFocusing: boolean) => void;
  87. notifyClick: DateInputFoundationProps['onClick'];
  88. notifyChange: DateInputFoundationProps['onChange'];
  89. notifyInsetInputChange: DateInputFoundationProps['onInsetInputChange'];
  90. notifyEnter: DateInputFoundationProps['onEnterPress'];
  91. notifyBlur: DateInputFoundationProps['onBlur'];
  92. notifyClear: DateInputFoundationProps['onClear'];
  93. notifyFocus: DateInputFoundationProps['onFocus'];
  94. notifyRangeInputClear: DateInputFoundationProps['onRangeInputClear'];
  95. notifyRangeInputFocus: DateInputFoundationProps['onFocus'];
  96. notifyTabPress: DateInputFoundationProps['onRangeEndTabPress']
  97. }
  98. export default class InputFoundation extends BaseFoundation<DateInputAdapter> {
  99. constructor(adapter: DateInputAdapter) {
  100. super({ ...adapter });
  101. }
  102. init() {}
  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, rangeSeparator } = this._adapter.getProps();
  171. const insetFormatToken = getInsetInputFormatToken({ type, format });
  172. const newInsetInputValue = set(copy(insetInputValue), valuePath, value);
  173. const insetInputStr = this.concatInsetInputValue({ insetInputValue: newInsetInputValue });
  174. const parsedInsetInputValueFromInputStr = getInsetInputValueFromInsetInputStr({ inputValue: insetInputStr, type, rangeSeparator });
  175. const filledTimeInsetInputValue = this._autoFillTimeToInsetInputValue({ insetInputValue: parsedInsetInputValueFromInputStr, valuePath, format: insetFormatToken });
  176. const finalInsetInputStr = this.concatInsetInputValue({ insetInputValue: filledTimeInsetInputValue });
  177. this._adapter.notifyInsetInputChange({ insetInputValue: filledTimeInsetInputValue, format: insetFormatToken, insetInputStr: finalInsetInputStr });
  178. }
  179. _autoFillTimeToInsetInputValue(options: { insetInputValue: InsetInputValue; format: string; valuePath: string}) {
  180. const { valuePath, insetInputValue, format } = options;
  181. const { type, defaultPickerValue, dateFnsLocale, timeZone } = this._adapter.getProps();
  182. const insetInputValueWithTime = copy(insetInputValue);
  183. const { nowDate, nextDate } = getDefaultPickerDate({ defaultPickerValue, format, dateFnsLocale, timeZone });
  184. if (type.includes('Time')) {
  185. let timeStr = '';
  186. const dateFormatToken = get(format.split(' '), '0', strings.FORMAT_FULL_DATE);
  187. const timeFormatToken = get(format.split(' '), '1', strings.FORMAT_TIME_PICKER);
  188. switch (valuePath) {
  189. case 'monthLeft.dateInput':
  190. const dateLeftStr = insetInputValueWithTime.monthLeft.dateInput;
  191. if (!insetInputValueWithTime.monthLeft.timeInput && dateLeftStr.length === dateFormatToken.length) {
  192. const dateLeftParsed = compatibleParse(insetInputValueWithTime.monthLeft.dateInput, dateFormatToken);
  193. if (isValidDate(dateLeftParsed)) {
  194. timeStr = formatFn(nowDate, timeFormatToken);
  195. insetInputValueWithTime.monthLeft.timeInput = timeStr;
  196. }
  197. }
  198. break;
  199. case 'monthRight.dateInput':
  200. const dateRightStr = insetInputValueWithTime.monthRight.dateInput;
  201. if (!insetInputValueWithTime.monthRight.timeInput && dateRightStr.length === dateFormatToken.length) {
  202. const dateRightParsed = compatibleParse(dateRightStr, dateFormatToken);
  203. if (isValidDate(dateRightParsed)) {
  204. timeStr = formatFn(nextDate, timeFormatToken);
  205. insetInputValueWithTime.monthRight.timeInput = timeStr;
  206. }
  207. }
  208. break;
  209. default:
  210. break;
  211. }
  212. }
  213. return insetInputValueWithTime;
  214. }
  215. /**
  216. * 只有传入的 format 符合 formatReg 时,才会使用用户传入的 format
  217. * 否则会使用默认的 format 作为 placeholder
  218. *
  219. * The format passed in by the user will be used only if the incoming format conforms to formatReg
  220. * Otherwise the default format will be used as placeholder
  221. */
  222. getInsetInputPlaceholder() {
  223. const { type, format, rangeSeparator } = this._adapter.getProps();
  224. const insetInputFormat = getInsetInputFormatToken({ type, format });
  225. let datePlaceholder, timePlaceholder;
  226. switch (type) {
  227. case 'date':
  228. case 'month':
  229. case 'dateRange':
  230. datePlaceholder = insetInputFormat;
  231. break;
  232. case 'dateTime':
  233. case 'dateTimeRange':
  234. [datePlaceholder, timePlaceholder] = insetInputFormat.split(' ');
  235. break;
  236. case 'monthRange':
  237. datePlaceholder = insetInputFormat + rangeSeparator + insetInputFormat;
  238. break;
  239. }
  240. return ({
  241. datePlaceholder,
  242. timePlaceholder,
  243. });
  244. }
  245. /**
  246. * 从当前日期值或 inputValue 中解析出 insetInputValue
  247. *
  248. * Parse out insetInputValue from current date value or inputValue
  249. */
  250. getInsetInputValue({ value, insetInputValue }: { value: BaseValueType[]; insetInputValue: InsetInputValue }) {
  251. const { type, rangeSeparator, format } = this._adapter.getProps();
  252. let inputValueStr = '';
  253. if (isObject(insetInputValue)) {
  254. inputValueStr = this.concatInsetInputValue({ insetInputValue });
  255. } else {
  256. const insetInputFormat = getInsetInputFormatToken({ format, type });
  257. inputValueStr = this.formatShowText(value, insetInputFormat);
  258. }
  259. const newInsetInputValue = getInsetInputValueFromInsetInputStr({ inputValue: inputValueStr, type, rangeSeparator });
  260. return newInsetInputValue;
  261. }
  262. concatInsetDateAndTime({ date, time }) {
  263. return `${date} ${time}`;
  264. }
  265. concatInsetDateRange({ rangeStart, rangeEnd }) {
  266. const { rangeSeparator } = this._adapter.getProps();
  267. return `${rangeStart}${rangeSeparator}${rangeEnd}`;
  268. }
  269. concatInsetInputValue({ insetInputValue }: { insetInputValue: InsetInputValue }) {
  270. const { type } = this._adapter.getProps();
  271. let inputValue = '';
  272. switch (type) {
  273. case 'date':
  274. case 'month':
  275. case 'monthRange':
  276. inputValue = insetInputValue.monthLeft.dateInput;
  277. break;
  278. case 'dateRange':
  279. inputValue = this.concatInsetDateRange({ rangeStart: insetInputValue.monthLeft.dateInput, rangeEnd: insetInputValue.monthRight.dateInput });
  280. break;
  281. case 'dateTime':
  282. inputValue = this.concatInsetDateAndTime({ date: insetInputValue.monthLeft.dateInput, time: insetInputValue.monthLeft.timeInput });
  283. break;
  284. case 'dateTimeRange':
  285. const rangeStart = this.concatInsetDateAndTime({ date: insetInputValue.monthLeft.dateInput, time: insetInputValue.monthLeft.timeInput });
  286. const rangeEnd = this.concatInsetDateAndTime({ date: insetInputValue.monthRight.dateInput, time: insetInputValue.monthRight.timeInput });
  287. inputValue = this.concatInsetDateRange({ rangeStart, rangeEnd });
  288. break;
  289. }
  290. return inputValue;
  291. }
  292. }