Skip to content

Commit

Permalink
feat: formItem的required的星号支持从rules中自动判断是否显示(jd-opensource#2637)
Browse files Browse the repository at this point in the history
  • Loading branch information
yi-boide committed Nov 6, 2023
1 parent b14edf1 commit 367164f
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 12 deletions.
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
18 changes: 15 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,19 @@ export default create({
return { parent };
}
};
useParent();
const { parent: parentObj } = useParent();
const isRequired = computed(() => {
const rules = parentObj.props?.rules;
let formRequired = false;
if (Object.keys(rules).length !== 0) {
Object.keys(rules).forEach((key) => {
if (key === props.prop) {
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 +128,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>
18 changes: 15 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,19 @@ export default create({
return { parent };
}
};
useParent();
const { parent: parentObj } = useParent();
const isRequired = computed(() => {
const rules = parentObj.props?.rules;
let formRequired = false;
if (Object.keys(rules).length !== 0) {
Object.keys(rules).forEach((key) => {
if (key === props.prop) {
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 +128,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>

0 comments on commit 367164f

Please sign in to comment.