feat(SelectMenu): new component (#103)

This commit is contained in:
Benjamin Canac
2024-05-13 14:26:01 +02:00
committed by GitHub
parent 8baee1292f
commit 7a376b5e49
23 changed files with 2318 additions and 196 deletions

View File

@@ -218,12 +218,7 @@ onMounted(() => {
<ComboboxSeparator v-else-if="item?.type === 'separator'" :class="ui.separator()" />
<ComboboxItem
v-else
:class="ui.item()"
:disabled="item.disabled"
:value="item"
>
<ComboboxItem v-else :class="ui.item()" :disabled="item.disabled" :value="item">
<slot name="item" :item="(item as T)" :index="index">
<slot name="item-leading" :item="(item as T)" :index="index">
<UAvatar v-if="item.avatar" size="2xs" v-bind="item.avatar" :class="ui.itemLeadingAvatar()" />

View File

@@ -1,9 +1,10 @@
<script lang="ts">
import { tv, type VariantProps } from 'tailwind-variants'
import type { RadioGroupRootProps, RadioGroupRootEmits } from 'radix-vue'
import type { RadioGroupRootProps, RadioGroupRootEmits, RadioGroupItemProps } from 'radix-vue'
import type { AppConfig } from '@nuxt/schema'
import _appConfig from '#build/app.config'
import theme from '#build/ui/radio-group'
import type { AcceptableValue } from '#ui/types/utils'
const appConfig = _appConfig as AppConfig & { ui: { radioGroup: Partial<typeof theme> } }
@@ -11,9 +12,8 @@ const radioGroup = tv({ extend: tv(theme), ...(appConfig.ui?.radioGroup || {}) }
type RadioGroupVariants = VariantProps<typeof radioGroup>
export type RadioGroupItem = {
label: string
value: string
export interface RadioGroupItem extends Pick<RadioGroupItemProps, 'disabled' | 'value'> {
label?: string
description?: string
}
@@ -37,7 +37,7 @@ export interface RadioGroupSlots<T> {
}
</script>
<script setup lang="ts" generic="T extends RadioGroupItem | string">
<script setup lang="ts" generic="T extends RadioGroupItem | AcceptableValue">
import { computed } from 'vue'
import { RadioGroupRoot, RadioGroupItem, RadioGroupIndicator, Label, useForwardPropsEmits } from 'radix-vue'
import { reactivePick } from '@vueuse/core'

View File

@@ -135,12 +135,7 @@ const groups = computed(() => props.items?.length ? (Array.isArray(props.items[0
{{ item.label }}
</SelectLabel>
<SelectSeparator v-else-if="item?.type === 'separator'" :class="ui.separator()" />
<SelectItem
v-else
:class="ui.item()"
:disabled="item.disabled"
:value="typeof item === 'object' ? item.value : item"
>
<SelectItem v-else :class="ui.item()" :disabled="item.disabled" :value="typeof item === 'object' ? item.value : item">
<slot name="item" :item="(item as T)" :index="index">
<slot name="item-leading" :item="(item as T)" :index="index">
<UAvatar v-if="item.avatar" size="2xs" v-bind="item.avatar" :class="ui.itemLeadingAvatar()" />

View File

@@ -0,0 +1,242 @@
<script lang="ts">
import { tv, type VariantProps } from 'tailwind-variants'
import type { ComboboxRootProps, ComboboxRootEmits, ComboboxContentProps, ComboboxItemProps, ComboboxArrowProps } from 'radix-vue'
import type { AppConfig } from '@nuxt/schema'
import _appConfig from '#build/app.config'
import theme from '#build/ui/select-menu'
import type { UseComponentIconsProps } from '#ui/composables/useComponentIcons'
import type { AvatarProps, ChipProps, InputProps } from '#ui/types'
import type { AcceptableValue, ArrayOrWrapped } from '#ui/types/utils'
const appConfig = _appConfig as AppConfig & { ui: { selectMenu: Partial<typeof theme> } }
const selectMenu = tv({ extend: tv(theme), ...(appConfig.ui?.selectMenu || {}) })
export interface SelectMenuItem extends Pick<ComboboxItemProps, 'disabled'> {
label?: string
icon?: string
avatar?: AvatarProps
chip?: ChipProps
/**
* The item type.
* @defaultValue `'item'`
*/
type?: 'label' | 'separator' | 'item'
}
type SelectMenuVariants = VariantProps<typeof selectMenu>
export interface SelectMenuProps<T> extends Omit<ComboboxRootProps<T>, 'asChild' | 'dir' | 'filterFunction' | 'displayValue' | 'multiple'>, UseComponentIconsProps {
id?: string
/** The placeholder text when the select is empty. */
placeholder?: string
/** The placeholder text when the search input is empty. */
searchPlaceholder?: string
color?: SelectMenuVariants['color']
variant?: SelectMenuVariants['variant']
size?: SelectMenuVariants['size']
required?: boolean
/**
* The icon displayed to open the menu.
* @defaultValue `appConfig.ui.icons.chevronDown`
*/
trailingIcon?: string
/**
* The icon displayed when an item is selected.
* @defaultValue `appConfig.ui.icons.check`
*/
selectedIcon?: string
content?: Omit<ComboboxContentProps, 'asChild' | 'forceMount'>
arrow?: boolean | Omit<ComboboxArrowProps, 'asChild'>
portal?: boolean
/**
* Whether to filter items or not, can be an array of fields to filter.
* When `false`, items will not be filtered which is useful for custom filtering.
* @defaultValue `['label']`
*/
filter?: boolean | string[]
items?: T[] | T[][]
class?: any
ui?: Partial<typeof selectMenu.slots>
}
export type SelectMenuEmits<T> = ComboboxRootEmits<T>
type SlotProps<T> = (props: { item: T, index: number }) => any
export type SelectMenuSlots<T> = {
'leading'(props: { modelValue: T, open: boolean }): any
'default'(props: { modelValue: T, open: boolean }): any
'trailing'(props: { modelValue: T, open: boolean }): any
'empty'(props: { searchTerm?: string }): any
'item': SlotProps<T>
'item-leading': SlotProps<T>
'item-label': SlotProps<T>
'item-trailing': SlotProps<T>
}
</script>
<script setup lang="ts" generic="T extends SelectMenuItem | AcceptableValue">
import { computed, toRef } from 'vue'
import { ComboboxRoot, ComboboxAnchor, ComboboxInput, ComboboxTrigger, ComboboxPortal, ComboboxContent, ComboboxViewport, ComboboxEmpty, ComboboxGroup, ComboboxLabel, ComboboxSeparator, ComboboxItem, ComboboxItemIndicator, useForwardPropsEmits } from 'radix-vue'
import { defu } from 'defu'
import { reactivePick } from '@vueuse/core'
import { useAppConfig, useFormField, useButtonGroup, useComponentIcons } from '#imports'
import { UIcon, UChip, UAvatar } from '#components'
import { get } from '#ui/utils'
const props = withDefaults(defineProps<SelectMenuProps<T>>(), {
portal: true,
autofocusDelay: 0,
searchPlaceholder: 'Search...',
filter: () => ['label']
})
const emits = defineEmits<SelectMenuEmits<T>>()
const slots = defineSlots<SelectMenuSlots<T>>()
const searchTerm = defineModel<string>('searchTerm', { default: '' })
const appConfig = useAppConfig()
const rootProps = useForwardPropsEmits(reactivePick(props, 'as', 'modelValue', 'defaultValue', 'open', 'defaultOpen'), emits)
const contentProps = toRef(() => defu(props.content, { side: 'bottom', sideOffset: 8, position: 'popper' }) as ComboboxContentProps)
const { emitFormBlur, emitFormChange, size: formGroupSize, color, id, name, disabled } = useFormField<InputProps>(props)
const { orientation, size: buttonGroupSize } = useButtonGroup<InputProps>(props)
const { isLeading, isTrailing, leadingIconName, trailingIconName } = useComponentIcons(toRef(() => defu(props, { trailingIcon: appConfig.ui.icons.chevronDown })))
const inputSize = computed(() => buttonGroupSize.value || formGroupSize.value)
const ui = computed(() => tv({ extend: selectMenu, slots: props.ui })({
color: color.value,
variant: props.variant,
size: inputSize?.value,
loading: props.loading,
leading: isLeading.value || !!slots.leading,
trailing: isTrailing.value || !!slots.trailing,
buttonGroup: orientation.value
}))
function displayValue(val: AcceptableValue) {
if (typeof val === 'object') {
return val.label
}
return val && String(val)
}
function filterFunction(items: ArrayOrWrapped<AcceptableValue>, searchTerm: string): ArrayOrWrapped<AcceptableValue> {
if (props.filter === false) {
return items
}
const fields = Array.isArray(props.filter) ? props.filter : ['label']
return items.filter((item) => {
if (typeof item !== 'object') {
return String(item).search(new RegExp(searchTerm, 'i')) !== -1
}
return fields.some((field) => {
const child = get(item, field)
return child !== null && child !== undefined && String(child).search(new RegExp(searchTerm, 'i')) !== -1
})
}) as ArrayOrWrapped<T>
}
const groups = computed(() => props.items?.length ? (Array.isArray(props.items[0]) ? props.items : [props.items]) as SelectMenuItem[][] : [])
</script>
<template>
<ComboboxRoot
:id="id"
v-slot="{ modelValue, open }"
v-bind="rootProps"
v-model:search-term="searchTerm"
as-child
:name="name"
:disabled="disabled"
:display-value="displayValue"
:filter-function="filterFunction"
@update:model-value="emitFormChange()"
>
<ComboboxAnchor as-child>
<ComboboxTrigger :class="ui.base({ class: props.class })" tabindex="0">
<span v-if="isLeading || !!slots.leading" :class="ui.leading()">
<slot name="leading" :model-value="(modelValue as T)" :open="open">
<UIcon v-if="leadingIconName" :name="leadingIconName" :class="ui.leadingIcon()" />
</slot>
</span>
<slot :model-value="(modelValue as T)" :open="open">
<span v-if="displayValue(modelValue)" :class="ui.value()">
{{ displayValue(modelValue) }}
</span>
<span v-else :class="ui.placeholder()">
{{ placeholder ?? '&nbsp;' }}
</span>
</slot>
<span v-if="isTrailing || !!slots.trailing" :class="ui.trailing()">
<slot name="trailing" :model-value="(modelValue as T)" :open="open">
<UIcon v-if="trailingIconName" :name="trailingIconName" :class="ui.trailingIcon()" />
</slot>
</span>
</ComboboxTrigger>
</ComboboxAnchor>
<ComboboxPortal :disabled="!portal">
<ComboboxContent :class="ui.content()" v-bind="contentProps">
<ComboboxEmpty :class="ui.empty()">
<slot name="empty" :search-term="searchTerm">
{{ searchTerm ? `No results for ${searchTerm}` : 'No results' }}
</slot>
</ComboboxEmpty>
<ComboboxInput :placeholder="searchPlaceholder" :class="ui.input()" autofocus autocomplete="off" @blur="emitFormBlur()" />
<ComboboxViewport :class="ui.viewport()">
<ComboboxGroup v-for="(group, groupIndex) in groups" :key="`group-${groupIndex}`" :class="ui.group()">
<template v-for="(item, index) in group" :key="`group-${groupIndex}-${index}`">
<ComboboxLabel v-if="item?.type === 'label'" :class="ui.label()">
{{ item.label }}
</ComboboxLabel>
<ComboboxSeparator v-else-if="item?.type === 'separator'" :class="ui.separator()" />
<ComboboxItem v-else :class="ui.item()" :disabled="item.disabled" :value="item">
<slot name="item" :item="(item as T)" :index="index">
<slot name="item-leading" :item="(item as T)" :index="index">
<UAvatar v-if="item.avatar" size="2xs" v-bind="item.avatar" :class="ui.itemLeadingAvatar()" />
<UIcon v-else-if="item.icon" :name="item.icon" :class="ui.itemLeadingIcon()" />
<UChip
v-else-if="item.chip"
size="md"
inset
standalone
v-bind="item.chip"
:class="ui.itemLeadingChip()"
/>
</slot>
<span :class="ui.itemLabel()">
<slot name="item-label" :item="(item as T)" :index="index">
{{ displayValue(item as T) }}
</slot>
</span>
<span :class="ui.itemTrailing()">
<slot name="item-trailing" :item="(item as T)" :index="index" />
<ComboboxItemIndicator as-child>
<UIcon :name="selectedIcon || appConfig.ui.icons.check" :class="ui.itemTrailingSelectedIcon()" />
</ComboboxItemIndicator>
</span>
</slot>
</ComboboxItem>
</template>
</ComboboxGroup>
</ComboboxViewport>
</ComboboxContent>
</ComboboxPortal>
</ComboboxRoot>
</template>

View File

@@ -13,6 +13,7 @@ type TextareaVariants = VariantProps<typeof textarea>
export interface TextareaProps {
id?: string
name?: string
/** The placeholder text when the textarea is empty. */
placeholder?: string
color?: TextareaVariants['color']
variant?: TextareaVariants['variant']

View File

@@ -28,6 +28,7 @@ export * from '../components/Pagination.vue'
export * from '../components/Popover.vue'
export * from '../components/RadioGroup.vue'
export * from '../components/Select.vue'
export * from '../components/SelectMenu.vue'
export * from '../components/Separator.vue'
export * from '../components/Skeleton.vue'
export * from '../components/Slideover.vue'