2 Star 0 Fork 0

mirrors_skv-headless/react-select

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
MIT

React-Select

A Select control built with and for React. Initially built for use in KeystoneJS.

Demo & Examples

Live demo: jedwatson.github.io/react-select

To build the examples locally, run:

npm install
gulp dev

Then open localhost:8000 in a browser.

Project Status

This project is quite stable and ready for production use, however there are plans to improve it including:

  • CSS Styles and theme support (working, could be improved)
  • Documentation website (currently just examples)
  • Custom options rendering

It's loosely based on Selectize (in terms of behaviour and user experience) and React-Autocomplete (as a native React Combobox implemenation), as well as other select controls including Chosen and Select2.

Installation

The easiest way to use React-Select is to install it from NPM and include it in your own React build process (using Browserify, etc).

npm install react-select --save

You can also use the standalone build by including dist/select.js and dist/default.css in your page. If you use this, make sure you have already included the following dependencies:

Usage

React-Select generates a hidden text field containing the selected value, so you can submit it as part of a standard form. You can also listen for changes with the onChange event property.

Options should be provided as an Array of Objects, each with a value and label property for rendering and searching.

When the value is changed, onChange(newValue, [selectedOptions]) will fire.

var Select = require('react-select');

var options = [
	{ value: 'one', label: 'One' },
	{ value: 'two', label: 'Two' }
];

function logChange(val) {
	console.log("Selected: " + val);
}

<Select
	name="form-field-name"
	value="one"
	options={options}
	onChange={logChange}
/>

Multiselect options

You can enable multi-value selection by setting multi={true}. In this mode:

  • Selected options will be removed from the dropdown menu
  • The values of the selected items are joined using the delimiter property to create the input value
  • A simple value, if provided, will be split using the delimiter property
  • The onChange event provides an array of the selected options as the second argument

Async options

If you want to load options asynchronously, instead of providing an options Array, provide a asyncOptions Function.

The function takes two arguments String input, Function callbackand will be called when the input text is changed.

When your async process finishes getting the options, pass them to callback(err, data) in a Object { options: [] }.

The select control will intelligently cache options for input strings that have already been fetched. Async options will still be filtered like the normal options array, so if your async process would only return a smaller set of results for a more specific query, also pass complete: true in the callback object.

Unless you specify the property autoload={false} the control will automatically load the default set of options (i.e. for input: '') when it is mounted.

var Select = require('react-select');

var getOptions = function(input, callback) {
	setTimeout(function() {
		callback(null, {
			options: [
				{ value: 'one', label: 'One' },
				{ value: 'two', label: 'Two' }
			],
			// CAREFUL! Only set this to true when there are no more options,
			// or more specific queries will not be sent to the server.
			complete: true
		});
	}, 500);
};

<Select
	name="form-field-name"
	value="one"
	asyncOptions={getOptions}
/>

Filtering options

You can control how options are filtered with the following properties:

  • matchPos: "start" or "any": whether to match the text entered at the start or any position in the option value
  • matchProp: "label", "value" or "any": whether to match the value, label or both values of each option when filtering
  • ignoreCase: Boolean: whether to ignore case or match the text exactly when filtering

matchProp and matchPos both default to "any". ignoreCase defaults to true.

Advanced filters

You can also completely replace the method used to filter either a single option, or the entire options array (allowing custom sort mechanisms, etc.)

  • filterOption: function(Object option, String filter) returns Boolean. Will override matchPos, matchProp and ignoreCase options.
  • filterOptions: function(Array options, String filter, Array currentValues) returns Array filteredOptions. Will override filterOption, matchPos, matchProp and ignoreCase options.

For multi-select inputs, when providing a custom filterOptions method, remember to exclude current values from the returned array of options.

Further options

Property			|	Type		|	Description

:-----------------------|:--------------|:-------------------------------- value | any | initial field value multi | bool | multi-value input disabled | bool | whether the Select is disabled or not options | array | array of options delimiter | string | delimiter to use to join multiple values asyncOptions | func | function to call to get options autoload | bool | whether to auto-load the default async options set placeholder | string | field placeholder, displayed when there's no value noResultsText | string | placeholder displayed when there are no matching search results clearable | bool | should it be possible to reset value clearValueText | string | title for the "clear" control clearAllText | string | title for the "clear" control when multi: true searchable | bool | whether to enable searching feature or not searchPromptText | string | label to prompt for search input name | string | field name, for hidden tag onChange | func | onChange handler: function(newValue) {} onFocus | func | onFocus handler: function(event) {} onBlur | func | onBlur handler: function(event) {} className | string | className for the outer element filterOption | func | method to filter a single option: function(option, filterString) filterOptions | func | method to filter the options array: function([options], filterString, [values]) matchPos | string | (any, start) match the start or entire string when filtering matchProp | string | (any, label, value) which option property to filter on ignoreCase | bool | whether to perform case-insensitive filtering inputProps | object | custom attributes for the Input (in the Select-control) e.g: {'data-foo': 'bar'}

Contributing

See our CONTRIBUTING.md for information on how to contribute.

License

MIT Licensed. Copyright (c) Jed Watson 2015.

The MIT License (MIT) Copyright (c) 2015 Jed Watson Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

简介

A Select control built with and for React JS 展开 收起
README
MIT
取消

发行版

暂无发行版

近期动态

接近5年前创建了仓库
不能加载更多了
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/mirrors_skv-headless/react-select.git
git@gitee.com:mirrors_skv-headless/react-select.git
mirrors_skv-headless
react-select
react-select
master

搜索帮助