|
1 | 1 | import React, { useEffect } from 'react'; |
2 | | -import { get, unset, isEmpty } from 'lodash-es'; |
3 | | - |
4 | | -// 兼容特殊数据结构和受控 key |
5 | | -import Tree from '../../tree/Tree'; |
6 | | -import Upload from '../../upload/upload'; |
7 | | -import CheckTag from '../../tag/CheckTag'; |
8 | | -import Checkbox from '../../checkbox/Checkbox'; |
9 | | -import TagInput from '../../tag-input/TagInput'; |
10 | | -import RangeInput from '../../range-input/RangeInput'; |
11 | | -import Transfer from '../../transfer/Transfer'; |
12 | | -import CheckboxGroup from '../../checkbox/CheckboxGroup'; |
13 | | -import DateRangePicker from '../../date-picker/DateRangePicker'; |
14 | | -import TimeRangePicker from '../../time-picker/TimeRangePicker'; |
| 2 | +import { get, isEmpty, unset } from 'lodash-es'; |
15 | 3 |
|
16 | 4 | import { useFormContext, useFormListContext } from '../FormContext'; |
17 | | -import { FormItemProps } from '../FormItem'; |
| 5 | +import type { FormItemProps } from '../FormItem'; |
| 6 | + |
| 7 | +export const CTRL_KEY_MAP = new Map<string, string>(); |
| 8 | +CTRL_KEY_MAP.set('Checkbox', 'checked'); |
| 9 | +CTRL_KEY_MAP.set('CheckTag', 'checked'); |
| 10 | +CTRL_KEY_MAP.set('Upload', 'files'); |
| 11 | + |
| 12 | +const ARRAY_DEFAULT_COMPONENTS = [ |
| 13 | + 'Tree', |
| 14 | + 'Upload', |
| 15 | + 'Transfer', |
| 16 | + 'TagInput', |
| 17 | + 'RangeInput', |
| 18 | + 'CheckboxGroup', |
| 19 | + 'DateRangePicker', |
| 20 | + 'TimeRangePicker', |
| 21 | +]; |
| 22 | +const BOOLEAN_DEFAULT_COMPONENTS = ['Checkbox']; |
18 | 23 |
|
19 | | -// FormItem 子组件受控 key |
20 | | -export const ctrlKeyMap = new Map(); |
21 | | -ctrlKeyMap.set(Checkbox, 'checked'); |
22 | | -ctrlKeyMap.set(CheckTag, 'checked'); |
23 | | -ctrlKeyMap.set(Upload, 'files'); |
| 24 | +export const initialDataMap = new Map<string, any>(); |
24 | 25 |
|
25 | | -// FormItem 默认数据类型 |
26 | | -export const initialDataMap = new Map(); |
27 | | -[Tree, Upload, Transfer, TagInput, RangeInput, CheckboxGroup, DateRangePicker, TimeRangePicker].forEach((component) => { |
28 | | - initialDataMap.set(component, []); |
| 26 | +ARRAY_DEFAULT_COMPONENTS.forEach((componentName) => { |
| 27 | + initialDataMap.set(componentName, []); |
29 | 28 | }); |
30 | | -[Checkbox].forEach((component) => { |
31 | | - initialDataMap.set(component, false); |
| 29 | +BOOLEAN_DEFAULT_COMPONENTS.forEach((componentName) => { |
| 30 | + initialDataMap.set(componentName, false); |
32 | 31 | }); |
33 | 32 |
|
34 | 33 | export default function useFormItemInitialData(name: FormItemProps['name']) { |
@@ -81,9 +80,10 @@ export default function useFormItemInitialData(name: FormItemProps['name']) { |
81 | 80 | const childList = React.Children.toArray(children); |
82 | 81 | const lastChild = childList[childList.length - 1]; |
83 | 82 | if (lastChild && React.isValidElement(lastChild)) { |
84 | | - // @ts-ignore |
85 | 83 | const isMultiple = lastChild?.props?.multiple; |
86 | | - return isMultiple ? [] : initialDataMap.get(lastChild.type); |
| 84 | + // @ts-ignore |
| 85 | + const componentName = lastChild.type.displayName; |
| 86 | + return isMultiple ? [] : initialDataMap.get(componentName); |
87 | 87 | } |
88 | 88 | } |
89 | 89 | } |
|
0 commit comments