Line data Source code
1 : <template>
2 525 : <ComboboxPortal :key="Number(open)">
3 : <ComboboxContent
4 : position="popper"
5 : :side-offset="8"
6 : align="start"
7 : :style="[popoverStyle, contentStyle]"
8 : :class="cn(selectContentClass, 'flex flex-col')"
9 : @keydown="onKeydown"
10 : @focus-outside="preventFocusDismiss"
11 : >
12 : <FocusScope class="contents" @mount-auto-focus.prevent>
13 : <div v-if="showSearchBox" class="px-2 pt-2 pb-0">
14 : <div
15 : class="flex items-center gap-2 rounded-lg border px-3 py-1.5 text-sm"
16 : >
17 : <i class="icon-[lucide--search] text-muted-foreground" />
18 : <ComboboxInput
19 : v-model="searchQuery"
20 : :placeholder="searchPlaceholder"
21 : :aria-label="searchLabel"
22 : class="w-full border-none bg-transparent outline-none"
23 : />
24 : </div>
25 : </div>
26 :
27 : <div
28 : v-if="hasActions && actionsPlacement === 'header'"
29 : :class="actionsClass"
30 : >
31 : <span
32 : v-if="formatSelectedCount"
33 : class="px-1 text-sm text-muted-foreground"
34 : >
35 : {{ formatSelectedCount(selectedItems.length) }}
36 : </span>
37 : <Button
38 : v-if="showClearButton"
39 : variant="textonly"
40 : size="md"
41 : @click.stop="selectedItems = []"
42 : >
43 : {{ clearAllLabel }}
44 : </Button>
45 : </div>
46 :
47 : <ComboboxViewport
48 : :class="
49 : cn(
50 : 'flex flex-col gap-0 p-0 text-sm',
51 : 'scrollbar-custom overflow-y-auto',
52 : 'min-w-(--reka-combobox-trigger-width)'
53 : )
54 : "
55 : :style="{ maxHeight: `min(${listMaxHeight}, 50vh)` }"
56 : >
57 10 : <ComboboxItem
58 : v-for="opt in filteredOptions"
59 : :key="opt.value"
60 : :value="opt"
61 : :class="cn('group', selectItemVariants({ layout: 'multi' }))"
62 : >
63 : <div
64 : class="flex size-4 shrink-0 items-center justify-center rounded-sm transition-all duration-200 group-data-[state=checked]:bg-foreground group-data-[state=unchecked]:bg-secondary [&>span]:flex"
65 : >
66 : <ComboboxItemIndicator>
67 : <i
68 : class="icon-[lucide--check] text-xs font-bold text-background"
69 : />
70 : </ComboboxItemIndicator>
71 : </div>
72 : <span>{{ opt.name }}</span>
73 : </ComboboxItem>
74 : <ComboboxEmpty :class="selectEmptyMessageClass">
75 : {{ emptyText }}
76 : </ComboboxEmpty>
77 : </ComboboxViewport>
78 :
79 : <div
80 : v-if="hasActions && actionsPlacement === 'footer'"
81 : :class="actionsClass"
82 : >
83 : <span
84 : v-if="formatSelectedCount"
85 : class="px-1 text-sm text-muted-foreground"
86 : >
87 : {{ formatSelectedCount(selectedItems.length) }}
88 : </span>
89 : <Button
90 : v-if="showClearButton"
91 : variant="textonly"
92 : size="md"
93 : @click.stop="selectedItems = []"
94 : >
95 : {{ clearAllLabel }}
96 : </Button>
97 : </div>
98 : </FocusScope>
99 : </ComboboxContent>
100 : </ComboboxPortal>
101 : </template>
102 :
103 : <script setup lang="ts">
104 : import type { FocusOutsideEvent } from 'reka-ui'
105 : import {
106 : ComboboxContent,
107 : ComboboxEmpty,
108 : ComboboxInput,
109 : ComboboxItem,
110 : ComboboxItemIndicator,
111 : ComboboxViewport,
112 : FocusScope,
113 : injectComboboxRootContext,
114 : ComboboxPortal
115 : } from 'reka-ui'
116 : import { computed } from 'vue'
117 : import type { StyleValue } from 'vue'
118 :
119 : import Button from '@comfyorg/design-system/button/Button'
120 : import type { SelectOption } from '@comfyorg/design-system/lib/select/types'
121 : import { useSelectSearch } from '@comfyorg/design-system/lib/select/useSelectSearch'
122 :
123 : import {
124 : selectContentClass,
125 : selectEmptyMessageClass,
126 : selectItemVariants,
127 : stopEscapeToDocument
128 : } from '@comfyorg/design-system/select.variants'
129 : import { cn } from '@comfyorg/tailwind-utils'
130 :
131 : const {
132 : options,
133 : searchLabel,
134 : emptyText,
135 : clearAllLabel,
136 : formatSelectedCount,
137 : showSearchBox,
138 : showClearButton,
139 : actionsPlacement,
140 : searchPlaceholder,
141 : listMaxHeight,
142 : popoverStyle,
143 : contentStyle
144 : } = defineProps<{
145 : searchLabel?: string
146 : emptyText: string
147 : clearAllLabel?: string
148 : formatSelectedCount?: (count: number) => string
149 : options: SelectOption[]
150 : showSearchBox: boolean
151 : showClearButton: boolean
152 : actionsPlacement: 'header' | 'footer'
153 : searchPlaceholder?: string
154 : listMaxHeight: string
155 : popoverStyle: StyleValue
156 : contentStyle?: StyleValue
157 : }>()
158 :
159 156 : const { open } = injectComboboxRootContext()
160 :
161 156 : const emit = defineEmits<{
162 : escape: []
163 : }>()
164 :
165 156 : const selectedItems = defineModel<SelectOption[]>({ required: true })
166 156 : const searchQuery = defineModel<string>('searchQuery', { required: true })
167 :
168 156 : const hasActions = computed(
169 : () => Boolean(formatSelectedCount) || showClearButton
170 : )
171 156 : const actionsClass = computed(() =>
172 : cn(
173 : 'flex shrink-0 items-center justify-between',
174 : actionsPlacement === 'header'
175 : ? 'mt-2 border-b px-2 pb-4'
176 : : '-mx-2 mt-2 border-t px-4 pt-3 pb-1'
177 : )
178 : )
179 156 : const searchResults = useSelectSearch(searchQuery, () => options)
180 156 : const filteredOptions = computed(() => {
181 1 : if (!searchQuery.value.trim()) return options
182 :
183 : const selectedButNotInResults = selectedItems.value.filter(
184 : (item) => !searchResults.value.some(({ value }) => value === item.value)
185 : )
186 0 : return [...selectedButNotInResults, ...searchResults.value]
187 : })
188 :
189 1 : function onKeydown(event: KeyboardEvent) {
190 1 : if (event.key !== 'Escape') return
191 :
192 1 : stopEscapeToDocument(event)
193 1 : emit('escape')
194 : }
195 :
196 0 : function preventFocusDismiss(event: FocusOutsideEvent) {
197 0 : event.preventDefault()
198 : }
199 : </script>
|