-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathslds-button-group.vue
119 lines (98 loc) · 2.87 KB
/
slds-button-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
109
110
111
112
113
114
115
116
117
118
<template>
<div class="slds-button-group" role="group">
<slot/>
<div
v-if="showDropdownButton"
v-click-outside="handleClickOutside"
class="slds-dropdown-trigger slds-dropdown-trigger_click slds-is-open slds-button_last"
>
<slds-button-icon
bordered
:disabled="disabled"
icon-name="utility:down"
@click="handleClickButton"
/>
<slds-button-group-dropdown
:is-open="isOpen"
:dropdown-options="dropdownOptions"
@click-option="handleClickOption"
/>
</div>
</div>
</template>
<script lang="ts">
import { defineComponent, PropType } from "vue"
import SldsButtonIcon from "../slds-button-icon/slds-button-icon.vue"
import SldsButtonGroupDropdown from "./slds-button-group-dropdown.vue"
import { vOnClickOutside } from "@vueuse/components"
import { DropdownOption } from "../slds-dropdown/dropdown-option.ts"
export default defineComponent({
name: "SldsButtonGroup",
components: { SldsButtonGroupDropdown, SldsButtonIcon },
directives: {
ClickOutside: vOnClickOutside,
},
props: {
/**
* Indicates whether this component is disabled.
*/
disabled: Boolean,
/**
* Dropdown options.
*/
dropdownOptions: { type: Array as PropType<DropdownOption[]>, default: () => [] as DropdownOption[] },
},
data() {
return {
/**
* Indicates whether the dropdown is open.
*/
isOpen: false,
}
},
computed: {
/**
* Indicates whether the dropdown button is visible.
*/
showDropdownButton(): boolean {
return Boolean(this.dropdownOptions && this.dropdownOptions.length > 0)
},
},
methods: {
/**
* Handles the click event on the button.
*/
handleClickButton(): void {
if (this.isOpen) this.hideDropdown()
else if (!this.disabled) this.showDropdown()
},
/**
* Handles the click event on an option.
* @param option The clicked option.
*/
handleClickOption(option: DropdownOption): void {
if (this.disabled || option.disabled) return
this.$emit(option.value!)
this.hideDropdown()
},
/**
* Handles the click event outside this component.
*/
handleClickOutside(): void {
this.hideDropdown()
},
/**
* Hides the dropdown.
*/
hideDropdown(): void {
this.isOpen = false
},
/**
* Shows the dropdown.
*/
showDropdown(): void {
this.isOpen = true
},
},
})
</script>