Menampilkan daftar pilihan untuk dipilih pengguna, dibuka lewat sebuah tombol.
Installation
pnpm dlx kliping@latest add selectPasang dependensi berikut:
pnpm add reka-uiSalin dan tempel kode sumber di GitHub ke project Anda.
Sesuaikan path import dengan struktur project Anda.
Usage
<script setup lang="ts">
import {
Select,
SelectContent,
SelectItem,
SelectTrigger,
SelectValue,
} from '@/components/ui/select'
</script>
<template>
<Select>
<SelectTrigger>
<SelectValue placeholder="Select a fruit" />
</SelectTrigger>
<SelectContent>
<SelectItem value="apple">
Apple
</SelectItem>
<SelectItem value="banana">
Banana
</SelectItem>
<SelectItem value="blueberry">
Blueberry
</SelectItem>
<SelectItem value="grapes">
Grapes
</SelectItem>
<SelectItem value="pineapple">
Pineapple
</SelectItem>
</SelectContent>
</Select>
</template>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
Examples
Scrollable
Pilihan Ganda
Requirements
Semua dipasang otomatis oleh kliping add; tabel ini untuk Anda yang memasang manual atau memeriksa project.
| Requirement | Value |
|---|---|
| Project | Vue 3.5+, Tailwind CSS v4, alias @ — disiapkan kliping init |
| npm packages | @lucide/vue@^1.31.0, @vueuse/core@^14.4.0, reka-ui@^2.10.3 |
| Files | components/ui/select/select.vuecomponents/ui/select/select-content.vuecomponents/ui/select/select-group.vuecomponents/ui/select/select-item.vuecomponents/ui/select/select-item-text.vuecomponents/ui/select/select-label.vuecomponents/ui/select/select-scroll-down-button.vuecomponents/ui/select/select-scroll-up-button.vuecomponents/ui/select/select-separator.vuecomponents/ui/select/select-trigger.vuecomponents/ui/select/select-value.vuecomponents/ui/select/index.ts |
Anatomy
Susunan bagiannya seperti dipakai di contoh-contoh di atas. Impor semua bagian dari satu tempat, lalu susun begini:
<Select>
<SelectTrigger>
<SelectValue />
</SelectTrigger>
<SelectContent>
<SelectItem />
<SelectLabel />
<SelectGroup>
<SelectItem />
<SelectLabel />
</SelectGroup>
<SelectSeparator />
</SelectContent>
</Select>Bagian lain yang tersedia: SelectItemText, SelectScrollDownButton, SelectScrollUpButton.
API Reference
Select
| Prop | Type | Default | Description |
|---|---|---|---|
open? | boolean | — | The controlled open state of the Select. Can be bind as v-model:open. |
defaultOpen? | boolean | — | The open state of the select when it is initially rendered. Use when you do not need to control its open state. |
defaultValue? | AcceptableValue | AcceptableValue[] | — | The value of the select when initially rendered. Use when you do not need to control the state of the Select |
modelValue? | AcceptableValue | AcceptableValue[] | — | The controlled value of the Select. Can be bind as v-model. |
nullableValue? | string | — | The value of the hidden native select option when the model value is nullish. |
by? | string | ((a: AcceptableValue, b: AcceptableValue) => boolean) | — | Use this to compare objects by a particular field, or pass your own comparison function for complete control over how objects are compared. |
dir? | Direction | — | The reading direction of the combobox when applicable. If omitted, inherits globally from ConfigProvider or assumes LTR (left-to-right) reading mode. |
multiple? | boolean | — | Whether multiple options can be selected or not. |
autocomplete? | string | — | Native html input autocomplete attribute. |
disabled? | boolean | — | When true, prevents the user from interacting with Select |
name? | string | — | The name of the field. Submitted with its owning form as part of a name/value pair. |
required? | boolean | — | When true, indicates that the user must set the value before the owning form can be submitted. |
| Event | Payload | Description |
|---|---|---|
@update:modelValue | [value: AcceptableValue] | Emitted when the value changes. |
@update:open | [value: boolean] | Emitted when the open state changes. |
| Slot | Props |
|---|---|
#default | { modelValue: string | number | bigint | Record<string, any> | (string | number | bigint | Record<string, any> | null)[] | null | undefined; open: boolean; } |
| Data attribute | Value |
|---|---|
[data-slot] | "select" |
SelectContent
| Prop | Type | Default | Description |
|---|---|---|---|
forceMount? | boolean | — | Used to force mounting when more control is needed. Useful when controlling animation with Vue animation libraries. |
position? | "popper" | "item-aligned" | "popper" | The positioning mode to use item-aligned (default) - behaves similarly to a native MacOS menu by positioning content relative to the active item. popper - positions content in the same way as our other primitives, for example Popover or DropdownMenu. |
bodyLock? | boolean | — | The document.body will be lock, and scrolling will be disabled. |
memoDependencies? | unknown[] | — | Reactive dependencies that should invalidate the memoized content subtree. |
side? | "bottom" | "top" | "right" | "left" | — | The preferred side of the trigger to render against when open. Will be reversed when collisions occur and avoidCollisions is enabled. |
sideOffset? | number | — | The distance in pixels from the trigger. |
sideFlip? | boolean | — | Flip to the opposite side when colliding with boundary. |
align? | "start" | "end" | "center" | — | The preferred alignment against the trigger. May change when collisions occur. |
alignOffset? | number | — | An offset in pixels from the start or end alignment options. |
alignFlip? | boolean | — | Flip alignment when colliding with boundary. May only occur when prioritizePosition is true. |
avoidCollisions? | boolean | — | When true, overrides the side and align preferences to prevent collisions with boundary edges. |
collisionBoundary? | Element | (Element | null)[] | — | The element used as the collision boundary. By default this is the viewport, though you can provide additional element(s) to be included in this check. |
collisionPadding? | number | Partial<Record<"bottom" | "top" | "right" | "left", number>> | — | The distance in pixels from the boundary edges where collision detection should occur. Accepts a number (same for all sides), or a partial padding object, for example: { top: 20, left: 20 }. |
arrowPadding? | number | — | The padding between the arrow and the edges of the content. If your content has border-radius, this will prevent it from overflowing the corners. |
hideShiftedArrow? | boolean | — | When true, hides the arrow when it cannot be centered to the reference element. |
sticky? | "partial" | "always" | — | The sticky behavior on the align axis. partial will keep the content in the boundary as long as the trigger is at least partially in the boundary whilst "always" will keep the content in the boundary regardless. |
hideWhenDetached? | boolean | — | Whether to hide the content when the trigger becomes fully occluded. |
positionStrategy? | "fixed" | "absolute" | — | The type of CSS position property to use. |
updatePositionStrategy? | "always" | "optimized" | — | Strategy to update the position of the floating element on every animation frame. |
disableUpdateOnLayoutShift? | boolean | — | Whether to disable the update position for the content when the layout shifted. |
prioritizePosition? | boolean | — | Force content to be position within the viewport. Might overlap the reference element, which may not be desired. |
reference? | ReferenceElement | — | The custom element or virtual element that will be set as the reference to position the floating element. If provided, it will replace the default anchor element. |
dir? | Direction | — | The reading direction of the popper content when applicable. If omitted, inherits globally from ConfigProvider or assumes LTR (left-to-right) reading mode. |
asChild? | boolean | — | Change the default rendered element for the one passed as a child, merging their props and behavior. Read our Composition guide for more details. |
as? | AsTag | Component | — | The element or component this component should render as. Can be overwritten by asChild. |
disableOutsidePointerEvents? | boolean | — | When true, hover/focus/click interactions will be disabled on elements outside the DismissableLayer. Users will need to click twice on outside elements to interact with them: once to close the DismissableLayer, and again to trigger the element. |
| Event | Payload | Description |
|---|---|---|
@escapeKeyDown | [event: KeyboardEvent] | Emitted when Escape is pressed; call event.preventDefault() to keep it open. |
@pointerDownOutside | [event: PointerDownOutsideEvent] | Emitted on a pointer down outside; call event.preventDefault() to keep it open. |
@closeAutoFocus | [event: Event] | Emitted when focus returns on close; call event.preventDefault() to stop it. |
| Slot | Props |
|---|---|
#default | — |
| Data attribute | Value |
|---|---|
[data-slot] | "select-content" |
SelectGroup
| Prop | Type | Default | Description |
|---|---|---|---|
asChild? | boolean | — | Change the default rendered element for the one passed as a child, merging their props and behavior. Read our Composition guide for more details. |
as? | AsTag | Component | — | The element or component this component should render as. Can be overwritten by asChild. |
| Slot | Props |
|---|---|
#default | — |
| Data attribute | Value |
|---|---|
[data-slot] | "select-group" |
SelectItem
| Prop | Type | Default | Description |
|---|---|---|---|
value | AcceptableValue | — | The value given as data when submitted with a name. |
disabled? | boolean | — | When true, prevents the user from interacting with the item. |
textValue? | string | — | Optional text used for typeahead purposes. By default the typeahead behavior will use the .textContent of the SelectItemText part. Use this when the content is complex, or you have non-textual content inside. |
asChild? | boolean | — | Change the default rendered element for the one passed as a child, merging their props and behavior. Read our Composition guide for more details. |
as? | AsTag | Component | — | The element or component this component should render as. Can be overwritten by asChild. |
| Slot | Props |
|---|---|
#indicator-icon | — |
#default | — |
| Data attribute | Value |
|---|---|
[data-slot] | "select-item" |
SelectItemText
| Prop | Type | Default | Description |
|---|---|---|---|
asChild? | boolean | — | Change the default rendered element for the one passed as a child, merging their props and behavior. Read our Composition guide for more details. |
as? | AsTag | Component | — | The element or component this component should render as. Can be overwritten by asChild. |
| Slot | Props |
|---|---|
#default | — |
| Data attribute | Value |
|---|---|
[data-slot] | "select-item-text" |
SelectLabel
| Prop | Type | Default | Description |
|---|---|---|---|
for? | string | — | Id of the control this label names. |
asChild? | boolean | — | Change the default rendered element for the one passed as a child, merging their props and behavior. Read our Composition guide for more details. |
as? | AsTag | Component | — | The element or component this component should render as. Can be overwritten by asChild. |
| Slot | Props |
|---|---|
#default | — |
| Data attribute | Value |
|---|---|
[data-slot] | "select-label" |
SelectScrollDownButton
| Prop | Type | Default | Description |
|---|---|---|---|
asChild? | boolean | — | Change the default rendered element for the one passed as a child, merging their props and behavior. Read our Composition guide for more details. |
as? | AsTag | Component | — | The element or component this component should render as. Can be overwritten by asChild. |
| Slot | Props |
|---|---|
#default | — |
| Data attribute | Value |
|---|---|
[data-slot] | "select-scroll-down-button" |
SelectScrollUpButton
| Prop | Type | Default | Description |
|---|---|---|---|
asChild? | boolean | — | Change the default rendered element for the one passed as a child, merging their props and behavior. Read our Composition guide for more details. |
as? | AsTag | Component | — | The element or component this component should render as. Can be overwritten by asChild. |
| Slot | Props |
|---|---|
#default | — |
| Data attribute | Value |
|---|---|
[data-slot] | "select-scroll-up-button" |
SelectSeparator
| Prop | Type | Default | Description |
|---|---|---|---|
asChild? | boolean | — | Change the default rendered element for the one passed as a child, merging their props and behavior. Read our Composition guide for more details. |
as? | AsTag | Component | — | The element or component this component should render as. Can be overwritten by asChild. |
| Data attribute | Value |
|---|---|
[data-slot] | "select-separator" |
SelectTrigger
| Prop | Type | Default | Description |
|---|---|---|---|
disabled? | boolean | — | Prevents the user from interacting with it. |
reference? | ReferenceElement | — | The reference (or anchor) element that is being referred to for positioning. If not provided will use the current component as anchor. |
asChild? | boolean | — | Change the default rendered element for the one passed as a child, merging their props and behavior. Read our Composition guide for more details. |
as? | AsTag | Component | — | The element or component this component should render as. Can be overwritten by asChild. |
size? | "default" | "sm" | "default" | The visual size. |
| Slot | Props |
|---|---|
#default | — |
| Data attribute | Value |
|---|---|
[data-slot] | "select-trigger" |
[data-size] | "default" | "sm" |
SelectValue
| Prop | Type | Default | Description |
|---|---|---|---|
placeholder? | string | — | The content that will be rendered inside the SelectValue when no value or defaultValue is set. |
asChild? | boolean | — | Change the default rendered element for the one passed as a child, merging their props and behavior. Read our Composition guide for more details. |
as? | AsTag | Component | — | The element or component this component should render as. Can be overwritten by asChild. |
| Slot | Props |
|---|---|
#default | — |
| Data attribute | Value |
|---|---|
[data-slot] | "select-value" |
Keyboard Interactions
Perilaku keyboard-nya datang dari primitif Reka UI Select yang dipakai komponen ini, mengikuti pola WAI-ARIA.
| Key | Description |
|---|---|
Space | When focus is on SelectTrigger, opens the select and focuses the selected item. When focus is on an item, selects the focused item. |
Enter | When focus is on SelectTrigger, opens the select and focuses the first item. When focus is on an item, selects the focused item. |
ArrowDown | When focus is on SelectTrigger, opens the Select When focus is on an item, moves focus to the next item. |
ArrowUp | When focus is on SelectTrigger, opens the Select When focus is on an item, moves focus to the previous item. |
Esc | Closes the select and moves focus to SelectTrigger. |
CSS Variables
Token tema yang dibaca komponen ini — ubah nilainya di CSS global (:root dan .dark, lihat Theming) untuk mengubah tampilannya tanpa menyentuh komponennya: --accent, --accent-foreground, --border, --destructive, --input, --muted-foreground, --popover, --popover-foreground, --radius, --ring.
Credits
Berasal dari shadcn-vue (MIT, Copyright (c) 2023 unovue). Rantai lengkapnya di Credits.