forked from bootstrap-vue-next/bootstrap-vue-next
/
BFormCheckboxGroup.vue
165 lines (153 loc) · 4.28 KB
/
BFormCheckboxGroup.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
<template>
<div
v-bind="computedAttrs"
:id="computedId"
ref="element"
role="group"
:class="computedClasses"
class="bv-no-focus-ring"
tabindex="-1"
>
<slot name="first" />
<BFormCheckbox v-for="item in normalizeOptions" :key="item.self" v-bind="item.props">
<!-- eslint-disable-next-line vue/no-v-html -->
<span v-if="!!item.html" v-html="item.html" />
<template v-else>
{{ item.text }}
</template>
</BFormCheckbox>
<slot />
</div>
</template>
<script setup lang="ts">
import {computed, provide, ref, toRef} from 'vue'
import BFormCheckbox from './BFormCheckbox.vue'
import type {AriaInvalid, ButtonVariant, CheckboxOptionRaw, CheckboxValue, Size} from '../../types'
import {getGroupAttr, getGroupClasses, useId} from '../../composables'
import {checkboxGroupKey} from '../../utils'
import {useFocus, useVModel} from '@vueuse/core'
const props = withDefaults(
defineProps<{
ariaInvalid?: AriaInvalid
autofocus?: boolean
buttonVariant?: ButtonVariant | null
buttons?: boolean
disabled?: boolean
disabledField?: string
form?: string
htmlField?: string
id?: string
modelValue?: readonly CheckboxValue[]
name?: string
options?: readonly CheckboxOptionRaw[]
plain?: boolean
required?: boolean
reverse?: boolean
size?: Size
stacked?: boolean
state?: boolean | null
switches?: boolean
textField?: string
validated?: boolean
valueField?: string
}>(),
{
ariaInvalid: undefined,
autofocus: false,
buttonVariant: 'secondary',
buttons: false,
disabled: false,
disabledField: 'disabled',
form: undefined,
htmlField: 'html',
id: undefined,
modelValue: () => [],
name: undefined,
options: () => [],
plain: false,
required: false,
reverse: false,
size: 'md',
stacked: false,
state: null,
switches: false,
textField: 'text',
validated: false,
valueField: 'value',
}
)
const emit = defineEmits<{
'update:modelValue': [value: CheckboxValue[]]
}>()
defineSlots<{
// eslint-disable-next-line @typescript-eslint/no-explicit-any
default?: (props: Record<string, never>) => any
// eslint-disable-next-line @typescript-eslint/no-explicit-any
first?: (props: Record<string, never>) => any
}>()
const modelValue = useVModel(props, 'modelValue', emit, {passive: true})
const computedId = useId(() => props.id, 'checkbox')
const computedName = useId(() => props.name, 'checkbox')
const element = ref<HTMLElement | null>(null)
const {focused} = useFocus(element, {
initialValue: props.autofocus,
})
provide(checkboxGroupKey, {
modelValue,
switch: toRef(() => props.switches),
buttonVariant: toRef(() => props.buttonVariant),
form: toRef(() => props.form),
name: computedName,
state: toRef(() => props.state),
plain: toRef(() => props.plain),
size: toRef(() => props.size),
inline: toRef(() => !props.stacked),
reverse: toRef(() => props.reverse),
required: toRef(() => props.required),
buttons: toRef(() => props.buttons),
disabled: toRef(() => props.disabled),
})
const normalizeOptions = computed(() =>
props.options.map((el, ind) =>
typeof el === 'string' || typeof el === 'number'
? {
props: {
value: el,
disabled: props.disabled,
},
text: el.toString(),
html: undefined,
self: Symbol(`checkboxGroupOptionItem${ind}`),
}
: {
props: {
value: el[props.valueField] as string | number | undefined,
disabled: el[props.disabledField] as boolean | undefined,
...(el.props ? el.props : {}),
},
text: el[props.textField] as string | undefined,
html: el[props.htmlField] as string | undefined,
self: Symbol(`checkboxGroupOptionItem${ind}`),
}
)
)
const classesObject = computed(() => ({
required: props.required,
ariaInvalid: props.ariaInvalid,
state: props.state,
validated: props.validated,
buttons: props.buttons,
stacked: props.stacked,
size: props.size,
}))
const computedAttrs = getGroupAttr(classesObject)
const computedClasses = getGroupClasses(classesObject)
defineExpose({
blur: () => {
focused.value = false
},
focus: () => {
focused.value = true
},
})
</script>