-
-
Notifications
You must be signed in to change notification settings - Fork 173
/
HstButtonGroup.vue
64 lines (58 loc) · 1.58 KB
/
HstButtonGroup.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
<script lang="ts">
export default {
name: 'HstButtonGroup',
}
</script>
<script setup lang="ts">
import { computed, ComputedRef } from 'vue'
import HstWrapper from '../HstWrapper.vue'
import { HstControlOption } from '../../types'
import HstButton from './HstButton.vue'
const props = defineProps<{
title?: string
modelValue: string
options: HstControlOption[]
}>()
const formattedOptions: ComputedRef<Record<string, string>> = computed(() => {
if (Array.isArray(props.options)) {
return Object.fromEntries(props.options.map((value: string | HstControlOption) => {
if (typeof value === 'string') {
return [value, value]
} else {
return [value.value, value.label]
}
}))
}
return props.options
})
const emit = defineEmits<{
(e: 'update:modelValue', value: string): void
}>()
function selectOption (value: string) {
emit('update:modelValue', value)
}
</script>
<template>
<HstWrapper
tag="div"
role="group"
:title="title"
class="htw-flex-nowrap htw-items-center"
>
<div class="htw-flex htw-gap-px htw-border htw-border-solid htw-border-black/25 dark:htw-border-white/25 htw-rounded-sm htw-p-px">
<HstButton
v-for="( label, value ) in formattedOptions"
:key="value"
class="htw-px-1 htw-h-[22px] htw-flex-1 !htw-rounded-[3px]"
:color="value === modelValue ? 'primary' : 'flat'"
:rounded="false"
@click="selectOption(value)"
>
{{ label }}
</HstButton>
</div>
<template #actions>
<slot name="actions" />
</template>
</HstWrapper>
</template>