@chakra-ui/select

中文文档
2.1.2发布于 2023/11/09MITTypeScriptESM + CJS近 30 天下载 127.8万

description

原始使用文档
文档目录12 节
  1. Installation
  2. Import component
  3. Usage
  4. States
  5. Disabled
  6. Invalid
  7. Variants
  8. Sizes
  9. Controlled Select
  10. Changing the icon in the Select
  11. Focus border color and error border color
  12. Overriding the Select styles

Select

The Select component is a component that allows users pick a value from predefined options.

Ideally, it should be used when there are more than 5 options, otherwise you might consider using a radio group instead.

Installation

yarn add @chakra-ui/select

# or

npm i @chakra-ui/select

Import component

import { Select } from "@chakra-ui/select"

Usage

<Select placeholder="A simple select component">
  <option value="1">Option 1</option>
  <option value="2">Option 2</option>
  <option value="3">Option 3</option>
</Select>

States

Disabled

Pass the isDisabled prop to put the select component in an invalid state

<Select placeholder="Select option" isDisabled>
  <option value="Option 1">Option 1</option>
  <option value="Option 2">Option 2</option>
  <option value="Option 3">Option 3</option>
</Select>

Invalid

Pass the isInvalid prop to put the select component in an invalid state

<Select placeholder="Select option" isInvalid>
  <option value="Option 1">Option 1</option>
  <option value="Option 2">Option 2</option>
  <option value="Option 3">Option 3</option>
</Select>

Variants

Control the visual appearance of the select component by passing the variant prop.

The following values are allowed: outline, filled, flushed, unstyled

<Stack>
  <Select placeholder="Select option" variant="outline">
    <option value="Option 1">Option 1</option>
    <option value="Option 2">Option 2</option>
    <option value="Option 3">Option 3</option>
  </Select>

  <Select placeholder="Select option" variant="filled">
    <option value="Option 1">Option 1</option>
    <option value="Option 2">Option 2</option>
    <option value="Option 3">Option 3</option>
  </Select>

  <Select placeholder="Select option" variant="flushed">
    <option value="Option 1">Option 1</option>
    <option value="Option 2">Option 2</option>
    <option value="Option 3">Option 3</option>
  </Select>

  <Select placeholder="Select option" variant="unstyled">
    <option value="Option 1">Option 1</option>
    <option value="Option 2">Option 2</option>
    <option value="Option 3">Option 3</option>
  </Select>
</Stack>

Sizes

Pass the size prop to change the size and height of the select component.

The following values are allowed: sm, md, lg

<Stack spacing={4}>
  {["sm", "md", "lg"].map((size) => (
    <Select key={size} placeholder="Select option" size={size}>
      <option value="Option 1">Option 1</option>
      <option value="Option 2">Option 2</option>
      <option value="Option 3">Option 3</option>
    </Select>
  ))}
</Stack>

Controlled Select

const ControlledSelectExample = () => {
  const [value, setValue] = React.useState("")
  const handleChange = (event: React.ChangeEvent<HTMLSelectElement>) => {
    setValue(event.target.value)
  }

  return (
    <Select
      value={value}
      onChange={handleChange}
      placeholder="Controlled select"
    >
      <option value="Option 1">Option 1</option>
      <option value="Option 2">Option 2</option>
      <option value="Option 3">Option 3</option>
    </Select>
  )
}

Changing the icon in the Select

Pass the icon prop to change the arrow icon of the select component to a custom icon.

You also have access to the iconSize prop to change the size of the custom arrow icon.

const CustomSelectIconExample = () => {
  const SelectIcon = () => (
    <Icon viewBox="0 0 24 24">
      <path
        fill="currentColor"
        d="M20 12l-1.41-1.41L13 16.17V4h-2v12.17l-5.58-5.59L4 12l8 8 8-8z"
      />
    </Icon>
  )
  return <Select icon={SelectIcon} placeholder="Placeholder" size="md" />
}

Focus border color and error border color

Pass the focusBorderColor prop to change the border color of the select component in the focused state.

Pass the errorBorderColor prop to change the border color of the select component in the invalid state.

The value of these props can be set to a color in the theme object, or a raw CSS value.

<Stack>
  <Select focusBorderColor="lime" placeholder="Here is a sample placeholder" />

  <Select
    isInvalid
    errorBorderColor="crimson"
    placeholder="Here is a sample placeholder"
  />
</Stack>

Overriding the Select styles

Even though the select comes with predefined styles, you can override pretty much any property. Here's we'll override the background color.

<Select
  color="white"
  borderColor="tomato"
  backgroundColor="tomato"
  placeholder="Woohoo! A new background color!"
/>

版本与发布记录

@chakra-ui/select 共发布 490 个版本,首次发布于 2020/06/21;过去 12 个月发布了 0 个版本。

版本发布日期距今
2.1.2latest2023/11/092 年前
0.0.0-dev-20231109134954dev2023/11/092 年前
0.0.0-dev-202311091309492023/11/092 年前
0.0.0-dev-202311091255552023/11/092 年前
0.0.0-dev-202311091125242023/11/092 年前
2.1.12023/09/093 年前

其他发布通道:canary → 0.0.0-canary-2021118164051,pr → 0.0.0-pr-202212011232,next → 2.0.0-next.3,next-dev → 0.0.0-next-dev-202241614737,dev → 0.0.0-dev-20231109134954。

关于 @chakra-ui/select 的常见问题

@chakra-ui/select 是什么?

description

如何安装 @chakra-ui/select?

在项目目录运行 npm install @chakra-ui/select,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:react(>=18)、@chakra-ui/system(>=2.0.0)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@chakra-ui/select 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@chakra-ui/select 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@chakra-ui/select 可以商用吗?

@chakra-ui/select 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@chakra-ui/select 的下载量有多少?

根据 npm 官方下载统计,@chakra-ui/select 在 2026/10/10 – 2026/11/08 这 30 天内被下载 1,278,440 次,日均约 42,615 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@chakra-ui/select 还在维护吗?

最新版本 2.1.2 发布于 2023/11/09(2 年前);至今共发布 490 个版本,其中过去 12 个月发布了 0 个;首次发布于 2020/06/21。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。