Skip to content

Commit

Permalink
feat: Form validateMessages support ${label} (#21835)
Browse files Browse the repository at this point in the history
* update form deps

* update demo

* update test case
  • Loading branch information
zombieJ authored Mar 3, 2020
1 parent 5d63040 commit 661ff79
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 5 deletions.
7 changes: 7 additions & 0 deletions components/form/FormItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ function FormItem(props: FormItemProps): React.ReactElement {
validateStatus,
children,
required,
label,
trigger = 'onChange',
validateTrigger = 'onChange',
...restProps
Expand Down Expand Up @@ -204,9 +205,15 @@ function FormItem(props: FormItemProps): React.ReactElement {
return renderLayout(children as ChildrenNodeType);
}

const variables: Record<string, string> = {};
if (typeof label === 'string') {
variables.label = label;
}

return (
<Field
{...props}
messageVariables={variables}
trigger={trigger}
validateTrigger={validateTrigger}
onReset={() => {
Expand Down
22 changes: 22 additions & 0 deletions components/form/__tests__/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -593,4 +593,26 @@ describe('Form', () => {

expect(errorSpy).not.toHaveBeenCalled();
});

it('`label` support template', async () => {
const wrapper = mount(
// eslint-disable-next-line no-template-curly-in-string
<Form validateMessages={{ required: '${label} is good!' }}>
<Form.Item name="test" label="Bamboo" rules={[{ required: true }]}>
<input />
</Form.Item>
</Form>,
);

wrapper.find('form').simulate('submit');
await delay(50);
wrapper.update();

expect(
wrapper
.find('.ant-form-item-explain')
.first()
.text(),
).toEqual('Bamboo is good!');
});
});
8 changes: 4 additions & 4 deletions components/form/demo/nest-messages.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@ const layout = {
};

const validateMessages = {
required: 'This field is required!',
required: '${label} is required!',
types: {
email: 'Not a validate email!',
number: 'Not a validate number!',
email: '${label} is not validate email!',
number: '${label} is not a validate number!',
},
number: {
range: 'Must be between ${min} and ${max}',
range: '${label} must be between ${min} and ${max}',
},
};

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@
"rc-dialog": "~7.6.0",
"rc-drawer": "~3.1.1",
"rc-dropdown": "~3.0.0",
"rc-field-form": "~1.0.0",
"rc-field-form": "~1.1.0",
"rc-input-number": "~4.5.4",
"rc-mentions": "~1.0.0",
"rc-menu": "~8.0.1",
Expand Down

0 comments on commit 661ff79

Please sign in to comment.