-
Notifications
You must be signed in to change notification settings - Fork 358
Expand file tree
/
Copy pathuseFormItemInitialData.ts
More file actions
102 lines (85 loc) · 3.53 KB
/
useFormItemInitialData.ts
File metadata and controls
102 lines (85 loc) · 3.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
import React, { useEffect } from 'react';
import { get, has, isEmpty, unset } from 'lodash-es';
import { useFormContext, useFormListContext } from '../FormContext';
import { FormItemProps } from '../FormItem';
import { TD_DEFAULT_VALUE_MAP } from '../const';
import type { NamePath } from '../type';
export default function useFormItemInitialData(
name: NamePath,
fullPath: NamePath,
initialData: FormItemProps['initialData'],
children: FormItemProps['children'],
) {
let hadReadFloatingFormData = false;
const { form, floatingFormDataRef, initialData: formContextInitialData } = useFormContext();
const { name: formListName, initialData: formListInitialData } = useFormListContext();
// 组件渲染后删除对应游离值
useEffect(() => {
if (hadReadFloatingFormData) {
const nameList = formListName ? [formListName, name].flat() : name;
unset(floatingFormDataRef.current, nameList);
}
}, [hadReadFloatingFormData, floatingFormDataRef, formListName, name]);
const defaultInitialData = getDefaultInitialData(children, initialData);
// 优先级:floatFormData > FormItem.initialData > FormList.initialData > Form.initialData
function getDefaultInitialData(children: FormItemProps['children'], initialData: FormItemProps['initialData']) {
if (name && floatingFormDataRef?.current && !isEmpty(floatingFormDataRef.current)) {
const nameList = formListName ? [formListName, name].flat() : name;
const defaultInitialData = get(floatingFormDataRef.current, nameList);
if (typeof defaultInitialData !== 'undefined') {
// 首次渲染
hadReadFloatingFormData = true;
return defaultInitialData;
}
}
if (typeof initialData !== 'undefined') {
return initialData;
}
if (formListName && Array.isArray(fullPath)) {
const pathPrefix = fullPath.slice(0, -1);
const parentPathExisted = has(form.store, pathPrefix);
if (parentPathExisted) {
// 只要路径存在,哪怕值为 undefined 也取 store 里的值
// 兼容 add() 或者 add({}) 导致的空对象场景
// https://github.com/Tencent/tdesign-react/issues/2329
return get(form.store, fullPath);
}
}
if (formListInitialData?.length && (typeof name === 'number' || Array.isArray(name))) {
const fullPathExisted = has(form.store, fullPath);
if (fullPathExisted) {
return get(form.store, fullPath);
}
let defaultInitialData;
let index;
let relativePath = [];
if (typeof name === 'number') {
index = name;
} else {
[index, ...relativePath] = name;
}
const itemData = formListInitialData[index];
if (itemData) {
defaultInitialData = relativePath.length ? get(itemData, relativePath) : itemData;
}
if (typeof defaultInitialData !== 'undefined') return defaultInitialData;
}
if (name && formContextInitialData) {
const defaultInitialData = get(formContextInitialData, name);
if (typeof defaultInitialData !== 'undefined') return defaultInitialData;
}
if (typeof children !== 'function') {
const childList = React.Children.toArray(children);
const lastChild = childList[childList.length - 1];
if (lastChild && React.isValidElement(lastChild)) {
const isMultiple = lastChild?.props?.multiple;
// @ts-ignore
const componentName = lastChild.type.displayName;
return isMultiple ? [] : TD_DEFAULT_VALUE_MAP.get(componentName);
}
}
}
return {
defaultInitialData,
};
}