Native Select(原生选择框)
应用样式并与设计系统保持一致的原生 HTML 选择元素。
如需使用自定义样式的选择器,请参阅 Select 组件。
loading
安装
bash
npx shadcn@latest add native-select1
1. 将以下代码复制并粘贴到项目中。
tsx
import * as React from "react"
import { cn } from "cn"
import { ChevronDownIcon } from "lucide-react"
type NativeSelectProps = Omit<React.ComponentProps<"select">, "size"> & {
size?: "sm" | "default"
}
function NativeSelect({
className,
size = "default",
...props
}: NativeSelectProps) {
return (
<div
className={cn(
"group/native-select relative w-fit has-[select:disabled]:opacity-50",
className
)}
data-slot="native-select-wrapper"
data-size={size}
>
<select
data-slot="native-select"
data-size={size}
className="h-9 w-full min-w-0 appearance-none rounded-4xl border border-input bg-transparent py-1 pe-8 ps-3 text-sm transition-colors outline-none select-none selection:bg-primary selection:text-primary-foreground placeholder:text-muted-foreground focus-visible:border-ring focus-visible:ring-[3px] focus-visible:ring-ring/50 disabled:pointer-events-none disabled:cursor-not-allowed aria-invalid:border-destructive aria-invalid:ring-[3px] aria-invalid:ring-destructive/20 data-[size=sm]:h-8 dark:bg-input/30 dark:hover:bg-input/50 dark:aria-invalid:border-destructive/50 dark:aria-invalid:ring-destructive/40"
{...props}
/>
<ChevronDownIcon className="pointer-events-none absolute top-1/2 end-3.5 size-4 -translate-y-1/2 text-muted-foreground select-none" aria-hidden="true" data-slot="native-select-icon" />
</div>
)
}
function NativeSelectOption({
className,
...props
}: React.ComponentProps<"option">) {
return (
<option
data-slot="native-select-option"
className={cn("bg-popover text-popover-foreground", className)}
{...props}
/>
)
}
function NativeSelectOptGroup({
className,
...props
}: React.ComponentProps<"optgroup">) {
return (
<optgroup
data-slot="native-select-optgroup"
className={cn("bg-popover text-popover-foreground", className)}
{...props}
/>
)
}
export { NativeSelect, NativeSelectOptGroup, NativeSelectOption }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
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
2. 根据项目配置调整导入路径。
用法
tsx
import {
NativeSelect,
NativeSelectOptGroup,
NativeSelectOption,
} from "@/components/ui/native-select"1
2
3
4
5
2
3
4
5
tsx
<NativeSelect>
<NativeSelectOption value="">Select a fruit</NativeSelectOption>
<NativeSelectOption value="apple">Apple</NativeSelectOption>
<NativeSelectOption value="banana">Banana</NativeSelectOption>
<NativeSelectOption value="blueberry">Blueberry</NativeSelectOption>
<NativeSelectOption value="pineapple">Pineapple</NativeSelectOption>
</NativeSelect>1
2
3
4
5
6
7
2
3
4
5
6
7
组件结构
简单
直接将选项放在 NativeSelect 下(无需使用 NativeSelectOptGroup)。
text
NativeSelect
├── NativeSelectOption
├── NativeSelectOption
├── NativeSelectOption
└── NativeSelectOption1
2
3
4
5
2
3
4
5
带分组
使用 NativeSelectOptGroup 按类别组织选项。
text
NativeSelect
├── NativeSelectOptGroup
│ ├── NativeSelectOption
│ └── NativeSelectOption
└── NativeSelectOptGroup
├── NativeSelectOption
└── NativeSelectOption1
2
3
4
5
6
7
2
3
4
5
6
7
分组
使用 NativeSelectOptGroup 按类别组织选项。
loading
禁用
在 NativeSelect 组件上添加 disabled 属性可禁用选择器。
loading
无效状态
使用 aria-invalid 显示校验错误,并在 Field 组件上添加 data-invalid 属性来设置样式。
loading
Native Select 与 Select 的区别
- 需要浏览器原生交互、更好的性能或针对移动设备优化的下拉列表时,使用
NativeSelect。 - 需要自定义样式、动画或复杂交互时,使用
Select。
RTL
要在 shadcn/ui 中启用 RTL 支持,请参阅 RTL 配置指南。
loading
API 参考
NativeSelect
包裹原生 HTML 选择元素的主组件。
tsx
<NativeSelect>
<NativeSelectOption value="option1">Option 1</NativeSelectOption>
<NativeSelectOption value="option2">Option 2</NativeSelectOption>
</NativeSelect>1
2
3
4
2
3
4
NativeSelectOption
表示选择器中的单个选项。
| 属性 | 类型 | 默认值 |
|---|---|---|
value | string | |
disabled | boolean | false |
NativeSelectOptGroup
将相关选项归为一组,便于组织。
| 属性 | 类型 | 默认值 |
|---|---|---|
label | string | |
disabled | boolean | false |
tsx
<NativeSelectOptGroup label="Fruits">
<NativeSelectOption value="apple">Apple</NativeSelectOption>
<NativeSelectOption value="banana">Banana</NativeSelectOption>
</NativeSelectOptGroup>1
2
3
4
2
3
4