Skip to content

Commit c12858e

Browse files
committed
perf(Form): update controlled key mapping to use displayNam
1 parent 95010f2 commit c12858e

File tree

2 files changed

+32
-30
lines changed

2 files changed

+32
-30
lines changed

packages/components/form/FormItem.tsx

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
import { flattenDeep, get, isEqual, isFunction, isObject, isString, merge, set, unset } from 'lodash-es';
21
import React, { forwardRef, ReactNode, useEffect, useImperativeHandle, useMemo, useRef, useState } from 'react';
32
import {
43
CheckCircleFilledIcon as TdCheckCircleFilledIcon,
54
CloseCircleFilledIcon as TdCloseCircleFilledIcon,
65
ErrorCircleFilledIcon as TdErrorCircleFilledIcon,
76
} from 'tdesign-icons-react';
7+
import { flattenDeep, get, isEqual, isFunction, isObject, isString, merge, set, unset } from 'lodash-es';
88
import useConfig from '../hooks/useConfig';
99
import useDefaultProps from '../hooks/useDefaultProps';
1010
import useGlobalIcon from '../hooks/useGlobalIcon';
@@ -14,7 +14,7 @@ import { formItemDefaultProps } from './defaultProps';
1414
import { useFormContext, useFormListContext } from './FormContext';
1515
import { parseMessage, validate as validateModal } from './formModel';
1616
import { HOOK_MARK } from './hooks/useForm';
17-
import useFormItemInitialData, { ctrlKeyMap } from './hooks/useFormItemInitialData';
17+
import useFormItemInitialData, { CTRL_KEY_MAP } from './hooks/useFormItemInitialData';
1818
import useFormItemStyle from './hooks/useFormItemStyle';
1919
import { calcFieldValue } from './utils';
2020

@@ -521,7 +521,9 @@ const FormItem = forwardRef<FormItemInstance, FormItemProps>((originalProps, ref
521521
});
522522
}
523523
if (typeof child.type === 'object') {
524-
ctrlKey = ctrlKeyMap.get(child.type) || 'value';
524+
// @ts-ignore
525+
const componentName = child.type?.displayName;
526+
ctrlKey = CTRL_KEY_MAP.get(componentName) || 'value';
525527
}
526528
const childProps = child.props as any;
527529
return React.cloneElement(child, {

packages/components/form/hooks/useFormItemInitialData.ts

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,33 @@
11
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';
153

164
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'];
1823

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>();
2425

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, []);
2928
});
30-
[Checkbox].forEach((component) => {
31-
initialDataMap.set(component, false);
29+
BOOLEAN_DEFAULT_COMPONENTS.forEach((componentName) => {
30+
initialDataMap.set(componentName, false);
3231
});
3332

3433
export default function useFormItemInitialData(name: FormItemProps['name']) {
@@ -81,9 +80,10 @@ export default function useFormItemInitialData(name: FormItemProps['name']) {
8180
const childList = React.Children.toArray(children);
8281
const lastChild = childList[childList.length - 1];
8382
if (lastChild && React.isValidElement(lastChild)) {
84-
// @ts-ignore
8583
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);
8787
}
8888
}
8989
}

0 commit comments

Comments
 (0)