ant-design/components/form/FormList.tsx
二货机器人 7a3bf8287f
feat: Form rule support warningOnly that not block form submit (#30829)
* chore: bump rc-field-form

* docs: Demo driven

* refactor: Use event instead

* chore: collection logic update

* chore: clean up

* chore: show warning

* chore: warning no need required mark

* feat: warning example

* docs: mix error

* chore: tmp list

* chore: magic code

* chore: fix motion

* chore: fix style

* chore: clean up useless code

* chore: RM useless import

* chore: RM useless file

* test: Update snapshot

* chore: Should reset

* fix: Memo logic

* fix: Form basic test case

* fix: lint

* chore: back of ref

* test: Update snapshot

* test: RM ueseless test case

* chore: RM useless code
2021-06-04 14:44:41 +08:00

61 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[]; warnings: 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) => (
<FormItemPrefixContext.Provider value={{ prefixCls, status: 'error' }}>
{children(
fields.map(field => ({ ...field, fieldKey: field.key })),
operation,
{
errors: meta.errors,
warnings: meta.warnings,
},
)}
</FormItemPrefixContext.Provider>
)}
</List>
);
};
export default FormList;