Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,6 @@ const { data: users, status, execute } = await useLazyFetch('https://jsonplaceho
immediate: false
})

function getUserAvatar(value: string) {
return users.value?.find(user => user.value === value)?.avatar || {}
}

function onOpen() {
if (!users.value?.length) {
execute()
Expand All @@ -34,10 +30,10 @@ function onOpen() {
class="w-48"
@update:open="onOpen"
>
<template #leading="{ modelValue, ui }">
<template #leading="{ item, ui }">
<UAvatar
v-if="modelValue"
v-bind="getUserAvatar(modelValue)"
v-if="item"
v-bind="item.avatar"
:size="(ui.leadingAvatarSize() as AvatarProps['size'])"
:class="ui.leadingAvatar()"
/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,14 @@ const items = ref([
] satisfies SelectItem[])

const value = ref(items.value[0]?.value)

function getChip(value: string) {
return items.value.find(item => item.value === value)?.chip
}
</script>

<template>
<USelect v-model="value" :items="items" value-key="value" class="w-48">
<template #leading="{ modelValue, ui }">
<template #leading="{ item, ui }">
<UChip
v-if="modelValue"
v-bind="getChip(modelValue)"
v-if="item"
v-bind="item.chip"
inset
standalone
:size="(ui.itemLeadingChipSize() as ChipProps['size'])"
Expand Down
4 changes: 4 additions & 0 deletions docs/content/docs/2.components/input-menu.md
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,10 @@ props:
Use the `by` prop to compare objects by a field instead of reference when the `model-value` is an object.
::

::note
The `#leading`, `#default` and `#trailing` slots receive the selected item as `item`, so you can still read its other properties when `model-value` only holds its key.
::

### Multiple

Use the `multiple` prop to allow multiple selections, the selected items will be displayed as tags.
Expand Down
4 changes: 4 additions & 0 deletions docs/content/docs/2.components/select-menu.md
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,10 @@ props:
Use the `by` prop to compare objects by a field instead of reference when the `model-value` is an object.
::

::note
The `#leading`, `#default` and `#trailing` slots receive the selected item as `item`, so you can still read its other properties when `model-value` only holds its key.
::

### Multiple

Use the `multiple` prop to allow multiple selections, the selected items will be separated by a comma in the trigger.
Expand Down
10 changes: 7 additions & 3 deletions docs/content/docs/2.components/select.md
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,10 @@ props:
---
::

::tip
The `#leading`, `#default` and `#trailing` slots receive the selected item as `item`, so you can still read its other properties when `model-value` only holds its key.
::

### Multiple

Use the `multiple` prop to allow multiple selections, the selected items will be separated by a comma in the trigger.
Expand Down Expand Up @@ -692,7 +696,7 @@ In this example, the icon is computed from the `value` property of the selected
::

::tip
You can also use the `#leading` slot to display the selected icon.
You can also use the `#leading` slot and its `item` prop to display the selected icon.
::

### With avatar in items
Expand All @@ -711,7 +715,7 @@ In this example, the avatar is computed from the `value` property of the selecte
::

::tip
You can also use the `#leading` slot to display the selected avatar.
You can also use the `#leading` slot and its `item` prop to display the selected avatar.
::

### With chip in items
Expand All @@ -726,7 +730,7 @@ name: 'select-items-chip-example'
::

::note
In this example, the `#leading` slot is used to display the selected chip.
In this example, the `#leading` slot uses its `item` prop to display the chip of the selected item.
::

### Control open state
Expand Down
6 changes: 6 additions & 0 deletions playgrounds/nuxt/app/pages/components/input-menu.vue
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ const { data: users, status } = await useFetch('https://jsonplaceholder.typicode

const value = ref('Apple')
const valueMultiple = ref([fruits[0]!, vegetables[0]!])
const statusKey = ref('todo')
</script>

<template>
Expand Down Expand Up @@ -128,5 +129,10 @@ const valueMultiple = ref([fruits[0]!, vegetables[0]!])
:items="[Array(1000).fill(0).map((_, i) => ({ label: `item-${i}`, value: i }))]"
v-bind="props"
/>
<UInputMenu v-model="statusKey" :items="statuses" value-key="value" v-bind="props">
<template #leading="{ item, ui }">
<UIcon v-if="item" :name="item.icon" :class="ui.leadingIcon()" />
</template>
</UInputMenu>
</Matrix>
</template>
16 changes: 16 additions & 0 deletions playgrounds/nuxt/app/pages/components/select-menu.vue
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ const { data: users, status } = await useFetch('https://jsonplaceholder.typicode

const value = ref('Apple')
const valueMultiple = ref([fruits[0]!, vegetables[0]!])

const statusKey = ref('todo')
const statusKeys = ref(['todo', 'done'])
</script>

<template>
Expand Down Expand Up @@ -127,5 +130,18 @@ const valueMultiple = ref([fruits[0]!, vegetables[0]!])
:items="[Array(1000).fill(0).map((_, i) => ({ label: `item-${i}`, value: i }))]"
v-bind="props"
/>
<USelectMenu v-model="statusKey" :items="statuses" value-key="value" v-bind="props">
<template #leading="{ item, ui }">
<UIcon v-if="item" :name="item.icon" :class="ui.leadingIcon()" />
</template>
<template #default="{ modelValue, item }">
{{ item?.label }} ({{ modelValue }})
</template>
</USelectMenu>
<USelectMenu v-model="statusKeys" :items="statuses" value-key="value" multiple v-bind="props">
<template #default="{ item }">
{{ item.map(i => i.label).join(' + ') }}
</template>
</USelectMenu>
</Matrix>
</template>
31 changes: 19 additions & 12 deletions playgrounds/nuxt/app/pages/components/select.vue
Original file line number Diff line number Diff line change
Expand Up @@ -52,16 +52,10 @@ const { data: users, status } = await useFetch('https://jsonplaceholder.typicode
lazy: true
})

function getStatusIcon(value: string) {
return statuses.find(status => status.value === value)?.icon || 'i-lucide-user'
}

function getUserAvatar(value: string) {
return users.value?.find(user => user.value === value)?.avatar || {}
}

const value = ref('Apple')
const valueMultiple = ref([fruits[0]!, vegetables[0]!])
const statusKey = ref('todo')
const statusKeys = ref(['todo', 'done'])
</script>

<template>
Expand Down Expand Up @@ -100,8 +94,8 @@ const valueMultiple = ref([fruits[0]!, vegetables[0]!])
:items="statuses"
v-bind="props"
>
<template #leading="{ modelValue, ui }">
<UIcon v-if="modelValue" :name="getStatusIcon(modelValue)" :class="ui.leadingIcon()" />
<template #leading="{ item, ui }">
<UIcon v-if="item" :name="item.icon" :class="ui.leadingIcon()" />
</template>
</USelect>
<USelect
Expand All @@ -111,8 +105,21 @@ const valueMultiple = ref([fruits[0]!, vegetables[0]!])
:items="users"
v-bind="props"
>
<template #leading="{ modelValue, ui }">
<UAvatar v-if="modelValue" :size="(ui.itemLeadingAvatarSize() as AvatarProps['size'])" v-bind="getUserAvatar(modelValue)" />
<template #leading="{ item, ui }">
<UAvatar v-if="item" :size="(ui.itemLeadingAvatarSize() as AvatarProps['size'])" v-bind="item.avatar" />
</template>
</USelect>
<USelect v-model="statusKey" :items="statuses" v-bind="props">
<template #leading="{ item, ui }">
<UIcon v-if="item" :name="item.icon" :class="ui.leadingIcon()" />
</template>
<template #default="{ modelValue, item }">
{{ item?.label }} ({{ modelValue }})
</template>
</USelect>
<USelect v-model="statusKeys" :items="statuses" multiple v-bind="props">
<template #default="{ item }">
{{ item.map(i => i.label).join(' + ') }}
</template>
</USelect>
</Matrix>
Expand Down
46 changes: 38 additions & 8 deletions src/runtime/components/InputMenu.vue
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import type { InputProps } from './Input.vue'
import type { LinkPropsKeys } from './Link.vue'
import type { ModelModifiers, ApplyModifiers } from '../types/input'
import type { InputHTMLAttributes } from '../types/html'
import type { AcceptableValue, ArrayOrNested, GetItemKeys, GetItemValue, GetModelValue, NestedItem, EmitsToProps } from '../types/utils'
import type { AcceptableValue, ArrayOrNested, GetItemKeys, GetItemValue, GetModelValue, GetSelectedItem, NestedItem, EmitsToProps } from '../types/utils'
import type { ComponentConfig } from '../types/tv'

type InputMenu = ComponentConfig<typeof theme, AppConfig, 'inputMenu'>
Expand Down Expand Up @@ -211,6 +211,19 @@ export interface InputMenuEmits<

type SlotProps<T extends InputMenuItem> = (props: { item: T, index: number, ui: InputMenu['ui'] }) => VNode[]

type TriggerSlotProps<
A extends ArrayOrNested<InputMenuItem>,
VK extends GetItemKeys<A> | undefined,
M extends boolean,
Mod extends Omit<ModelModifiers, 'lazy'>,
C extends boolean | object
> = (props: {
modelValue: ApplyModifiers<GetModelValue<A, VK, M, ExcludeItem>, Mod> | IsClearUsed<M, C>
item: GetSelectedItem<A, M, ExcludeItem>
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
open: boolean
ui: InputMenu['ui']
}) => VNode[]

export interface InputMenuSlots<
A extends ArrayOrNested<InputMenuItem> = ArrayOrNested<InputMenuItem>,
VK extends GetItemKeys<A> | undefined = undefined,
Expand All @@ -219,9 +232,9 @@ export interface InputMenuSlots<
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: InputMenu['ui'] }): VNode[]
'default'?(props: { modelValue: ApplyModifiers<GetModelValue<A, VK, M, ExcludeItem>, Mod> | IsClearUsed<M, C>, open: boolean, ui: InputMenu['ui'] }): VNode[]
'trailing'?(props: { modelValue: ApplyModifiers<GetModelValue<A, VK, M, ExcludeItem>, Mod> | IsClearUsed<M, C>, open: boolean, ui: InputMenu['ui'] }): VNode[]
'leading'?: TriggerSlotProps<A, VK, M, Mod, C>
'default'?: TriggerSlotProps<A, VK, M, Mod, C>
'trailing'?: TriggerSlotProps<A, VK, M, Mod, C>
'empty'?(props: { searchTerm: string }): VNode[]
'item'?: SlotProps<T>
'item-leading'?: SlotProps<T>
Expand Down Expand Up @@ -251,7 +264,7 @@ 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 { compare, get, getDisplayValue, getSelectedItem, isArrayOfArray, looseToNumber } from '../utils'
import { getEstimateSize } from '../utils/virtualizer'
import { tv } from '../utils/tv'
import UIcon from './Icon.vue'
Expand Down Expand Up @@ -376,6 +389,23 @@ const ui = computed(() => tv({ extend: theme, ...(appConfig.ui?.inputMenu || {})
// eslint-disable-next-line vue/no-dupe-keys
const items = computed(() => groups.value.flatMap(group => group) as T[])

function resolveSelectedItem(slot: 'leading' | 'default' | 'trailing', value: unknown) {
if (!slots[slot]) {
return undefined as GetSelectedItem<T, M, ExcludeItem>
}

// In autocomplete mode `modelValue` is the typed text, so nothing is selected.
if (isAutocomplete.value) {
return (props.multiple ? [] : undefined) as GetSelectedItem<T, M, ExcludeItem>
}

return getSelectedItem(items.value, value, {
valueKey: props.valueKey,
by: props.by,
multiple: isMultiple.value
}) as GetSelectedItem<T, M, ExcludeItem>
}

function displayValue(value: GetItemValue<T, VK, ExcludeItem>): string {
return getDisplayValue<T[], GetItemValue<T, VK, ExcludeItem>>(items.value, value, {
labelKey: props.labelKey,
Expand Down Expand Up @@ -768,17 +798,17 @@ defineExpose({
@update:model-value="onInputUpdate($event, open, modelValue)"
/>

<slot :model-value="(modelValue as ApplyModifiers<GetModelValue<T, VK, M, ExcludeItem>, Mod>)" :open="open" :ui="ui" />
<slot :model-value="(modelValue as ApplyModifiers<GetModelValue<T, VK, M, ExcludeItem>, Mod>)" :item="resolveSelectedItem('default', modelValue)" :open="open" :ui="ui" />

<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">
<slot name="leading" :model-value="(modelValue as ApplyModifiers<GetModelValue<T, VK, M, ExcludeItem>, Mod>)" :item="resolveSelectedItem('leading', modelValue)" :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>

<Component.Trigger v-if="isTrailing || !!slots.trailing || !!props.clear" :aria-label="t('inputMenu.trigger')" 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">
<slot name="trailing" :model-value="(modelValue as ApplyModifiers<GetModelValue<T, VK, M, ExcludeItem>, Mod>)" :item="resolveSelectedItem('trailing', modelValue)" :open="open" :ui="ui">
<Component.Cancel v-if="!!props.clear && !disabled && !isModelValueEmpty(modelValue as ApplyModifiers<GetModelValue<T, VK, M, ExcludeItem>, Mod>)" as-child>
<UButton
as="span"
Expand Down
39 changes: 31 additions & 8 deletions src/runtime/components/Select.vue
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import type { IconProps } from './Icon.vue'
import type { InputProps } from './Input.vue'
import type { ModelModifiers, ApplyModifiers } from '../types/input'
import type { ButtonHTMLAttributes } from '../types/html'
import type { AcceptableValue, ArrayOrNested, GetItemKeys, GetModelValue, NestedItem, EmitsToProps } from '../types/utils'
import type { AcceptableValue, ArrayOrNested, GetItemKeys, GetModelValue, GetSelectedItem, NestedItem, EmitsToProps } from '../types/utils'
import type { ComponentConfig } from '../types/tv'

type Select = ComponentConfig<typeof theme, AppConfig, 'select'>
Expand Down Expand Up @@ -132,16 +132,28 @@ export interface SelectEmits<

type SlotProps<T extends SelectItem> = (props: { item: T, index: number, ui: Select['ui'] }) => VNode[]

type TriggerSlotProps<
A extends ArrayOrNested<SelectItem>,
VK extends GetItemKeys<A> | undefined,
M extends boolean,
Mod extends Omit<ModelModifiers, 'lazy'>
> = (props: {
modelValue: ApplyModifiers<GetModelValue<A, VK, M, ExcludeItem>, Mod>
item: GetSelectedItem<A, M, ExcludeItem>
open: boolean
ui: Select['ui']
}) => VNode[]

export interface SelectSlots<
A extends ArrayOrNested<SelectItem> = ArrayOrNested<SelectItem>,
VK extends GetItemKeys<A> | undefined = undefined,
M extends boolean = false,
Mod extends Omit<ModelModifiers, 'lazy'> = Omit<ModelModifiers, 'lazy'>,
T extends NestedItem<A> = NestedItem<A>
> {
'leading'?(props: { modelValue: ApplyModifiers<GetModelValue<A, VK, M, ExcludeItem>, Mod>, open: boolean, ui: Select['ui'] }): VNode[]
'default'?(props: { modelValue: ApplyModifiers<GetModelValue<A, VK, M, ExcludeItem>, Mod>, open: boolean, ui: Select['ui'] }): VNode[]
'trailing'?(props: { modelValue: ApplyModifiers<GetModelValue<A, VK, M, ExcludeItem>, Mod>, open: boolean, ui: Select['ui'] }): VNode[]
'leading'?: TriggerSlotProps<A, VK, M, Mod>
'default'?: TriggerSlotProps<A, VK, M, Mod>
'trailing'?: TriggerSlotProps<A, VK, M, Mod>
'item'?: SlotProps<T>
'item-leading'?: SlotProps<T>
'item-label'?(props: { item: T, index: number }): VNode[]
Expand All @@ -163,7 +175,7 @@ import { useFieldGroup, FieldGroupReset } from '../composables/useFieldGroup'
import { useComponentIcons } from '../composables/useComponentIcons'
import { useFormField } from '../composables/useFormField'
import { usePortal } from '../composables/usePortal'
import { get, getDisplayValue, isArrayOfArray, looseToNumber, omitUnsafeAttrs } from '../utils'
import { get, getDisplayValue, getSelectedItem, isArrayOfArray, looseToNumber, omitUnsafeAttrs } from '../utils'
import { tv } from '../utils/tv'
import UIcon from './Icon.vue'
import UAvatar from './Avatar.vue'
Expand Down Expand Up @@ -262,6 +274,17 @@ function displayValue(value: ApplyModifiers<GetModelValue<T, VK, M, ExcludeItem>
})
}

function resolveSelectedItem(slot: 'leading' | 'default' | 'trailing', value: unknown) {
if (!slots[slot]) {
return undefined as GetSelectedItem<T, M, ExcludeItem>
}

return getSelectedItem(items.value, value, {
valueKey: props.valueKey,
multiple: props.multiple
}) as GetSelectedItem<T, M, ExcludeItem>
}

const triggerRef = useTemplateRef('triggerRef')

function autoFocus() {
Expand Down Expand Up @@ -373,7 +396,7 @@ defineExpose({
@click="onTriggerClick(open)"
>
<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">
<slot name="leading" :model-value="(modelValue as ApplyModifiers<GetModelValue<T, VK, M, ExcludeItem>, Mod>)" :item="resolveSelectedItem('leading', modelValue)" :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>
Expand All @@ -384,14 +407,14 @@ defineExpose({
:data-slot="displayedModelValue != null ? 'value' : 'placeholder'"
:class="displayedModelValue != null ? ui.value({ class: props.ui?.value }) : ui.placeholder({ class: props.ui?.placeholder })"
>
<slot :model-value="(modelValue as ApplyModifiers<GetModelValue<T, VK, M, ExcludeItem>, Mod>)" :open="open" :ui="ui">
<slot :model-value="(modelValue as ApplyModifiers<GetModelValue<T, VK, M, ExcludeItem>, Mod>)" :item="resolveSelectedItem('default', modelValue)" :open="open" :ui="ui">
{{ displayedModelValue ?? (props.placeholder ?? '&nbsp;') }}
</slot>
</RSelectValue>
</template>

<span v-if="isTrailing || !!slots.trailing" 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">
<slot name="trailing" :model-value="(modelValue as ApplyModifiers<GetModelValue<T, VK, M, ExcludeItem>, Mod>)" :item="resolveSelectedItem('trailing', modelValue)" :open="open" :ui="ui">
<UIcon v-if="trailingIconName" :name="trailingIconName" data-slot="trailingIcon" :class="ui.trailingIcon({ class: props.ui?.trailingIcon, leading: isLeading })" />
</slot>
</span>
Expand Down
Loading
Loading