(component): 新增formItem 组件 新增label-width属性,用于控制宽度

This commit is contained in:
0o张不歪o0 2022-07-13 14:01:23 +08:00
parent 831274035c
commit 5c89ea0aa4
3 changed files with 25 additions and 22 deletions

View File

@ -33,6 +33,7 @@ export interface LayFormItemProps {
mode?: string; mode?: string;
label?: string; label?: string;
labelPosition?: string; labelPosition?: string;
labelWidth?: string | number;
errorMessage?: string; errorMessage?: string;
rules?: Rule; rules?: Rule;
required?: boolean; required?: boolean;
@ -41,6 +42,7 @@ export interface LayFormItemProps {
const props = withDefaults(defineProps<LayFormItemProps>(), { const props = withDefaults(defineProps<LayFormItemProps>(), {
mode: "block", mode: "block",
labelPosition: "right", labelPosition: "right",
labelWidth: 95
}); });
const layForm = inject("LayForm", {} as LayFormContext); const layForm = inject("LayForm", {} as LayFormContext);
@ -162,41 +164,40 @@ onMounted(() => {
); );
} }
}); });
const getMarginLeft = computed(() => {
if (props.mode === 'block') {
let labelWidth = typeof props.labelWidth === "string" ? parseFloat(props.labelWidth) : props.labelWidth;
labelWidth+=15;
return{
marginLeft:labelWidth+'px'
}
}
})
</script> </script>
<template> <template>
<div <div class="layui-form-item" :class="[`layui-form-item-${labelPosition}`]" ref="formItemRef">
class="layui-form-item" <label class="layui-form-label" :style="{ width: labelWidth + 'px' }">
:class="[`layui-form-item-${labelPosition}`]" <span v-if="props.prop && isRequired" :class="
ref="formItemRef" ['layui-required', 'layui-icon'].concat(layForm.requiredIcons ?? '')
> ">
<label class="layui-form-label">
<span
v-if="props.prop && isRequired"
:class="
['layui-required', 'layui-icon'].concat(layForm.requiredIcons ?? '')
"
>
<slot name="required" :props="{ ...props, model: layForm.model }">{{ <slot name="required" :props="{ ...props, model: layForm.model }">{{
layForm.requiredIcons ? "" : "*" layForm.requiredIcons ? "" : "*"
}}</slot> }}</slot>
</span> </span>
<slot name="label" :props="{ ...props, model: layForm.model }"> <slot name="label" :props="{ ...props, model: layForm.model }">
{{ label }} {{ label }}
</slot> </slot>
</label> </label>
<div :class="[mode ? 'layui-input-' + mode : '']"> <div :class="[mode ? 'layui-input-' + mode : '']" :style="getMarginLeft">
<div ref="slotParent"> <div ref="slotParent">
<slot :props="{ ...props, model: layForm.model }"></slot> <slot :props="{ ...props, model: layForm.model }"></slot>
</div> </div>
<span <span v-if="errorStatus" :class="[
v-if="errorStatus" 'layui-error-message',
:class="[ { 'layui-error-message-anim': errorStatus },
'layui-error-message', ]">{{ errorMsg }}</span>
{ 'layui-error-message-anim': errorStatus },
]"
>{{ errorMsg }}</span
>
</div> </div>
</div> </div>
</template> </template>

View File

@ -532,6 +532,7 @@ export default {
| prop | 在表单绑定值(`model`)中字段`key` | `string` | - | - | | prop | 在表单绑定值(`model`)中字段`key` | `string` | - | - |
| label | 子项前边描述值,**尽量填写**,中文校验错误需要用到 | `string` | - | - | | label | 子项前边描述值,**尽量填写**,中文校验错误需要用到 | `string` | - | - |
| label-position| 子项前边描述值的位置 | `string` | `left` `top` `right` |`right` | | label-position| 子项前边描述值的位置 | `string` | `left` `top` `right` |`right` |
| label-width | 子项前边描述值的宽度 | `string` `number` | - | `95` |
| required | 是否必填 | `boolean` | `true` `false` | `false` | | required | 是否必填 | `boolean` | `true` `false` | `false` |
| rules | 表单校验规则; <br>可查看[async-validator](https://github.com/yiminghe/async-validator) | `object` | - | - | | rules | 表单校验规则; <br>可查看[async-validator](https://github.com/yiminghe/async-validator) | `object` | - | - |
| error-message | 表单校验失败固定提示语 | `string` |`block` `inline`| `block` | | error-message | 表单校验失败固定提示语 | `string` |`block` `inline`| `block` |

View File

@ -22,6 +22,7 @@
<li>[修复] datePicker 组件 启用simple属性后无法弹出问题。 by @SmallWai</li> <li>[修复] datePicker 组件 启用simple属性后无法弹出问题。 by @SmallWai</li>
<li>[修复] datePicker 组件 上一次更新带来的Bug。 by @SmallWai</li> <li>[修复] datePicker 组件 上一次更新带来的Bug。 by @SmallWai</li>
<li>[优化] radio 组件 动画效果。 by @SmallWai</li> <li>[优化] radio 组件 动画效果。 by @SmallWai</li>
<li>[优化] formItem 组件 新增label-width属性用于控制宽度 by @SmallWai</li>
</ul> </ul>
</li> </li>
</ul> </ul>