Skip to content

Commit

Permalink
fix(FormItem): add name as default variables.label value (ant-design#…
Browse files Browse the repository at this point in the history
…30179)

* fix(FormItem): add name as default variables.label value

* test(Form.Item): add name as default test
  • Loading branch information
jameslahm authored and yingpengsha committed Jul 2, 2021
1 parent 3a19397 commit 6a7e3cf
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
2 changes: 2 additions & 0 deletions components/form/FormItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 };
Expand Down
17 changes: 17 additions & 0 deletions components/form/__tests__/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 6a7e3cf

Please sign in to comment.