chore: Warning when FormItem use render props and name at same time (#20733)

This commit is contained in:
二货机器人 2020-01-07 19:17:51 +08:00 committed by GitHub
parent 4068f3d190
commit 40d9627554
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 22 additions and 1 deletions

View File

@ -181,7 +181,16 @@ const FormItem: React.FC<FormItemProps> = (props: FormItemProps) => {
warning(false, 'Form.Item', '`children` is array of render props cannot have `name`.');
childNode = children;
} else if (typeof children === 'function' && (!shouldUpdate || !!name)) {
warning(false, 'Form.Item', '`children` of render props only work with `shouldUpdate`.');
warning(
!!shouldUpdate,
'Form.Item',
'`children` of render props only work with `shouldUpdate`.',
);
warning(
!name,
'Form.Item',
"Do not use `name` with `children` of render props since it's not a field.",
);
} else if (!mergedName.length && !shouldUpdate && !dependencies) {
childNode = children;
} else if (React.isValidElement(children)) {

View File

@ -141,6 +141,18 @@ describe('Form', () => {
'Warning: [antd: Form.Item] `children` of render props only work with `shouldUpdate`.',
);
});
it('`name` should not work with render props', () => {
mount(
<Form>
<Form.Item name="test" shouldUpdate>
{() => null}
</Form.Item>
</Form>,
);
expect(errorSpy).toHaveBeenCalledWith(
"Warning: [antd: Form.Item] Do not use `name` with `children` of render props since it's not a field.",
);
});
it('children is array has name props', () => {
mount(
<Form>