Skip to content

Commit

Permalink
chore(form): fixed type issues
Browse files Browse the repository at this point in the history
  • Loading branch information
l123wx committed Oct 15, 2024
1 parent ceebac6 commit 581ddb5
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 10 deletions.
2 changes: 1 addition & 1 deletion src/form/FormContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export const FormListContext = React.createContext<{
name: NamePath;
rules: TdFormListProps['rules'];
formListMapRef: React.RefObject<Map<any, React.RefObject<FormItemInstance>>>;
initialData: TdFormProps['initialData'];
initialData: TdFormListProps['initialData'];
}>({
name: undefined,
rules: undefined,
Expand Down
2 changes: 1 addition & 1 deletion src/form/FormItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import useDefaultProps from '../hooks/useDefaultProps';
import { useLocaleReceiver } from '../locale/LocalReceiver';

export interface FormItemProps extends TdFormItemProps, StyledProps {
children?: React.ReactNode | ((form: FormInstanceFunctions) => React.ReactElement);
children?: React.ReactNode | React.ReactNode[] | ((form: FormInstanceFunctions) => React.ReactElement);
}

export interface FormItemInstance {
Expand Down
31 changes: 23 additions & 8 deletions src/form/useInitialData.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import React from 'react';
import get from 'lodash/get';
import isFunction from 'lodash/isFunction';

// 兼容特殊数据结构和受控 key
import Tree from '../tree/Tree';
Expand All @@ -13,6 +14,9 @@ import CheckboxGroup from '../checkbox/CheckboxGroup';
import DateRangePicker from '../date-picker/DateRangePicker';
import TimeRangePicker from '../time-picker/TimeRangePicker';

import { FormItemProps } from './FormItem';
import { TdFormListProps, TdFormProps } from './type';

// FormItem 子组件受控 key
export const ctrlKeyMap = new Map();
ctrlKeyMap.set(Checkbox, 'checked');
Expand All @@ -36,25 +40,36 @@ export function getDefaultInitialData({
initialData,
FormContextInitialData,
FormListInitialData,
}: {
name: FormItemProps['name'];
formListName: TdFormListProps['name'];
children: FormItemProps['children'];
initialData: FormItemProps['initialData'];
FormContextInitialData: TdFormProps['initialData'];
FormListInitialData: TdFormListProps['initialData'];
}) {
let defaultInitialData;
if (FormContextInitialData) {
if (typeof name === 'string') defaultInitialData = get(FormContextInitialData, name);
if (Array.isArray(name)) {
const nameList = formListName ? [formListName, ...name] : name;
const nameList = formListName ? [formListName, name].flat() : name;
defaultInitialData = get(FormContextInitialData, nameList);
}
}
if (FormListInitialData.length) {
defaultInitialData = get(FormListInitialData, name);
}
if (typeof initialData !== 'undefined') defaultInitialData = initialData;
React.Children.forEach(children, (child) => {
if (child && React.isValidElement(child) && typeof defaultInitialData === 'undefined') {
// @ts-ignore
const isMultiple = child?.props?.multiple;
defaultInitialData = isMultiple ? [] : initialDataMap.get(child.type);
}
});

if (!isFunction(children)) {
React.Children.forEach(children, (child) => {
if (child && React.isValidElement(child) && typeof defaultInitialData === 'undefined') {
// @ts-ignore
const isMultiple = child?.props?.multiple;
defaultInitialData = isMultiple ? [] : initialDataMap.get(child.type);
}
});
}

return defaultInitialData;
}

0 comments on commit 581ddb5

Please sign in to comment.