Skip to Content
文档

选择(原生)

用于从预定义选项中选择一个值。

SourceStorybookRecipe

用法

¥Usage

import { NativeSelect } from "@chakra-ui/react"
<NativeSelect.Root>
  <NativeSelect.Field>
    <option value="1">Option 1</option>
    <option value="2">Option 2</option>
  </NativeSelect.Field>
  <NativeSelect.Indicator />
</NativeSelect.Root>
info

如果你更喜欢封闭的组件组合,请查看 以下代码片段

¥If you prefer a closed component composition, check out the snippet below.

示例

¥Examples

尺寸

¥Sizes

使用 size 属性更改选择组件的大小。

¥Use the size prop to change the size of the select component.

变量

¥Variants

使用 variant 属性更改选择组件的外观。

¥Use the variant prop to change the appearance of the select component.

受控

¥Controlled

使用 valueonChange 属性控制选择组件。

¥Use the value and onChange props to control the select component.

已禁用

¥Disabled

disabled 属性传递给 NativeSelect.Root 组件,即可禁用选择组件。

¥Pass the disabled prop to the NativeSelect.Root component to disable the select component.

无效

¥Invalid

编写原生组件和 Field 组件,设置无效集并显示错误文本。

¥Compose the native and Field component to set the invalid set and show the error text.

This is an error

或者,你也可以在 NativeSelect.Root 组件上使用 invalid 属性。

¥Alternatively, you can use the invalid prop on the NativeSelect.Root component as well.

钩子表单

¥Hook Form

以下是如何将 NativeSelect 组件与 react-hook-form 一起使用的示例。

¥Here is an example of how to use the NativeSelect component with react-hook-form.

参考

¥Ref

以下是如何访问底层元素引用的示例

¥Here's how to access the underlying element reference

import { NativeSelect } from "@chakra-ui/react"

const Demo = () => {
  const ref = useRef<HTMLSelectElement | null>(null)
  return (
    <NativeSelect.Root>
      <NativeSelect.Field ref={ref}>
        <option value="1">Option 1</option>
        <option value="2">Option 2</option>
      </NativeSelect.Field>
      <NativeSelect.Indicator />
    </NativeSelect.Root>
  )
}

已关闭组件

¥Closed Component

以下是如何设置封闭组件组合的原生选择。

¥Here's how to setup the Native Select for a closed component composition.

"use client"

import { NativeSelect as Select } from "@chakra-ui/react"
import * as React from "react"

type FieldProp = "name" | "value" | "onChange" | "defaultValue"

interface NativeSelectProps
  extends Omit<Select.RootProps, FieldProp>,
    Pick<Select.FieldProps, FieldProp> {
  icon?: React.ReactNode
  items: Array<{ label: string; value: string; disabled?: boolean }>
}

export const NativeSelect = React.forwardRef<
  HTMLSelectElement,
  NativeSelectProps
>(function NativeSelect(props, ref) {
  const {
    icon,
    children,
    name,
    items,
    value,
    defaultValue,
    onChange,
    ...rest
  } = props
  return (
    <Select.Root {...rest}>
      <Select.Field
        ref={ref}
        name={name}
        value={value}
        defaultValue={defaultValue}
        onChange={onChange}
      >
        {children}
        {items?.map((item) => (
          <option key={item.value} value={item.value} disabled={item.disabled}>
            {item.label}
          </option>
        ))}
      </Select.Field>
      <Select.Indicator>{icon}</Select.Indicator>
    </Select.Root>
  )
})

如果你想自动将封闭式组件添加到项目中,请运行以下命令:

¥If you want to automatically add the closed component to your project, run the command:

npx @chakra-ui/cli snippet add native-select

属性

¥Props

根元素

¥Root

PropDefaultType
colorPalette 'gray'
'gray' | 'red' | 'orange' | 'yellow' | 'green' | 'teal' | 'blue' | 'cyan' | 'purple' | 'pink'

The color palette of the component

variant 'outline'
'outline' | 'subtle' | 'plain'

The variant of the component

size 'md'
'xs' | 'sm' | 'md' | 'lg' | 'xl'

The size of the component

disabled
boolean | undefined

invalid
boolean | undefined

as
React.ElementType

The underlying element to render.

asChild
boolean

Use the provided child element as the default rendered element, combining their props and behavior.

For more details, read our Composition guide.
unstyled
boolean

Whether to remove the component's style.

Previous

分段控件

Next

开关