From 7a10ed1e12eff7721eab92d62793de250612894d Mon Sep 17 00:00:00 2001 From: wangao Date: Wed, 30 Jun 2021 11:32:34 +0800 Subject: [PATCH] fix(FormItem): add name as default variables.label value (#30179) * fix(FormItem): add name as default variables.label value * test(Form.Item): add name as default test --- components/form/FormItem.tsx | 2 ++ components/form/__tests__/index.test.js | 17 +++++++++++++++++ 2 files changed, 19 insertions(+) diff --git a/components/form/FormItem.tsx b/components/form/FormItem.tsx index 74f5fb0667..33092b50e8 100644 --- a/components/form/FormItem.tsx +++ b/components/form/FormItem.tsx @@ -265,6 +265,8 @@ function FormItem(props: FormItemProps): React.ReactElemen let variables: Record = {}; if (typeof label === 'string') { variables.label = label; + } else if (name) { + variables.label = String(name); } if (messageVariables) { variables = { ...variables, ...messageVariables }; diff --git a/components/form/__tests__/index.test.js b/components/form/__tests__/index.test.js index 684219c65a..b80716ae51 100644 --- a/components/form/__tests__/index.test.js +++ b/components/form/__tests__/index.test.js @@ -564,6 +564,23 @@ describe('Form', () => { expect(wrapper.find('.ant-form-item-explain').first().text()).toEqual('Bamboo is good!'); }); + it('`name` support template when label is not provided', async () => { + const wrapper = mount( + // eslint-disable-next-line no-template-curly-in-string +
+ + + +
, + ); + + wrapper.find('form').simulate('submit'); + await sleep(100); + wrapper.update(); + await sleep(100); + expect(wrapper.find('.ant-form-item-explain').first().text()).toEqual('Bamboo is good!'); + }); + it('`messageVariables` support validate', async () => { const wrapper = mount( // eslint-disable-next-line no-template-curly-in-string