-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Expand file tree
/
Copy pathSelectMenu.vue
More file actions
704 lines (625 loc) · 28.8 KB
/
SelectMenu.vue
File metadata and controls
704 lines (625 loc) · 28.8 KB
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
<script lang="ts">
import type { ComboboxRootProps, ComboboxRootEmits, ComboboxContentProps, ComboboxContentEmits, ComboboxArrowProps } from 'reka-ui'
import type { VNode } from 'vue'
import type { AppConfig } from '@nuxt/schema'
import theme from '#build/ui/select-menu'
import type { UseComponentIconsProps } from '../composables/useComponentIcons'
import type { AvatarProps, ButtonProps, ChipProps, IconProps, InputProps, LinkPropsKeys, BadgeProps } from '../types'
import type { ModelModifiers, ApplyModifiers } from '../types/input'
import type { ButtonHTMLAttributes } from '../types/html'
import type { AcceptableValue, ArrayOrNested, GetItemKeys, GetItemValue, GetModelValue, NestedItem, EmitsToProps } from '../types/utils'
import type { ComponentConfig } from '../types/tv'
type SelectMenu = ComponentConfig<typeof theme, AppConfig, 'selectMenu'>
export type SelectMenuValue = AcceptableValue
export type SelectMenuItem = SelectMenuValue | {
label?: string
description?: string
/**
* @IconifyIcon
*/
icon?: IconProps['name']
avatar?: AvatarProps
chip?: ChipProps
/**
* Display a badge on the item.
* `{ color: 'neutral', variant: 'soft', size: 'sm' }`{lang="ts-type"}
*/
badge?: string | number | BadgeProps
/**
* The item type.
* @defaultValue 'item'
*/
type?: 'label' | 'separator' | 'item'
disabled?: boolean
onSelect?: (e: Event) => void
class?: any
ui?: Pick<SelectMenu['slots'], 'label' | 'separator' | 'item' | 'itemLeadingIcon' | 'itemLeadingAvatarSize' | 'itemLeadingAvatar' | 'itemLeadingChipSize' | 'itemLeadingChip' | 'itemWrapper' | 'itemLabel' | 'itemDescription' | 'itemTrailing' | 'itemTrailingIcon' | 'itemBadgeSize' | 'itemBadge'>
[key: string]: any
}
type ExcludeItem = { type: 'label' | 'separator' }
type IsClearUsed<M extends boolean, C extends boolean | object> = M extends false
? (C extends true ? null : C extends object ? null : never)
: never
export interface SelectMenuProps<T extends ArrayOrNested<SelectMenuItem> = ArrayOrNested<SelectMenuItem>, VK extends GetItemKeys<T> | undefined = undefined, M extends boolean = false, Mod extends Omit<ModelModifiers, 'lazy'> = Omit<ModelModifiers, 'lazy'>, C extends boolean | object = false> extends Pick<ComboboxRootProps<T>, 'open' | 'defaultOpen' | 'disabled' | 'name' | 'resetSearchTermOnBlur' | 'resetSearchTermOnSelect' | 'resetModelValueOnClear' | 'highlightOnHover' | 'by'>, UseComponentIconsProps, /** @vue-ignore */ Omit<ButtonHTMLAttributes, 'type' | 'disabled' | 'name'> {
id?: string
/** The placeholder text when the select is empty. */
placeholder?: string
/**
* Whether to display the search input or not.
* Can be an object to pass additional props to the input.
* `{ placeholder: 'Search...', variant: 'none' }`{lang="ts-type"}
* @defaultValue true
*/
searchInput?: boolean | Omit<InputProps, 'modelValue' | 'defaultValue'>
/**
* @defaultValue 'primary'
*/
color?: SelectMenu['variants']['color']
/**
* @defaultValue 'outline'
*/
variant?: SelectMenu['variants']['variant']
/**
* @defaultValue 'md'
*/
size?: SelectMenu['variants']['size']
required?: boolean
/**
* The icon displayed to open the menu.
* @defaultValue appConfig.ui.icons.chevronDown
* @IconifyIcon
*/
trailingIcon?: IconProps['name']
/**
* The icon displayed when an item is selected.
* @defaultValue appConfig.ui.icons.check
* @IconifyIcon
*/
selectedIcon?: IconProps['name']
/**
* Display a clear button to reset the model value.
* Can be an object to pass additional props to the Button.
* @defaultValue false
*/
clear?: (C & boolean) | (C & Partial<Omit<ButtonProps, LinkPropsKeys>>)
/**
* The icon displayed in the clear button.
* @defaultValue appConfig.ui.icons.close
* @IconifyIcon
*/
clearIcon?: IconProps['name']
/**
* The content of the menu.
* @defaultValue { side: 'bottom', sideOffset: 8, collisionPadding: 8, position: 'popper' }
*/
content?: Omit<ComboboxContentProps, 'as' | 'asChild' | 'forceMount'> & Partial<EmitsToProps<ComboboxContentEmits>>
/**
* Display an arrow alongside the menu.
* `{ rounded: true }`{lang="ts-type"}
* @defaultValue false
*/
arrow?: boolean | Omit<ComboboxArrowProps, 'as' | 'asChild'>
/**
* Render the menu in a portal.
* @defaultValue true
*/
portal?: boolean | string | HTMLElement
/**
* Enable virtualization for large lists.
* Note: when enabled, all groups are flattened into a single list due to a limitation of Reka UI (https://github.com/unovue/reka-ui/issues/1885).
* @defaultValue false
*/
virtualize?: boolean | {
/**
* Number of items rendered outside the visible area
* @defaultValue 12
*/
overscan?: number
/**
* Estimated size (in px) of each item, or a function that returns the size for a given index
* @defaultValue 32
*/
estimateSize?: number | ((index: number) => number)
}
/**
* When `items` is an array of objects, select the field to use as the value instead of the object itself.
* @defaultValue undefined
*/
valueKey?: VK
/**
* When `items` is an array of objects, select the field to use as the label.
* @defaultValue 'label'
*/
labelKey?: GetItemKeys<T>
/**
* When `items` is an array of objects, select the field to use as the description.
* @defaultValue 'description'
*/
descriptionKey?: GetItemKeys<T>
items?: T
/** The value of the SelectMenu when initially rendered. Use when you do not need to control the state of the SelectMenu. */
defaultValue?: ApplyModifiers<GetModelValue<T, VK, M, ExcludeItem>, Mod> | IsClearUsed<M, C>
/** The controlled value of the SelectMenu. Can be binded-with with `v-model`. */
modelValue?: ApplyModifiers<GetModelValue<T, VK, M, ExcludeItem>, Mod> | IsClearUsed<M, C>
modelModifiers?: Mod
/** Whether multiple options can be selected or not. */
multiple?: M & boolean
/** Highlight the ring color like a focus state. */
highlight?: boolean
/**
* Determines if custom user input that does not exist in options can be added.
* @defaultValue false
*/
createItem?: boolean | 'always' | { position?: 'top' | 'bottom', when?: 'empty' | 'always' }
/**
* Fields to filter items by.
* @defaultValue [labelKey]
*/
filterFields?: string[]
/**
* When `true`, disable the default filters, useful for custom filtering (useAsyncData, useFetch, etc.).
* @defaultValue false
*/
ignoreFilter?: boolean
autofocus?: boolean
autofocusDelay?: number
class?: any
ui?: SelectMenu['slots']
}
export interface SelectMenuEmits<
A extends ArrayOrNested<SelectMenuItem>,
VK extends GetItemKeys<A> | undefined,
M extends boolean,
Mod extends Omit<ModelModifiers, 'lazy'> = Omit<ModelModifiers, 'lazy'>,
C extends boolean | object = false
> extends Pick<ComboboxRootEmits, 'update:open'> {
'change': [event: Event]
'blur': [event: FocusEvent]
'focus': [event: FocusEvent]
'create': [item: string]
'clear': []
/** Event handler when highlighted element changes. */
'highlight': [payload: {
ref: HTMLElement
value: ApplyModifiers<GetModelValue<A, VK, M, ExcludeItem>, Mod> | IsClearUsed<M, C>
} | undefined]
'update:modelValue': [value: ApplyModifiers<GetModelValue<A, VK, M, ExcludeItem>, Mod> | IsClearUsed<M, C>]
}
type SlotProps<T extends SelectMenuItem> = (props: { item: T, index: number, ui: SelectMenu['ui'] }) => VNode[]
export interface SelectMenuSlots<
A extends ArrayOrNested<SelectMenuItem> = ArrayOrNested<SelectMenuItem>,
VK extends GetItemKeys<A> | undefined = undefined,
M extends boolean = false,
Mod extends Omit<ModelModifiers, 'lazy'> = Omit<ModelModifiers, 'lazy'>,
C extends boolean | object = false,
T extends NestedItem<A> = NestedItem<A>
> {
'leading'?(props: {
modelValue: ApplyModifiers<GetModelValue<A, VK, M, ExcludeItem>, Mod> | IsClearUsed<M, C>
open: boolean
ui: SelectMenu['ui']
}): VNode[]
'default'?(props: {
modelValue: ApplyModifiers<GetModelValue<A, VK, M, ExcludeItem>, Mod> | IsClearUsed<M, C>
open: boolean
ui: SelectMenu['ui']
}): VNode[]
'trailing'?(props: {
modelValue: ApplyModifiers<GetModelValue<A, VK, M, ExcludeItem>, Mod> | IsClearUsed<M, C>
open: boolean
ui: SelectMenu['ui']
}): VNode[]
'empty'?(props: { searchTerm: string }): VNode[]
'item'?: SlotProps<T>
'item-leading'?: SlotProps<T>
'item-label'?(props: { item: T, index: number }): VNode[]
'item-description'?(props: { item: T, index: number }): VNode[]
'item-trailing'?: SlotProps<T>
'content-top'?: (props?: {}) => VNode[]
'content-bottom'?: (props?: {}) => VNode[]
'create-item-label'?(props: { item: string }): VNode[]
}
</script>
<script setup lang="ts" generic="T extends ArrayOrNested<SelectMenuItem>, VK extends GetItemKeys<T> | undefined = undefined, M extends boolean = false, Mod extends Omit<ModelModifiers, 'lazy'> = Omit<ModelModifiers, 'lazy'>, C extends boolean | object = false">
import { useTemplateRef, computed, onMounted, toRef, toRaw } from 'vue'
import { ComboboxRoot, ComboboxArrow, ComboboxAnchor, ComboboxInput, ComboboxTrigger, ComboboxCancel, ComboboxPortal, ComboboxContent, ComboboxEmpty, ComboboxGroup, ComboboxVirtualizer, ComboboxLabel, ComboboxSeparator, ComboboxItem, ComboboxItemIndicator, FocusScope } from 'reka-ui'
import { useForwardProps } from '../composables/useForwardProps'
import { defu } from 'defu'
import { reactivePick, createReusableTemplate } from '@vueuse/core'
import { useAppConfig } from '#imports'
import { useComponentProps } from '../composables/useComponentProps'
import { useFieldGroup, FieldGroupReset } from '../composables/useFieldGroup'
import { useComponentIcons } from '../composables/useComponentIcons'
import { useFormField } from '../composables/useFormField'
import { useFilter } from '../composables/useFilter'
import { useLocale } from '../composables/useLocale'
import { usePortal } from '../composables/usePortal'
import { compare, get, getDisplayValue, isArrayOfArray, looseToNumber } from '../utils'
import { getEstimateSize } from '../utils/virtualizer'
import { tv } from '../utils/tv'
import UIcon from './Icon.vue'
import UAvatar from './Avatar.vue'
import UButton from './Button.vue'
import UChip from './Chip.vue'
import UInput from './Input.vue'
defineOptions({ inheritAttrs: false })
const _props = withDefaults(defineProps<SelectMenuProps<T, VK, M, Mod, C>>(), {
portal: true,
searchInput: true,
labelKey: 'label',
descriptionKey: 'description',
resetSearchTermOnBlur: true,
resetSearchTermOnSelect: true,
resetModelValueOnClear: true,
autofocusDelay: 0,
virtualize: false
})
const emits = defineEmits<SelectMenuEmits<T, VK, M, Mod, C>>()
const slots = defineSlots<SelectMenuSlots<T, VK, M, Mod, C>>()
const props = useComponentProps<SelectMenuProps<T, VK, M, Mod, C>>('selectMenu', _props)
const searchTerm = defineModel<string>('searchTerm', { default: '' })
const { t } = useLocale()
const appConfig = useAppConfig() as SelectMenu['AppConfig']
const { filterGroups } = useFilter()
const rootProps = useForwardProps(reactivePick(props, 'modelValue', 'defaultValue', 'open', 'defaultOpen', 'required', 'multiple', 'resetSearchTermOnBlur', 'resetSearchTermOnSelect', 'resetModelValueOnClear', 'highlightOnHover', 'by'), emits)
const portalProps = usePortal(toRef(() => props.portal))
const contentProps = toRef(() => defu(props.content, { side: 'bottom', sideOffset: 8, collisionPadding: 8, position: 'popper' }) as ComboboxContentProps)
const arrowProps = toRef(() => defu(props.arrow, { rounded: true }) as ComboboxArrowProps)
const clearProps = computed(() => typeof props.clear === 'object' ? props.clear : {} as Partial<Omit<ButtonProps, LinkPropsKeys>>)
const virtualizerProps = toRef(() => {
if (!props.virtualize) return false
return defu(typeof props.virtualize === 'boolean' ? {} : props.virtualize, {
estimateSize: getEstimateSize(filteredItems.value, selectSize.value || 'md', props.descriptionKey as string, !!slots['item-description'])
})
})
const searchInputProps = toRef(() => defu(props.searchInput, { placeholder: t('selectMenu.search'), variant: 'none' }) as Omit<InputProps, 'modelValue' | 'defaultValue'>)
const { emitFormBlur, emitFormFocus, emitFormInput, emitFormChange, size: formFieldSize, color, id, name, highlight, disabled, ariaAttrs } = useFormField<InputProps>(_props)
const { orientation, size: fieldGroupSize } = useFieldGroup<InputProps>(_props)
const { isLeading, isTrailing, leadingIconName, trailingIconName } = useComponentIcons(toRef(() => defu(props, { trailingIcon: appConfig.ui.icons.chevronDown })))
const selectSize = computed(() => fieldGroupSize.value || formFieldSize.value)
const [DefineCreateItemTemplate, ReuseCreateItemTemplate] = createReusableTemplate()
const [DefineItemTemplate, ReuseItemTemplate] = createReusableTemplate<{ item: SelectMenuItem, index: number }>({
props: {
item: {
type: [Object, String, Number, Boolean],
required: true
},
index: {
type: Number,
required: false
}
}
})
// eslint-disable-next-line vue/no-dupe-keys
const ui = computed(() => tv({ extend: tv(theme), ...(appConfig.ui?.selectMenu || {}) })({
color: color.value ?? props.color,
variant: props.variant,
size: selectSize?.value ?? props.size,
loading: props.loading,
highlight: highlight.value ?? props.highlight,
leading: isLeading.value || !!props.avatar || !!slots.leading,
trailing: isTrailing.value || !!slots.trailing,
fieldGroup: orientation.value,
virtualize: !!props.virtualize
}))
function displayValue(value: GetItemValue<T, VK, ExcludeItem> | GetItemValue<T, VK, ExcludeItem>[]): string | undefined {
if (props.multiple && Array.isArray(value)) {
const displayedValues = value
.map(item => getDisplayValue<T[], GetItemValue<T, VK, ExcludeItem>>(items.value, item, {
labelKey: props.labelKey,
valueKey: props.valueKey,
by: props.by
}))
.filter((v): v is string => v != null && v !== '')
return displayedValues.length > 0 ? displayedValues.join(', ') : undefined
}
return getDisplayValue<T[], GetItemValue<T, VK, ExcludeItem>>(items.value, value as GetItemValue<T, VK, ExcludeItem>, {
labelKey: props.labelKey,
valueKey: props.valueKey,
by: props.by
})
}
const groups = computed<SelectMenuItem[][]>(() =>
props.items?.length
? isArrayOfArray(props.items)
? props.items
: [props.items]
: []
)
// eslint-disable-next-line vue/no-dupe-keys
const items = computed(() => groups.value.flatMap(group => group) as T[])
const filteredGroups = computed(() => {
if (props.ignoreFilter || !searchTerm.value) {
return groups.value
}
const fields = Array.isArray(props.filterFields) ? props.filterFields : [props.labelKey] as string[]
return filterGroups(groups.value, searchTerm.value, {
fields,
isStructural: (item: SelectMenuItem) => isSelectItem(item) && !!item.type && ['label', 'separator'].includes(item.type)
})
})
const filteredItems = computed(() => filteredGroups.value.flatMap(group => group))
// eslint-disable-next-line vue/no-dupe-keys
const createItem = computed(() => {
if (!props.createItem || !searchTerm.value) {
return false
}
const newItem = props.valueKey ? { [props.valueKey]: searchTerm.value } as NestedItem<T> : searchTerm.value
if ((typeof props.createItem === 'object' && props.createItem.when === 'always') || props.createItem === 'always') {
return !filteredItems.value.find(item => compare(item, newItem, (props.by ?? props.valueKey) as string | undefined))
}
return !filteredItems.value.length
})
const createItemPosition = computed(() => typeof props.createItem === 'object' ? props.createItem.position : 'bottom')
const triggerRef = useTemplateRef('triggerRef')
function autoFocus() {
if (props.autofocus) {
triggerRef.value?.$el?.focus({
focusVisible: true
})
}
}
onMounted(() => {
setTimeout(() => {
autoFocus()
}, props.autofocusDelay)
})
function onUpdate(value: any) {
if (toRaw(props.modelValue) === value) {
return
}
if (props.modelModifiers?.trim && (typeof value === 'string' || value === null || value === undefined)) {
value = value?.trim() ?? null
}
if (props.modelModifiers?.number) {
value = looseToNumber(value)
}
if (props.modelModifiers?.nullable) {
value ??= null
}
if (props.modelModifiers?.optional && !props.modelModifiers?.nullable && value !== null) {
value ??= undefined
}
// @ts-expect-error - 'target' does not exist in type 'EventInit'
const event = new Event('change', { target: { value } })
emits('change', event)
emitFormChange()
emitFormInput()
if (props.resetSearchTermOnSelect) {
searchTerm.value = ''
}
}
function onUpdateOpen(value: boolean) {
let timeoutId
if (!value) {
const event = new FocusEvent('blur')
emits('blur', event)
emitFormBlur()
// Since we use `displayValue` prop inside ComboboxInput we should reset searchTerm manually
// https://reka-ui.com/docs/components/combobox#api-reference
if (props.resetSearchTermOnBlur) {
const STATE_ANIMATION_DELAY_MS = 100
timeoutId = setTimeout(() => {
searchTerm.value = ''
}, STATE_ANIMATION_DELAY_MS)
}
} else {
const event = new FocusEvent('focus')
emits('focus', event)
emitFormFocus()
clearTimeout(timeoutId)
}
}
function onCreate(e: Event) {
e.preventDefault()
e.stopPropagation()
emits('create', searchTerm.value)
}
function onSelect(e: Event, item: SelectMenuItem) {
if (!isSelectItem(item)) {
return
}
if (item.disabled) {
e.preventDefault()
return
}
item.onSelect?.(e)
}
function isSelectItem(item: SelectMenuItem): item is Exclude<SelectMenuItem, SelectMenuValue> {
return typeof item === 'object' && item !== null
}
function isModelValueEmpty(modelValue: ApplyModifiers<GetModelValue<T, VK, M, ExcludeItem>, Mod>): boolean {
if (props.multiple && Array.isArray(modelValue)) {
return modelValue.length === 0
}
return modelValue === undefined || modelValue === null || modelValue === ''
}
function onClear() {
emits('clear')
}
const viewportRef = useTemplateRef('viewportRef')
defineExpose({
triggerRef: toRef(() => triggerRef.value?.$el as HTMLButtonElement),
viewportRef: toRef(() => viewportRef.value)
})
</script>
<!-- eslint-disable vue/no-template-shadow -->
<template>
<DefineCreateItemTemplate>
<ComboboxItem
data-slot="item"
:class="ui.item({ class: props.ui?.item })"
:value="searchTerm"
@select="onCreate"
>
<span data-slot="itemLabel" :class="ui.itemLabel({ class: props.ui?.itemLabel })">
<slot name="create-item-label" :item="searchTerm">
{{ t('selectMenu.create', { label: searchTerm }) }}
</slot>
</span>
</ComboboxItem>
</DefineCreateItemTemplate>
<DefineItemTemplate v-slot="{ item, index }">
<ComboboxLabel v-if="isSelectItem(item) && item.type === 'label'" data-slot="label" :class="ui.label({ class: [props.ui?.label, item.ui?.label, item.class] })">
{{ get(item, props.labelKey as string) }}
</ComboboxLabel>
<ComboboxSeparator v-else-if="isSelectItem(item) && item.type === 'separator'" data-slot="separator" :class="ui.separator({ class: [props.ui?.separator, item.ui?.separator, item.class] })" />
<ComboboxItem
v-else
data-slot="item"
:class="ui.item({ class: [props.ui?.item, isSelectItem(item) && item.ui?.item, isSelectItem(item) && item.class] })"
:disabled="isSelectItem(item) && item.disabled"
:value="props.valueKey && isSelectItem(item) ? get(item, props.valueKey as string) : item"
@select="onSelect($event, item)"
>
<slot name="item" :item="(item as NestedItem<T>)" :index="index" :ui="ui">
<slot name="item-leading" :item="(item as NestedItem<T>)" :index="index" :ui="ui">
<UIcon v-if="isSelectItem(item) && item.icon" :name="item.icon" data-slot="itemLeadingIcon" :class="ui.itemLeadingIcon({ class: [props.ui?.itemLeadingIcon, item.ui?.itemLeadingIcon] })" />
<UAvatar v-else-if="isSelectItem(item) && item.avatar" :size="((item.ui?.itemLeadingAvatarSize || props.ui?.itemLeadingAvatarSize || ui.itemLeadingAvatarSize()) as AvatarProps['size'])" v-bind="item.avatar" data-slot="itemLeadingAvatar" :class="ui.itemLeadingAvatar({ class: [props.ui?.itemLeadingAvatar, item.ui?.itemLeadingAvatar] })" />
<UChip
v-else-if="isSelectItem(item) && item.chip"
:size="((item.ui?.itemLeadingChipSize || props.ui?.itemLeadingChipSize || ui.itemLeadingChipSize()) as ChipProps['size'])"
inset
standalone
v-bind="item.chip"
data-slot="itemLeadingChip"
:class="ui.itemLeadingChip({ class: [props.ui?.itemLeadingChip, item.ui?.itemLeadingChip] })"
/>
</slot>
<span data-slot="itemWrapper" :class="ui.itemWrapper({ class: [props.ui?.itemWrapper, isSelectItem(item) && item.ui?.itemWrapper] })">
<span data-slot="itemLabel" :class="ui.itemLabel({ class: [props.ui?.itemLabel, isSelectItem(item) && item.ui?.itemLabel] })">
<slot name="item-label" :item="(item as NestedItem<T>)" :index="index">
{{ isSelectItem(item) ? get(item, props.labelKey as string) : item }}
</slot>
<UBadge
v-if="isSelectItem(item) && item.badge !== undefined && item.badge !== null"
color="neutral"
variant="soft"
:size="((item.ui?.itemBadgeSize || uiProp?.itemBadgeSize || ui.itemBadgeSize()) as BadgeProps['size'])"
v-bind="(typeof item.badge === 'string' || typeof item.badge === 'number') ? { label: item.badge } : item.badge"
data-slot="itemLeadingBadge"
:class="ui.itemBadge({ class: [uiProp?.itemBadge, item.ui?.itemBadge] })"
/>
</span>
<span v-if="isSelectItem(item) && (get(item, props.descriptionKey as string) || !!slots['item-description'])" data-slot="itemDescription" :class="ui.itemDescription({ class: [props.ui?.itemDescription, isSelectItem(item) && item.ui?.itemDescription] })">
<slot name="item-description" :item="(item as NestedItem<T>)" :index="index">
{{ get(item, props.descriptionKey as string) }}
</slot>
</span>
</span>
<span data-slot="itemTrailing" :class="ui.itemTrailing({ class: [props.ui?.itemTrailing, isSelectItem(item) && item.ui?.itemTrailing] })">
<slot name="item-trailing" :item="(item as NestedItem<T>)" :index="index" :ui="ui" />
<ComboboxItemIndicator as-child>
<UIcon :name="props.selectedIcon || appConfig.ui.icons.check" data-slot="itemTrailingIcon" :class="ui.itemTrailingIcon({ class: [props.ui?.itemTrailingIcon, isSelectItem(item) && item.ui?.itemTrailingIcon] })" />
</ComboboxItemIndicator>
</span>
</slot>
</ComboboxItem>
</DefineItemTemplate>
<ComboboxRoot
:id="id"
v-slot="{ modelValue, open }"
v-bind="({ ...rootProps, ...$attrs, ...ariaAttrs } as any)"
ignore-filter
as-child
:name="name"
:disabled="disabled"
@update:model-value="onUpdate"
@update:open="onUpdateOpen"
>
<ComboboxAnchor as-child>
<ComboboxTrigger ref="triggerRef" data-slot="base" :class="ui.base({ class: [props.ui?.base, props.class] })" tabindex="0">
<span v-if="isLeading || !!props.avatar || !!slots.leading" data-slot="leading" :class="ui.leading({ class: props.ui?.leading })">
<slot name="leading" :model-value="(modelValue as ApplyModifiers<GetModelValue<T, VK, M, ExcludeItem>, Mod>)" :open="open" :ui="ui">
<UIcon v-if="isLeading && leadingIconName" :name="leadingIconName" data-slot="leadingIcon" :class="ui.leadingIcon({ class: props.ui?.leadingIcon })" />
<UAvatar v-else-if="!!props.avatar" :size="((props.ui?.itemLeadingAvatarSize || ui.itemLeadingAvatarSize()) as AvatarProps['size'])" v-bind="props.avatar" data-slot="itemLeadingAvatar" :class="ui.itemLeadingAvatar({ class: props.ui?.itemLeadingAvatar })" />
</slot>
</span>
<slot :model-value="(modelValue as ApplyModifiers<GetModelValue<T, VK, M, ExcludeItem>, Mod>)" :open="open" :ui="ui">
<template v-for="displayedModelValue in [displayValue(modelValue as any)]" :key="displayedModelValue">
<span v-if="displayedModelValue !== undefined && displayedModelValue !== null" data-slot="value" :class="ui.value({ class: props.ui?.value })">
{{ displayedModelValue }}
</span>
<span v-else data-slot="placeholder" :class="ui.placeholder({ class: props.ui?.placeholder })">
{{ props.placeholder ?? ' ' }}
</span>
</template>
</slot>
<span v-if="isTrailing || !!slots.trailing || !!props.clear" data-slot="trailing" :class="ui.trailing({ class: props.ui?.trailing })">
<slot name="trailing" :model-value="(modelValue as ApplyModifiers<GetModelValue<T, VK, M, ExcludeItem>, Mod>)" :open="open" :ui="ui">
<ComboboxCancel v-if="!!props.clear && !isModelValueEmpty(modelValue as ApplyModifiers<GetModelValue<T, VK, M, ExcludeItem>, Mod>)" as-child>
<UButton
as="span"
:icon="props.clearIcon || appConfig.ui.icons.close"
:size="selectSize"
variant="link"
color="neutral"
tabindex="-1"
v-bind="clearProps"
data-slot="trailingClear"
:class="ui.trailingClear({ class: props.ui?.trailingClear })"
@click.stop="onClear"
/>
</ComboboxCancel>
<UIcon v-else-if="trailingIconName" :name="trailingIconName" data-slot="trailingIcon" :class="ui.trailingIcon({ class: props.ui?.trailingIcon })" />
</slot>
</span>
</ComboboxTrigger>
</ComboboxAnchor>
<ComboboxPortal v-bind="portalProps">
<FieldGroupReset>
<ComboboxContent data-slot="content" :class="ui.content({ class: props.ui?.content })" v-bind="contentProps">
<FocusScope trapped data-slot="focusScope" :class="ui.focusScope({ class: props.ui?.focusScope })">
<slot name="content-top" />
<ComboboxInput v-if="!!props.searchInput" v-model="searchTerm" :display-value="() => searchTerm" as-child>
<UInput
autofocus
autocomplete="off"
:size="selectSize"
v-bind="searchInputProps"
:model-modifiers="{
trim: props.modelModifiers?.trim
}"
data-slot="input"
:class="ui.input({ class: props.ui?.input })"
@change.stop
/>
</ComboboxInput>
<ComboboxEmpty data-slot="empty" :class="ui.empty({ class: props.ui?.empty })">
<slot name="empty" :search-term="searchTerm">
{{ searchTerm ? t('selectMenu.noMatch', { searchTerm }) : t('selectMenu.noData') }}
</slot>
</ComboboxEmpty>
<div ref="viewportRef" role="presentation" data-slot="viewport" :class="ui.viewport({ class: props.ui?.viewport })">
<template v-if="!!props.virtualize">
<ReuseCreateItemTemplate v-if="createItem && createItemPosition === 'top'" />
<ComboboxVirtualizer
v-slot="{ option: item, virtualItem }"
:options="(filteredItems as any[])"
:text-content="item => isSelectItem(item) ? get(item, props.labelKey as string) : String(item)"
v-bind="virtualizerProps"
>
<ReuseItemTemplate :item="item" :index="virtualItem.index" />
</ComboboxVirtualizer>
<ReuseCreateItemTemplate v-if="createItem && createItemPosition === 'bottom'" />
</template>
<template v-else>
<ComboboxGroup v-if="createItem && createItemPosition === 'top'" data-slot="group" :class="ui.group({ class: props.ui?.group })">
<ReuseCreateItemTemplate />
</ComboboxGroup>
<ComboboxGroup v-for="(group, groupIndex) in filteredGroups" :key="`group-${groupIndex}`" data-slot="group" :class="ui.group({ class: props.ui?.group })">
<ReuseItemTemplate v-for="(item, index) in group" :key="`group-${groupIndex}-${index}`" :item="item" :index="index" />
</ComboboxGroup>
<ComboboxGroup v-if="createItem && createItemPosition === 'bottom'" data-slot="group" :class="ui.group({ class: props.ui?.group })">
<ReuseCreateItemTemplate />
</ComboboxGroup>
</template>
</div>
<slot name="content-bottom" />
</FocusScope>
<ComboboxArrow v-if="!!props.arrow" v-bind="arrowProps" data-slot="arrow" :class="ui.arrow({ class: props.ui?.arrow })" />
</ComboboxContent>
</FieldGroupReset>
</ComboboxPortal>
</ComboboxRoot>
</template>