Browse Source

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
pull/31202/head
wangao 3 years ago
committed by GitHub
parent
commit
7a10ed1e12
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 2
      components/form/FormItem.tsx
  2. 17
      components/form/__tests__/index.test.js

2
components/form/FormItem.tsx

@ -265,6 +265,8 @@ function FormItem<Values = any>(props: FormItemProps<Values>): React.ReactElemen
let variables: Record<string, string> = {};
if (typeof label === 'string') {
variables.label = label;
} else if (name) {
variables.label = String(name);
}
if (messageVariables) {
variables = { ...variables, ...messageVariables };

17
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
<Form validateMessages={{ required: '${label} is good!' }}>
<Form.Item name="Bamboo" rules={[{ required: true }]}>
<input />
</Form.Item>
</Form>,
);
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

Loading…
Cancel
Save