49 lines
2.1 KiB
Vue
49 lines
2.1 KiB
Vue
<script setup lang="ts">
|
|
import { ChevronDownIcon } from '@lucide/vue'
|
|
import type { HTMLAttributes } from 'vue'
|
|
import { inject } from 'vue'
|
|
import { cn } from '@/lib/utils'
|
|
import { SelectContextKey } from './context'
|
|
|
|
const props = withDefaults(
|
|
defineProps<{ class?: HTMLAttributes['class'], size?: 'sm' | 'default', disabled?: boolean }>(),
|
|
{ size: 'default' },
|
|
)
|
|
|
|
const context = inject(SelectContextKey)!
|
|
|
|
function onClick() {
|
|
if (props.disabled || context.disabled.value) return
|
|
context.open.value = !context.open.value
|
|
}
|
|
|
|
function onKeydown(event: KeyboardEvent) {
|
|
if (event.key === 'Enter' || event.key === ' ' || event.key === 'ArrowDown' || event.key === 'ArrowUp') {
|
|
event.preventDefault()
|
|
context.open.value = true
|
|
}
|
|
}
|
|
</script>
|
|
|
|
<template>
|
|
<button
|
|
:ref="(el) => { context.triggerRef.value = el as HTMLElement | null }"
|
|
type="button"
|
|
data-slot="select-trigger"
|
|
:data-size="size"
|
|
role="combobox"
|
|
:aria-controls="context.contentId"
|
|
:aria-expanded="context.open.value"
|
|
aria-autocomplete="none"
|
|
:disabled="disabled || context.disabled.value"
|
|
:class="cn(
|
|
'border-input data-placeholder:text-muted-foreground dark:bg-input/30 dark:hover:bg-input/50 focus-visible:border-ring focus-visible:ring-ring/50 aria-invalid:ring-destructive/20 dark:aria-invalid:ring-destructive/40 aria-invalid:border-destructive dark:aria-invalid:border-destructive/50 gap-1.5 rounded-lg border bg-transparent py-2 pr-2 pl-2.5 text-sm transition-colors select-none focus-visible:ring-3 aria-invalid:ring-3 data-[size=default]:h-8 data-[size=sm]:h-7 data-[size=sm]:rounded-[min(var(--radius-md),10px)] *:data-[slot=select-value]:gap-1.5 [&_svg:not([class*=size-])]:size-4 flex w-fit items-center justify-between whitespace-nowrap outline-none disabled:cursor-not-allowed disabled:opacity-50 *:data-[slot=select-value]:line-clamp-1 *:data-[slot=select-value]:flex *:data-[slot=select-value]:items-center [&_svg]:pointer-events-none [&_svg]:shrink-0',
|
|
props.class,
|
|
)"
|
|
@click="onClick"
|
|
@keydown="onKeydown"
|
|
>
|
|
<slot />
|
|
<ChevronDownIcon class="text-muted-foreground size-4 pointer-events-none" />
|
|
</button>
|
|
</template>
|