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';
|
2020-05-14 15:57:04 +08:00
|
|
|
import devWarning from '../_util/devWarning';
|
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-03-10 15:46:13 +08:00
|
|
|
add: (defaultValue?: StoreValue) => 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 {
|
2019-07-03 20:14:39 +08:00
|
|
|
name: string | number | (string | number)[];
|
2020-07-28 13:04:18 +08:00
|
|
|
children: (fields: FormListFieldData[], operation: FormListOperation) => React.ReactNode;
|
2019-07-03 20:14:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
const FormList: React.FC<FormListProps> = ({ 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
|
|
|
|
|
|
|
return (
|
|
|
|
<List {...props}>
|
|
|
|
{(fields, operation) => {
|
2020-05-14 15:57:04 +08:00
|
|
|
return children(
|
|
|
|
fields.map(field => ({ ...field, fieldKey: field.key })),
|
|
|
|
operation,
|
|
|
|
);
|
2019-07-03 20:14:39 +08:00
|
|
|
}}
|
|
|
|
</List>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default FormList;
|