-
-
Notifications
You must be signed in to change notification settings - Fork 3.6k
/
select-list-item-group.vue
90 lines (81 loc) · 2.31 KB
/
select-list-item-group.vue
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
<template>
<v-list-group
v-show="!item.hidden"
:active="isActive"
:clickable="groupSelectable || item.selectable"
:value="item.value"
@click="onGroupClick(item)"
>
<template #activator>
<v-list-item-icon v-if="multiple === false && allowOther === false && item.icon">
<v-icon :name="item.icon" />
</v-list-item-icon>
<v-list-item-content>
<span v-if="multiple === false || item.selectable === false" class="item-text">{{ item.text }}</span>
<v-checkbox
v-else
:model-value="modelValue || []"
:label="item.text"
:value="item.value"
:disabled="item.disabled"
@update:model-value="$emit('update:modelValue', $event.length > 0 ? $event : null)"
/>
</v-list-item-content>
</template>
<template v-for="(childItem, index) in item.children" :key="index">
<select-list-item-group
v-if="childItem.children"
:item="childItem"
:item-label-font-family="itemLabelFontFamily"
:model-value="modelValue"
:multiple="multiple"
:allow-other="allowOther"
:group-selectable="groupSelectable"
@update:model-value="$emit('update:modelValue', $event)"
/>
<select-list-item
v-else
:model-value="modelValue"
:item="childItem"
:item-label-font-family="itemLabelFontFamily"
:multiple="multiple"
:allow-other="allowOther"
@update:model-value="$emit('update:modelValue', $event)"
/>
</template>
</v-list-group>
</template>
<script setup lang="ts">
import { computed } from 'vue';
import { Option } from './types';
import SelectListItem from './select-list-item.vue';
interface Props {
item: Option;
itemLabelFontFamily?: string;
modelValue?: string | number | (string | number)[] | null;
multiple?: boolean;
allowOther?: boolean;
groupSelectable?: boolean;
}
const props = withDefaults(defineProps<Props>(), {
modelValue: null,
multiple: true,
allowOther: true,
groupSelectable: false,
});
const emit = defineEmits(['update:modelValue']);
const isActive = computed(() => {
if (props.multiple) {
if (!Array.isArray(props.modelValue) || !props.item.value) {
return false;
}
return props.modelValue.includes(props.item.value);
} else {
return props.modelValue === props.item.value;
}
});
function onGroupClick(item: Option) {
if (!props.groupSelectable) return;
emit('update:modelValue', item.value);
}
</script>