Skip to content

Commit

Permalink
fix(Form): useLabelForErrorMessage of Form.Item have a higher priorit…
Browse files Browse the repository at this point in the history
…y than Form (#4088)
  • Loading branch information
siyou authored Sep 7, 2022
1 parent cd8e782 commit b0aec62
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/form/item.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,8 @@ export default class Item extends React.Component {

const newLabel = label.replace(':', '').replace(':', '');

const labelForErrorMessage = useLabelForErrorMessage || this.context._formLabelForErrorMessage;
const labelForErrorMessage =
useLabelForErrorMessage !== undefined ? useLabelForErrorMessage : this.context._formLabelForErrorMessage;
if (labelForErrorMessage && newLabel) {
return newLabel;
}
Expand Down
40 changes: 40 additions & 0 deletions test/form/validate-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,46 @@ describe('Submit', () => {
.text() === '姓名 是必填字段'
);
});
it('the useLabelForErrorMessage of FormItem should have a higher priority', () => {
const wrapper = mount(
<Form useLabelForErrorMessage>
<FormItem useLabelForErrorMessage={false} required label="姓名" >
<Input name="first" />
</FormItem>
</Form>
);

wrapper
.find('input#first')
.simulate('change', { target: { value: '' } });
wrapper.update();
assert(
wrapper
.find('.next-form-item-help')
.first()
.text() === 'first 是必填字段'
);
});
it('the useLabelForErrorMessage of FormItem should have a higher priority', () => {
const wrapper = mount(
<Form useLabelForErrorMessage={false}>
<FormItem useLabelForErrorMessage required label="姓名" >
<Input name="first" />
</FormItem>
</Form>
);

wrapper
.find('input#first')
.simulate('change', { target: { value: '' } });
wrapper.update();
assert(
wrapper
.find('.next-form-item-help')
.first()
.text() === '姓名 是必填字段'
);
});
it('validate errorMessageName', () => {
const wrapper = mount(
<Form useLabelForErrorMessage>
Expand Down

0 comments on commit b0aec62

Please sign in to comment.