mirror of
https://gitee.com/ant-design/ant-design.git
synced 2024-12-15 17:31:25 +08:00
338ec7dad7
* pref: better code style for production * refactor `devWarning` * don't use `useEffect` only wrap `devWarning` * chore: add 'noop' to coverage * chore: add test cases for devWarning * chore: add test case * chore: update test cases for devWarning * chore: restore test script command * fix: remove 'throw new Error' * should not use `throw` for browser * chore: update test case for AutoComplete * perf: add prefix for `devWarning` * update RegExp for UMD * add prefix for ES and CJS * chore: better code style * perf: * upgrade antd-tools * remove `injectWarningCondition` * rename `devWarning` to `warning` * chore: better code style * chore: better code style * chore: restore hasValidName
67 lines
1.7 KiB
TypeScript
67 lines
1.7 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;
|
|
}
|
|
|
|
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;
|