-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathslds-checkbox-group.vue
109 lines (87 loc) · 2.96 KB
/
slds-checkbox-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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
<template>
<slds-form-element
:errors="errors"
:help="help"
:label="label"
:required="required"
:stacked="stacked"
:tooltip="tooltip"
>
<!-- Tooltip -->
<template v-if="$slots.tooltip" #tooltip>
<slot name="tooltip"/>
</template>
<!-- Default slot -->
<template #default>
<slot>
<slds-checkbox-group-option
v-for="option in options"
:key="option.value"
:checked="modelValue.includes(option.value)"
:label="option.label"
:disabled="option.disabled || disabled"
@click="handleClick($event, option)"
/>
</slot>
</template>
<!-- Inline help -->
<template #help>
<slot name="help"/>
</template>
<!-- Error messages -->
<template #error>
<slot name="error"/>
</template>
</slds-form-element>
</template>
<script lang="ts">
import SldsFormElement from "../slds-form-element/slds-form-element.vue"
import SldsCheckboxGroupOption from "../slds-checkbox-group/slds-checkbox-group-option.vue"
import { defineComponent, type PropType } from "vue"
import { type ValidationError } from "../slds-form-element/validation-error"
import { type CheckboxGroupOption } from "./checkbox-group-option"
import { EVENTS } from "../../constants"
export default defineComponent ({
name: "SldsCheckboxGroup",
components: { SldsCheckboxGroupOption, SldsFormElement },
props: {
disabled: Boolean,
/**
* Array of error objects from vuelidate.
*/
errors: { type: Array as PropType<ValidationError[]>, default: () => [] as ValidationError[] },
/**
* Inline help text.
* When using the help slot this prop is ignored.
*/
help: String,
label: String,
modelValue: { type: Array as PropType<string[]>, default: () => [] },
options: { type: Array as PropType<CheckboxGroupOption[]>, default: () => [] },
required: Boolean,
/**
* Indicates whether the input is stacked among other inputs.
*/
stacked: Boolean,
tooltip: String,
},
methods: {
/**
* Handles the click event on the checkbox group.
* @param event The fired event.
* @param option The clicked option.
*/
handleClick(event: Event, option: CheckboxGroupOption): void {
if (!event || this.disabled || option.disabled) {
event.preventDefault()
return
}
const value = [...this.modelValue]
const index = value.indexOf(option.value)
if (index === -1) value.push(option.value)
else value.splice(index, 1)
this.$emit(EVENTS.UPDATE_MODEL_VALUE, value)
},
},
})
</script>