ant-design/components/form/FormList.tsx
dingkang 52b049d170
test: replace Table part test with test lib (#35989)
* test: replace Table part test with test lib

* test: replace table test with test library

* test: update snapshot

* test: replace last mount

* test: fix 18 testing

Co-authored-by: zombiej <smith3816@gmail.com>
2022-06-13 11:26:16 +08:00

69 lines
1.8 KiB
TypeScript

import * as React from 'react';
import { List } from 'rc-field-form';
import type { ValidatorRule, StoreValue } from 'rc-field-form/lib/interface';
import warning from '../_util/warning';
import { ConfigContext } from '../config-provider';
import { FormItemPrefixContext } from './context';
export interface FormListFieldData {
name: number;
key: number;
/** @deprecated No need anymore Use key instead */
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
}) => {
warning(!!props.name, 'Form.List', 'Miss `name` prop.');
const { getPrefixCls } = React.useContext(ConfigContext);
const prefixCls = getPrefixCls('form', customizePrefixCls);
const contextValue = React.useMemo(
() => ({
prefixCls,
status: 'error' as const,
}),
[prefixCls],
);
return (
<List {...props}>
{(fields, operation, meta) => (
<FormItemPrefixContext.Provider value={contextValue}>
{children(
fields.map(field => ({ ...field, fieldKey: field.key })),
operation,
{
errors: meta.errors,
warnings: meta.warnings,
},
)}
</FormItemPrefixContext.Provider>
)}
</List>
);
};
export default FormList;