forked from Tencent/tdesign-react
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
160 additions
and
116 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
import React from 'react'; | ||
import get from 'lodash/get'; | ||
|
||
// 兼容特殊数据结构和受控 key | ||
import Tree from '../../tree/Tree'; | ||
import Upload from '../../upload/upload'; | ||
import CheckTag from '../../tag/CheckTag'; | ||
import Checkbox from '../../checkbox/Checkbox'; | ||
import TagInput from '../../tag-input/TagInput'; | ||
import RangeInput from '../../range-input/RangeInput'; | ||
import Transfer from '../../transfer/Transfer'; | ||
import CheckboxGroup from '../../checkbox/CheckboxGroup'; | ||
import DateRangePicker from '../../date-picker/DateRangePicker'; | ||
import TimeRangePicker from '../../time-picker/TimeRangePicker'; | ||
|
||
import { useFormContext, useFormListContext } from '../FormContext'; | ||
import { FormItemProps } from '../FormItem'; | ||
|
||
// FormItem 子组件受控 key | ||
export const ctrlKeyMap = new Map(); | ||
ctrlKeyMap.set(Checkbox, 'checked'); | ||
ctrlKeyMap.set(CheckTag, 'checked'); | ||
ctrlKeyMap.set(Upload, 'files'); | ||
|
||
// FormItem 默认数据类型 | ||
export const initialDataMap = new Map(); | ||
[Tree, Upload, Transfer, TagInput, RangeInput, CheckboxGroup, DateRangePicker, TimeRangePicker].forEach((component) => { | ||
initialDataMap.set(component, []); | ||
}); | ||
[Checkbox].forEach((component) => { | ||
initialDataMap.set(component, false); | ||
}); | ||
|
||
export default function useFormItemInitialData() { | ||
const { form, initialData: formContextInitialData } = useFormContext(); | ||
const { floatingFormData } = form; | ||
|
||
const { name: formListName, initialData: formListInitialData } = useFormListContext(); | ||
|
||
// 整理初始值 优先级:Form.initialData < FormList.initialData < FormItem.initialData < floatFormData | ||
function getDefaultInitialData({ | ||
name, | ||
children, | ||
initialData, | ||
}: { | ||
name: FormItemProps['name']; | ||
children: FormItemProps['children']; | ||
initialData: FormItemProps['initialData']; | ||
}) { | ||
if (name && floatingFormData) { | ||
const nameList = formListName ? [formListName, name].flat() : name; | ||
const defaultInitialData = get(floatingFormData, nameList); | ||
if (typeof defaultInitialData !== 'undefined') return defaultInitialData; | ||
} | ||
|
||
if (initialData) { | ||
return initialData; | ||
} | ||
|
||
if (name && formListInitialData.length) { | ||
const defaultInitialData = get(formListInitialData, name); | ||
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)) { | ||
// @ts-ignore | ||
const isMultiple = lastChild?.props?.multiple; | ||
return isMultiple ? [] : initialDataMap.get(lastChild.type); | ||
} | ||
} | ||
} | ||
|
||
return { | ||
getDefaultInitialData, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.