From ae88fdeabde38f26371c92d0fa5cfc1fd2de1eea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BA=8C=E8=B4=A7=E6=9C=BA=E5=99=A8=E4=BA=BA?= Date: Wed, 11 Dec 2019 16:08:59 +0800 Subject: [PATCH] feat: Warning invalidate element when using `name`. (#20191) --- components/form/FormItem.tsx | 7 +++++++ components/form/__tests__/index.test.js | 11 +++++++++++ 2 files changed, 18 insertions(+) diff --git a/components/form/FormItem.tsx b/components/form/FormItem.tsx index faed7f3890..5bf5fdddcb 100644 --- a/components/form/FormItem.tsx +++ b/components/form/FormItem.tsx @@ -188,6 +188,13 @@ const FormItem: React.FC = (props: FormItemProps) => { childNode = React.cloneElement(children, childProps); } else if (typeof children === 'function' && shouldUpdate && !name) { childNode = children(context); + } else { + warning( + !mergedName.length, + 'Form.Item', + '`name` is only used for validate React element. If you are using Form.Item as layout display, please remove `name` instead.', + ); + childNode = children; } if (noStyle) { diff --git a/components/form/__tests__/index.test.js b/components/form/__tests__/index.test.js index 83ba7a9b63..22a06dfd08 100644 --- a/components/form/__tests__/index.test.js +++ b/components/form/__tests__/index.test.js @@ -196,4 +196,15 @@ describe('Form', () => { ); expect(wrapper.render()).toMatchSnapshot(); }); + + it('warning when use `name` but children is not validate element', () => { + mount( +
+ text +
, + ); + expect(errorSpy).toHaveBeenCalledWith( + 'Warning: [antd: Form.Item] `name` is only used for validate React element. If you are using Form.Item as layout display, please remove `name` instead.', + ); + }); });