2019-07-03 20:14:39 +08:00
|
|
|
import * as React from 'react';
|
|
|
|
import { List } from 'rc-field-form';
|
2020-03-10 15:46:13 +08:00
|
|
|
import { StoreValue } from 'rc-field-form/lib/interface';
|
2019-08-12 13:22:36 +08:00
|
|
|
import warning from '../_util/warning';
|
2019-07-03 20:14:39 +08:00
|
|
|
|
|
|
|
interface FieldData {
|
|
|
|
name: number;
|
|
|
|
key: number;
|
|
|
|
fieldKey: number;
|
|
|
|
}
|
|
|
|
|
|
|
|
interface Operation {
|
2020-03-10 15:46:13 +08:00
|
|
|
add: (defaultValue?: StoreValue) => void;
|
2019-07-03 20:14:39 +08:00
|
|
|
remove: (index: number) => void;
|
2019-12-24 20:36:27 +08:00
|
|
|
move: (from: number, to: number) => void;
|
2019-07-03 20:14:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
interface FormListProps {
|
|
|
|
name: string | number | (string | number)[];
|
|
|
|
children: (fields: FieldData[], operation: Operation) => React.ReactNode;
|
|
|
|
}
|
|
|
|
|
|
|
|
const FormList: React.FC<FormListProps> = ({ children, ...props }) => {
|
|
|
|
warning(!!props.name, 'Form.List', 'Miss `name` prop.');
|
|
|
|
|
|
|
|
return (
|
|
|
|
<List {...props}>
|
|
|
|
{(fields, operation) => {
|
2020-03-10 15:46:13 +08:00
|
|
|
return children(fields.map(field => ({ ...field, fieldKey: field.key })), operation);
|
2019-07-03 20:14:39 +08:00
|
|
|
}}
|
|
|
|
</List>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default FormList;
|