ant-design/components/form/FormList.tsx
Debiancc ca5b47f674
fix: Add type of initialValue prop to Form.List (#28153)
* fix: Add type of initialValue prop to Form.List

* Update index.zh-CN.md

* Update index.en-US.md
2020-12-02 21:28:46 +08:00

62 lines
1.6 KiB
TypeScript

import * as React from 'react';
import { List } from 'rc-field-form';
import { ValidatorRule, StoreValue } from 'rc-field-form/lib/interface';
import devWarning from '../_util/devWarning';
import { ConfigContext } from '../config-provider';
import { FormItemPrefixContext } from './context';
export interface FormListFieldData {
name: number;
key: number;
fieldKey: number;
}
export interface FormListOperation {
add: (defaultValue?: StoreValue, insertIndex?: number) => void;
remove: (index: number | number[]) => void;
move: (from: number, to: number) => void;
}
export interface FormListProps {
prefixCls?: string;
name: string | number | (string | number)[];
rules?: ValidatorRule[];
initialValue?: any[];
children: (
fields: FormListFieldData[],
operation: FormListOperation,
meta: { errors: React.ReactNode[] },
) => React.ReactNode;
}
const FormList: React.FC<FormListProps> = ({
prefixCls: customizePrefixCls,
children,
...props
}) => {
devWarning(!!props.name, 'Form.List', 'Miss `name` prop.');
const { getPrefixCls } = React.useContext(ConfigContext);
const prefixCls = getPrefixCls('form', customizePrefixCls);
return (
<List {...props}>
{(fields, operation, meta) => {
return (
<FormItemPrefixContext.Provider value={{ prefixCls, status: 'error' }}>
{children(
fields.map(field => ({ ...field, fieldKey: field.key })),
operation,
{
errors: meta.errors,
},
)}
</FormItemPrefixContext.Provider>
);
}}
</List>
);
};
export default FormList;