Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: formItem的required的星号支持从rules中自动判断是否显示(#2637) #2640

Merged
merged 6 commits into from
Nov 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion packages/nutui-taro-demo/src/dentry/pages/form/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,15 @@
:model-value="formData"
:rules="{
name: [
{ required: true, message: '请填写姓名' },
{
message: 'Name should be at least two characters',
validator: nameLengthValidator
}
]
}"
>
<nut-form-item label="姓名" prop="name" required :rules="[{ required: true, message: '请填写姓名' }]">
<nut-form-item label="姓名" prop="name">
<nut-input
v-model="formData.name"
class="nut-input-text"
Expand Down
6 changes: 4 additions & 2 deletions src/packages/__VUE/form/__tests__/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -190,8 +190,8 @@ describe('Form', () => {
'nut-form-item': FormItem
},
template: `
<nut-form :model-value="formData" ref="ruleForm">
<nut-form-item label="姓名" prop="name" required :rules="[{ required: true, message: '请填写姓名' }]">
<nut-form :model-value="formData" ref="ruleForm" :rules="{name:[{ required: true, message: '请填写姓名' }]}">
<nut-form-item label="姓名" prop="name">
<input
class="nut-input-text"
v-model="formData.name"
Expand Down Expand Up @@ -231,6 +231,8 @@ describe('Form', () => {
}
});

const form1 = wrapper.find('.nut-cell__title');
expect(form1.classes()).toContain('required');
// .nut-input-text
const formitem: DOMWrapper<Element> = wrapper.find('.nut-input-text');
expect(formitem.exists()).toBe(true);
Expand Down
8 changes: 2 additions & 6 deletions src/packages/__VUE/form/demo.vue
Original file line number Diff line number Diff line change
Expand Up @@ -67,19 +67,15 @@
:model-value="formData"
:rules="{
name: [
{ required: true, message: translate('nameTip') },
{
message: '名称两个字以上',
validator: nameLengthValidator
}
]
}"
>
<nut-form-item
:label="translate('name')"
prop="name"
required
:rules="[{ required: true, message: translate('nameTip') }]"
>
<nut-form-item :label="translate('name')" prop="name">
<nut-input
v-model="formData.name"
class="nut-input-text"
Expand Down
3 changes: 2 additions & 1 deletion src/packages/__VUE/form/doc.en-US.md
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ const dynamicForm = {
:model-value="formData"
:rules="{
name: [
{ required: true, message: 'Please enter your name' },
{
message: 'Name should be at least two characters',
validator: nameLengthValidator
Expand All @@ -152,7 +153,7 @@ const dynamicForm = {
}"
ref="ruleForm"
>
<nut-form-item label="Name" prop="name" required :rules="[{ required: true, message: 'Please enter your name' }]">
<nut-form-item label="Name" prop="name">
<nut-input
class="nut-input-text"
@blur="customBlurValidate('name')"
Expand Down
3 changes: 2 additions & 1 deletion src/packages/__VUE/form/doc.md
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ const dynamicForm = {
:model-value="formData"
:rules="{
name: [
{ required: true, message: '请填写姓名' },
{
message: 'name 至少两个字符',
validator: nameLengthValidator
Expand All @@ -147,7 +148,7 @@ const dynamicForm = {
}"
ref="ruleForm"
>
<nut-form-item label="姓名" prop="name" required :rules="[{ required: true, message: '请填写姓名' }]">
<nut-form-item label="姓名" prop="name">
<nut-input
class="nut-input-text"
@blur="customBlurValidate('name')"
Expand Down
3 changes: 2 additions & 1 deletion src/packages/__VUE/form/doc.taro.md
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ const dynamicForm = {
:model-value="formData"
:rules="{
name: [
{ required: true, message: '请填写姓名' },
{
message: 'name 至少两个字符',
validator: nameLengthValidator
Expand All @@ -147,7 +148,7 @@ const dynamicForm = {
}"
ref="ruleForm"
>
<nut-form-item label="姓名" prop="name" required :rules="[{ required: true, message: '请填写姓名' }]">
<nut-form-item label="姓名" prop="name">
<nut-input
class="nut-input-text"
@blur="customBlurValidate('name')"
Expand Down
16 changes: 13 additions & 3 deletions src/packages/__VUE/formitem/index.taro.vue
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
v-if="label || getSlots('label')"
class="nut-cell__title nut-form-item__label"
:style="labelStyle"
:class="{ required: required }"
:class="{ required: isRequired }"
>
<slot name="label">{{ label }}</slot>
</view>
Expand Down Expand Up @@ -92,7 +92,17 @@ export default create({
return { parent };
}
};
useParent();
const { parent: parentObj } = useParent();
const isRequired = computed(() => {
const rules = parentObj.props?.rules;
let formRequired = false;
for (const key in rules) {
if (Object.prototype.hasOwnProperty.call(rules, key) && key === props.prop && Array.isArray(rules[key])) {
formRequired = rules[key].some((rule: FormItemRule) => rule.required);
}
}
return props.required || props.rules.some((rule) => rule.required) || formRequired;
});

const parent = inject('formErrorTip') as any;
provide('form', {
Expand All @@ -116,7 +126,7 @@ export default create({
} as CSSProperties;
});
const getSlots = (name: string) => slots[name];
return { parent, labelStyle, bodyStyle, errorMessageStyle, getSlots };
return { parent, labelStyle, bodyStyle, errorMessageStyle, getSlots, isRequired };
}
});
</script>
16 changes: 13 additions & 3 deletions src/packages/__VUE/formitem/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
v-if="label || getSlots('label')"
class="nut-cell__title nut-form-item__label"
:style="labelStyle"
:class="{ required: required }"
:class="{ required: isRequired }"
>
<slot name="label">{{ label }}</slot>
</view>
Expand Down Expand Up @@ -92,7 +92,17 @@ export default create({
return { parent };
}
};
useParent();
const { parent: parentObj } = useParent();
const isRequired = computed(() => {
const rules = parentObj.props?.rules;
let formRequired = false;
for (const key in rules) {
if (Object.prototype.hasOwnProperty.call(rules, key) && key === props.prop && Array.isArray(rules[key])) {
formRequired = rules[key].some((rule: FormItemRule) => rule.required);
}
}
return props.required || props.rules.some((rule) => rule.required) || formRequired;
});

const parent = inject('formErrorTip') as any;
provide('form', {
Expand All @@ -116,7 +126,7 @@ export default create({
} as CSSProperties;
});
const getSlots = (name: string) => slots[name];
return { parent, labelStyle, bodyStyle, errorMessageStyle, getSlots };
return { parent, labelStyle, bodyStyle, errorMessageStyle, getSlots, isRequired };
}
});
</script>