(component): [tagInput]modelValue 添加 tagData[] 类型

This commit is contained in:
sight 2022-09-26 12:18:49 +08:00
parent 4559a22dfc
commit 4e9604377b
4 changed files with 46 additions and 16 deletions

View File

@ -3,6 +3,9 @@
.layui-tag-input{ .layui-tag-input{
width: 220px; width: 220px;
&-clear{
visibility: unset;
}
} }
} }

View File

@ -53,6 +53,7 @@
padding-right: 10px; padding-right: 10px;
color: rgba(0, 0, 0, 0.45); color: rgba(0, 0, 0, 0.45);
cursor: pointer; cursor: pointer;
visibility: hidden;
} }
&-clear:hover { &-clear:hover {
@ -96,8 +97,6 @@
} }
} }
.set-size(@size) { .set-size(@size) {
@height: ~'tag-input-@{size}'; @height: ~'tag-input-@{size}';
@tag-margin-top: ~'tag-margin-top-@{size}'; @tag-margin-top: ~'tag-margin-top-@{size}';
@ -141,6 +140,6 @@
.layui-tag-input:focus-within { .layui-tag-input:focus-within {
border-color: #d2d2d2 !important; border-color: #d2d2d2 !important;
.layui-tag-input-clear{ .layui-tag-input-clear{
display: flex; visibility: visible;
} }
} }

View File

@ -16,7 +16,12 @@ import {
reactive, reactive,
nextTick, nextTick,
} from "vue"; } from "vue";
import { reactiveOmit, useResizeObserver, useVModel } from "@vueuse/core"; import {
isObject,
reactiveOmit,
useResizeObserver,
useVModel,
} from "@vueuse/core";
import { LayIcon } from "@layui/icons-vue"; import { LayIcon } from "@layui/icons-vue";
export interface TagData { export interface TagData {
@ -63,17 +68,20 @@ const tagData = useVModel(props, "modelValue", emit, {
const _tagProps = reactive(props.tagProps ?? {}); const _tagProps = reactive(props.tagProps ?? {});
const tagProps = reactiveOmit(_tagProps, "closable", "size", "disabled"); const tagProps = reactiveOmit(_tagProps, "closable", "size", "disabled");
const normalizedTags = computed(() => normalizedTagData(tagData.value ?? []));
const computedTagData = computed(() => { const computedTagData = computed(() => {
if (!tagData.value) return; if (!normalizedTags.value) return;
return props.minCollapsedNum return props.minCollapsedNum
? tagData.value?.slice(0, props.minCollapsedNum) ? normalizedTags.value?.slice(0, props.minCollapsedNum)
: tagData.value; : normalizedTags.value;
}); });
const collapsedTagData = computed(() => { const collapsedTagData = computed(() => {
if (!tagData.value) return; if (!normalizedTags.value) return;
return props.minCollapsedNum && tagData.value?.length > props.minCollapsedNum return props.minCollapsedNum &&
? tagData.value?.slice(props.minCollapsedNum) normalizedTags.value?.length > props.minCollapsedNum
? normalizedTags.value?.slice(props.minCollapsedNum)
: []; : [];
}); });
@ -161,6 +169,16 @@ const cls = computed(() => [
}, },
]); ]);
const normalizedTagData = (value: Array<string | number | TagData>) =>
value.map((item) => {
if (isObject(item)) return item;
return {
value: item,
label: String(item),
closable: true,
};
});
useResizeObserver(mirrorRefEl, () => { useResizeObserver(mirrorRefEl, () => {
handleResize(); handleResize();
}); });
@ -206,11 +224,11 @@ defineExpose({
> >
<LayTag <LayTag
v-bind="tagProps" v-bind="tagProps"
:closable="!readonly && !disabled" :closable="!readonly && !disabled && item.closable"
:size="size" :size="size"
@close="handleClose(index)" @close="handleClose(index)"
> >
{{ item }} {{ item.label }}
</LayTag> </LayTag>
</template> </template>
<template v-if="computedTagData?.length != tagData?.length"> <template v-if="computedTagData?.length != tagData?.length">
@ -229,11 +247,11 @@ defineExpose({
v-for="(item, index) of collapsedTagData" v-for="(item, index) of collapsedTagData"
:key="`${item}-${index}`" :key="`${item}-${index}`"
v-bind="tagProps" v-bind="tagProps"
:closable="!readonly && !disabled" :closable="!readonly && !disabled && item.closable"
:size="size" :size="size"
@close="handleClose(index + (minCollapsedNum ?? 0))" @close="handleClose(index + (minCollapsedNum ?? 0))"
> >
{{ item }} {{ item.label }}
</LayTag> </LayTag>
</div> </div>
</template> </template>

View File

@ -21,7 +21,7 @@ import { ref,watch } from 'vue'
export default { export default {
setup() { setup() {
const data1 = ref(['Vue']); const data1 = ref([{value:1,label:'Vue',closable: true}]);
const inputValue1 = ref(""); const inputValue1 = ref("");
return { return {
@ -216,7 +216,7 @@ export default {
| 属性 | 描述 | 类型 | 默认值 | 可选值 | | 属性 | 描述 | 类型 | 默认值 | 可选值 |
| ----------- | -------- | ------ | ------ | ------ | | ----------- | -------- | ------ | ------ | ------ |
| modelValue | 绑定值 | `string` `number` | -| -| | modelValue | 绑定值 | `string[]` `number[]` `TagData[]`| -| -|
| size | 输入框大小 |`string` | `md` | `lg` `md` `sm` `xs`| | size | 输入框大小 |`string` | `md` | `lg` `md` `sm` `xs`|
| inputValue | 输入框的值 | `string` | -| - | | inputValue | 输入框的值 | `string` | -| - |
| placeholder | 占位符 | `string` | - | - | | placeholder | 占位符 | `string` | - | - |
@ -231,6 +231,16 @@ export default {
::: :::
:::title TagInput 方法
:::
:::table
| 名称 | 描述 | 参数 |
|------ |----------|-----------|
| focus | 获取焦点 | - |
| blur |失去焦点 | - |
:::
:::title TagInput 插槽 :::title TagInput 插槽
::: :::