From 4adc4df3e6dc6ba63c562290c70c2f6db32c97c2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=8F=B6=E6=9E=AB?= Date: Wed, 1 Jan 2020 20:07:10 +0800 Subject: [PATCH] fix: form item array children interface (#20577) * Update Form.tsx * Update Form.tsx * fix: fix formItem array children interface * fix: array * feat: test --- components/form/FormItem.tsx | 12 +++++++++--- components/form/__tests__/index.test.js | 13 +++++++++++++ 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/components/form/FormItem.tsx b/components/form/FormItem.tsx index d4e90000ba..bc090195c4 100644 --- a/components/form/FormItem.tsx +++ b/components/form/FormItem.tsx @@ -19,12 +19,15 @@ export type ValidateStatus = typeof ValidateStatuses[number]; type RenderChildren = (form: FormInstance) => React.ReactElement; type RcFieldProps = Omit; -export interface FormItemProps extends FormItemLabelProps, FormItemInputProps, RcFieldProps { +export interface FormItemProps + extends FormItemLabelProps, + FormItemInputProps, + Omit { prefixCls?: string; noStyle?: boolean; style?: React.CSSProperties; className?: string; - children: React.ReactElement | RenderChildren; + children: React.ReactElement | RenderChildren | React.ReactElement[]; id?: string; hasFeedback?: boolean; validateStatus?: ValidateStatus; @@ -175,7 +178,10 @@ const FormItem: React.FC = (props: FormItemProps) => { }; let childNode; - if (typeof children === 'function' && (!shouldUpdate || !!name)) { + if (Array.isArray(children) && !!name) { + 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`.'); } else if (!mergedName.length && !shouldUpdate && !dependencies) { childNode = children; diff --git a/components/form/__tests__/index.test.js b/components/form/__tests__/index.test.js index cfa2cc1221..fc998f4700 100644 --- a/components/form/__tests__/index.test.js +++ b/components/form/__tests__/index.test.js @@ -137,6 +137,19 @@ describe('Form', () => { 'Warning: [antd: Form.Item] `children` of render props only work with `shouldUpdate`.', ); }); + it('children is array has name props', () => { + mount( +
+ +
one
+
two
+
+
, + ); + expect(errorSpy).toHaveBeenCalledWith( + 'Warning: [antd: Form.Item] `children` is array of render props cannot have `name`.', + ); + }); describe('scrollToField', () => { function test(name, genForm) {