react-select README

The project's README from github.com/JedWatson/react-select. As one markdown file: README.md.

NPM CircleCI Coverage Status Supported by Thinkmill

React-Select

The Select control for React. Initially built for use in KeystoneJS.

See react-select.com for live demos and comprehensive docs.

react-select is funded by Thinkmill and Atlassian. We are an open source project that is continuously supported by the community.

React Select helps you develop powerful select components that just work out of the box, without stopping you from customising the parts that are important to you.

For the story behind this component, watch Jed's talk at React Conf 2019 - building React Select

Features include:

Using an older version?

Installation and usage

The easiest way to use react-select is to install it from npm and build it into your app with Webpack.

yarn add react-select

Then use it in your app:

import React, { useState } from 'react';
import Select from 'react-select';

const options = [
  { value: 'chocolate', label: 'Chocolate' },
  { value: 'strawberry', label: 'Strawberry' },
  { value: 'vanilla', label: 'Vanilla' },
];

export default function App() {
  const [selectedOption, setSelectedOption] = useState(null);

  return (
    <div className="App">
      <Select
        defaultValue={selectedOption}
        onChange={setSelectedOption}
        options={options}
      />
    </div>
  );
}

Props

Common props you may want to specify include:

See the props documentation for complete documentation on the props react-select supports.

Controllable Props

You can control the following props by providing values for them. If you don't, react-select will manage them for you.

If you don't provide these props, you can set the initial value of the state they control:

Methods

React-select exposes two public methods:

Customisation

Check the docs for more information on:

TypeScript

The v5 release represents a rewrite from JavaScript to TypeScript. The types for v4 and earlier releases are available at @types. See the TypeScript guide for how to use the types starting with v5.

Thanks

Thank you to everyone who has contributed to this project. It's been a wild ride.

If you like React Select, you should follow me on Twitter!

Shout out to Joss Mackison, Charles Lee, Ben Conolly, Tom Walker, Nathan Bierema, Eric Bonow, Emma Hamilton, Dave Brotherstone, Brian Vaughn, and the Atlassian Design System team who along with many other contributors have made this possible ❤️

License

MIT Licensed. Copyright (c) Jed Watson 2022.

react-select (JedWatson/react-select)

Searching Database…

这是什么?React Select 是一个灵活、可定制的下拉选择(select)组件,适用于 React 应用,最初是为 KeystoneJS 开发的。
这是 UI 组件库还是完整的框架?它是一个单独的 UI 组件库,可以直接放进 React 应用中使用,而不是框架。它以 npm 包的形式发布,你导入后再渲染即可。
怎样把它添加到项目中?从 npm 安装,然后用 Webpack(或你使用的其他构建工具)打包,接着在应用中导入并渲染 Select 组件即可。
除了 React 本身,还需要别的东西吗?不需要特殊的后端或 API 密钥。它是一个客户端组件,不过在底层,样式定制会用到 Emotion CSS 库。
如果升级,我之前 v1/v2 的代码还能用吗?不能。大版本之间存在破坏性变更,而且 v5 是从 JavaScript 到 TypeScript 的完整重写,因此旧版文档和类型定义是分开保存的。
主要有哪些定制选项?你可以用函数自定义数据处理,通过基于 Emotion 的样式 API 设置所有样式,借助组件注入(Component Injection)API 替换为自定义组件,还可以用 value、menuIsOpen、inputValue 等可控属性来管理状态。
支持多选和搜索吗?支持。通过 isMulti 属性可以选择多个值,通过 isSearchable 属性可以让用户输入文字来筛选选项。
选项可以异步加载吗?可以。文档介绍了如何创建异步选择框来动态加载选项,同时还支持选项分组、Portal 和动画过渡效果。
支持 TypeScript 吗?支持。从 v5 开始,整个库都用 TypeScript 编写,并提供了专门的 TypeScript 指南;更早的版本则需要单独安装 @types 包。
Creator
JedWatson
Size
18.9 MB
Stars
28K
Age
12y
Commits · 90 Days