2019-07-03 20:14:39 +08:00
|
|
|
import * as React from 'react';
|
|
|
|
import { List } from 'rc-field-form';
|
2020-10-15 22:31:51 +08:00
|
|
|
import { ValidatorRule, StoreValue } from 'rc-field-form/lib/interface';
|
2020-05-14 15:57:04 +08:00
|
|
|
import devWarning from '../_util/devWarning';
|
2020-09-11 21:27:51 +08:00
|
|
|
import { ConfigContext } from '../config-provider';
|
|
|
|
import { FormItemPrefixContext } from './context';
|
2019-07-03 20:14:39 +08:00
|
|
|
|
2020-07-28 13:04:18 +08:00
|
|
|
export interface FormListFieldData {
|
2019-07-03 20:14:39 +08:00
|
|
|
name: number;
|
|
|
|
key: number;
|
|
|
|
fieldKey: number;
|
|
|
|
}
|
|
|
|
|
2020-07-28 13:04:18 +08:00
|
|
|
export interface FormListOperation {
|
2020-08-12 19:13:44 +08:00
|
|
|
add: (defaultValue?: StoreValue, insertIndex?: number) => void;
|
2020-07-15 22:17:17 +08:00
|
|
|
remove: (index: number | number[]) => void;
|
2019-12-24 20:36:27 +08:00
|
|
|
move: (from: number, to: number) => void;
|
2019-07-03 20:14:39 +08:00
|
|
|
}
|
|
|
|
|
2020-07-28 13:04:18 +08:00
|
|
|
export interface FormListProps {
|
2020-09-11 21:27:51 +08:00
|
|
|
prefixCls?: string;
|
2019-07-03 20:14:39 +08:00
|
|
|
name: string | number | (string | number)[];
|
2020-10-15 22:31:51 +08:00
|
|
|
rules?: ValidatorRule[];
|
2020-09-11 21:27:51 +08:00
|
|
|
children: (
|
|
|
|
fields: FormListFieldData[],
|
|
|
|
operation: FormListOperation,
|
|
|
|
meta: { errors: React.ReactNode[] },
|
|
|
|
) => React.ReactNode;
|
2019-07-03 20:14:39 +08:00
|
|
|
}
|
|
|
|
|
2020-09-11 21:27:51 +08:00
|
|
|
const FormList: React.FC<FormListProps> = ({
|
|
|
|
prefixCls: customizePrefixCls,
|
|
|
|
children,
|
|
|
|
...props
|
|
|
|
}) => {
|
2020-05-14 15:57:04 +08:00
|
|
|
devWarning(!!props.name, 'Form.List', 'Miss `name` prop.');
|
2019-07-03 20:14:39 +08:00
|
|
|
|
2020-09-11 21:27:51 +08:00
|
|
|
const { getPrefixCls } = React.useContext(ConfigContext);
|
|
|
|
const prefixCls = getPrefixCls('form', customizePrefixCls);
|
|
|
|
|
2019-07-03 20:14:39 +08:00
|
|
|
return (
|
|
|
|
<List {...props}>
|
2020-09-11 21:27:51 +08:00
|
|
|
{(fields, operation, meta) => {
|
|
|
|
return (
|
|
|
|
<FormItemPrefixContext.Provider value={{ prefixCls, status: 'error' }}>
|
|
|
|
{children(
|
|
|
|
fields.map(field => ({ ...field, fieldKey: field.key })),
|
|
|
|
operation,
|
|
|
|
{
|
|
|
|
errors: meta.errors,
|
|
|
|
},
|
|
|
|
)}
|
|
|
|
</FormItemPrefixContext.Provider>
|
2020-05-14 15:57:04 +08:00
|
|
|
);
|
2019-07-03 20:14:39 +08:00
|
|
|
}}
|
|
|
|
</List>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default FormList;
|