Line data Source code
1 : <template>
2 15 : <div
3 : :class="
4 : cn(WidgetInputBaseClass, 'flex w-full min-w-0 items-center gap-1 p-1')
5 : "
6 : >
7 45 : <button
8 : v-for="(option, index) in options"
9 : :key="getOptionValue(option, index)"
10 : :class="
11 : cn(
12 : 'flex h-6 min-w-0 flex-1 items-center justify-center gap-1 truncate rounded-sm px-5 py-[5px] transition-all duration-150 ease-in-out',
13 : 'border-none bg-transparent',
14 : 'text-center text-xs font-normal',
15 : {
16 : 'bg-muted': isSelected(index) && !disabled,
17 : 'hover:bg-muted/50': !isSelected(index) && !disabled,
18 : 'cursor-not-allowed opacity-50': disabled,
19 : 'cursor-pointer': !disabled
20 : },
21 : isSelected(index) && !disabled
22 : ? 'text-foreground'
23 : : 'text-muted-foreground'
24 : )
25 : "
26 : :disabled="disabled"
27 : @click="handleSelect(index)"
28 : >
29 : {{ getOptionLabel(option) }}
30 : </button>
31 : </div>
32 : </template>
33 :
34 : <script
35 : setup
36 : lang="ts"
37 : generic="
38 : T extends string | number | { label: string; value: string | number }
39 : "
40 : >
41 : import { cn } from '@comfyorg/tailwind-utils'
42 :
43 : import { WidgetInputBaseClass } from '@/renderer/extensions/vueNodes/widgets/components/layout'
44 :
45 : type ModelValue = T extends object ? T['value'] : T
46 :
47 : interface Props {
48 : modelValue: ModelValue | null | undefined
49 : options: T[]
50 : disabled?: boolean
51 : optionLabel?: string
52 : optionValue?: string
53 : }
54 :
55 : interface Emits {
56 : 'update:modelValue': [value: ModelValue]
57 : }
58 :
59 : const {
60 : modelValue,
61 : options,
62 : optionLabel = 'label',
63 : optionValue = 'value',
64 : disabled
65 : } = defineProps<Props>()
66 :
67 11 : const emit = defineEmits<Emits>()
68 :
69 11 : const getOptionValue = (option: T, index: number): ModelValue => {
70 184 : if (typeof option !== 'object') {
71 0 : return option as ModelValue
72 : }
73 :
74 184 : const valueField = optionValue
75 184 : const optionRecord = option as Record<string, unknown>
76 : const value =
77 : optionRecord[valueField] ??
78 : option.value ??
79 : optionRecord.name ??
80 : option.label ??
81 : index
82 : return value as ModelValue
83 : }
84 :
85 11 : const getOptionLabel = (option: T): string => {
86 45 : if (typeof option === 'object' && option !== null) {
87 45 : const labelField = optionLabel
88 45 : const optionRecord = option as Record<string, unknown>
89 45 : return String(
90 : optionRecord[labelField] ??
91 : option.label ??
92 : optionRecord.name ??
93 : option.value ??
94 : option
95 : )
96 : }
97 0 : return String(option)
98 : }
99 :
100 11 : const isSelected = (index: number): boolean => {
101 135 : const optVal = getOptionValue(options[index], index)
102 135 : return String(optVal) === String(modelValue ?? '')
103 : }
104 :
105 11 : const handleSelect = (index: number) => {
106 4 : if (disabled) return
107 :
108 4 : const optVal = getOptionValue(options[index], index)
109 4 : emit('update:modelValue', optVal)
110 : }
111 : </script>
|