Select 选择器
如果项目中使用的是 1.x 版本的基础组件(@alifd/next),请在左侧导航顶部切换组件版本。
安装方法
- 在命令行中执行以下命令
npm install @icedesign/base@latest -S
Guide
何时使用
Select
如果你不期望用户输入的值生效而仅仅是选择,那么使用Select. 同时可以使用Select的showSearch属性进行过滤。
Combobox
唯一和Select不一致的点在于Combobox会保留用户输入的值。
常见问题
出现类似的
Select默认使用value
作为菜单项的key,如果没有设置key值,则使用默认的序列index作为key值,确保这些值不会发生重复。
Combobox与Field配合使用
由于Field
会让组件变成受控组件,所以如果你发现你的输入框无法输入值,记得监听下onInputUpdate
,然后调用Field
的setValue
方法进行赋值。
伪代码如下
this.field.setValue(name, value);
dataSource的使用
Select同时支持children和在props中传入dataSource作为数据源,如果同时设置,则以children为准.
注意: Select默认使用value
作为渲染的菜单项的key
值,所以value
不能重复, 否则无法渲染下拉菜单
children
的方式
<Select>
<Select.Option value="option1">option1</Select.Option>
<Select.Option value="option2">option2</Select.Option>
<Select.Option disabled>disabled</Select.Option>
</Select>
props
的方式
const dataSource = [
{label:'option1', value:'option1'},
{label:'option2', value:'option2'},
{label:'disabled', disabled:true}
]
<Select dataSource={dataSource}/>
定制弹出层
参见示例中的弹层定制
。唯一需要注意的是overlay
的元素记得透传props.这是因为Overlay的弹层的动画是依靠className
实现的,如果不透传props则会造成无法监听到动画播放结束的事件。
API
选择器
参数 | 说明 | 类型 | 默认值 |
---|---|---|---|
prefix | 样式类名的品牌前缀 | String | 'next-' |
className | 自定义类名 | String | - |
style | 自定义内联样式 | Object | - |
container | 弹层挂载容器节点 | String/Function | - |
size | 选择器的尺寸可选值:'small', 'medium', 'large' | Enum | 'medium' |
shape | 选择器的形状可选值:'normal', 'arrow-only' | Enum | 'normal' |
placeholder | 没有值的时候的占位符 | String | '' |
value | 当前值 | String/Array/ReactNode/Object | - |
defaultValue | 初始默认值 | String/Array/ReactNode/Object | - |
visible | 当前弹层是否显示 | Boolean | - |
defaultVisible | 弹层初始是否显示 | Boolean | - |
disabled | 是否禁用 | Boolean | false |
dataSource | 传入的数据,可以动态渲染子项,详见dataSource的使用 | Array | - |
multiple | 是否是多选 | Boolean | false |
fillProps | 填充到选择框里面的值 | String | 'label' |
showSearch | 是否显示顶部的搜索框 | Boolean | false |
filterBy | 在输入的时候过滤的函数,仅在filterLocal为true时候有效签名:Function(filterValue: String, option: Object) => Boolean参数:filterValue: {String} 筛选值option: {Object} 选项返回值:{Boolean} 是否保留当前选项 | Function | - |
filterLocal | 是否使用本地过滤,在数据源为远程的时候需要关闭此选项 | Boolean | true |
hasArrow | 是否显示右侧的箭头 | Boolean | true |
autoWidth | 下拉菜单是否与选择器对齐 | Boolean | true |
onChange | Select发生改变的时候触发的回调签名:Function(value: String, option: Object) => void参数:value: {String} 数据option: {Object} 选项 | Function | () => {} |
onSearch | 在搜索框中输入触发的事件,仅在showSearch为true时候有效签名:Function(value: String) => void参数:value: {String} 搜索值 | Function | () => {} |
hasClear | 是否显示清空按钮,该按钮可以清空select的value, 该属性仅在单选模式下有效 | Boolean | false |
popupClassName | 弹出层的样式类 | String | - |
popupProps | 弹出层的属性 | Object | - |
onVisibleChange | 弹层显示或隐藏的时候触发的事件签名:Function(visible: Boolean) => void参数:visible: {Boolean} 弹层是否隐藏和显示 | Function | () => {} |
onOpen | 弹层显示时触发的事件签名:Function() => void | Function | () => {} |
onClose | 弹层隐藏时触发的事件签名:Function() => void | Function | () => {} |
locale | 自定义国际化文案对象属性:selectPlaceHolder: {String} 请选择的提示文案comboboxPlaceHolder: {String} 请输入的提示文案 | Object | { selectPlaceHolder: '请选择', comboboxPlaceHolder: '请输入' } |
language | 自定义国际化语言可选值:'en-us', 'zh-cn', 'zh-tw' | Enum | - |
Select.Combobox
参数 | 说明 | 类型 | 默认值 |
---|---|---|---|
prefix | 样式类名的品牌前缀 | String | 'next-' |
className | 自定义类名 | String | - |
style | 自定义内联样式 | Object | - |
container | 弹层挂载容器节点 | String/Function | - |
size | 选择器的尺寸可选值:'small', 'medium', 'large' | Enum | 'medium' |
shape | 选择器的形状可选值:'normal', 'arrow-only' | Enum | 'normal' |
placeholder | 没有值的时候的占位符 | String | '' |
value | 当前值 | String/Array/ReactNode/Object | - |
defaultValue | 初始默认值 | String/Array/ReactNode/Object | - |
visible | 当前弹层是否显示 | Boolean | - |
defaultVisible | 弹层初始是否显示 | Boolean | - |
disabled | 是否禁用 | Boolean | false |
dataSource | 传入的数据,可以动态渲染子项,详见dataSource的使用 | Array | - |
multiple | 是否是多选 | Boolean | false |
tags | 是否启用标签模式, 仅在多选时有效,效果是除了自动提示外的选项, 也可以使用输入的文字作为标签 | Boolean | - |
fillProps | 填充到选择框里面的值 | String | 'value' |
showSearch | 是否显示顶部的搜索框 | Boolean | false |
filterBy | 在输入的时候过滤的函数,仅在filterLocal为true时候有效签名:Function(filterValue: String, option: Object) => Boolean参数:filterValue: {String} 筛选值option: {Object} 选项返回值:{Boolean} 是否保留当前选项 | Function | - |
filterLocal | 是否使用本地过滤,在数据源为远程的时候需要关闭此选项 | Boolean | true |
hasArrow | 是否显示右侧的箭头 | Boolean | true |
autoWidth | 下拉菜单是否与选择器对齐 | Boolean | true |
hiddenSelected | 选择后是否立即隐藏菜单,单选是默认隐藏的,因此该选项只在多选的时候有效 | Boolean | false |
onInputUpdate | Combobox在用户输入的时候触发的回调签名:Function(value: String) => void参数:value: {String} 输入值 | Function | () => {} |
onInputFocus | Combobox获取焦点的时候触发的回调签名:Function(e: Event, clickByUser: Boolean) => void参数:e: {Event} DOM事件对象clickByUser: {Boolean} 是否用户点击 | Function | () => {} |
onInputBlur | Combobox失去焦点的时候触发的回调签名:Function(e: Event, inputValue: String) => void参数:e: {Event} DOM事件对象inputValue: {String} 输入值 | Function | () => {} |
onInputEnter | Combobox回车的时候触发的回调签名:Function(e: Event) => void参数:e: {Event} DOM事件对象 | Function | () => {} |
onChange | Combobox发生改变的时候触发的回调, 注意在输入的时候该事件不会被触发,如果需要监听输入的事件请使用onInputUpdate签名:Function(value: String, option: Object) => void参数:value: {String} 数据option: {Object} 选项 | Function | () => {} |
onSearch | 在搜索框中输入触发的事件,仅在showSearch为true时候有效签名:Function(value: String) => void参数:value: {String} 搜索值 | Function | () => {} |
hasClear | 是否显示清空按钮,该按钮可以清空select的value, 该属性仅在单选模式下有效 | Boolean | false |
popupClassName | 弹出层的样式类 | String | - |
popupProps | 弹出层的属性 | Object | - |
onVisibleChange | 弹层显示或隐藏的时候触发的事件签名:Function(visible: Boolean) => void参数:visible: {Boolean} 弹层是否隐藏和显示 | Function | () => {} |
onOpen | 弹层显示时触发的事件签名:Function() => void | Function | () => {} |
onClose | 弹层隐藏时触发的事件签名:Function() => void | Function | () => {} |
locale | 自定义国际化文案对象属性:selectPlaceHolder: {String} 请选择的提示文案comboboxPlaceHolder: {String} 请输入的提示文案 | Object | { selectPlaceHolder: '请选择', comboboxPlaceHolder: '请输入' } |
language | 自定义国际化语言可选值:'en-us', 'zh-cn', 'zh-tw' | Enum | - |
Select.OptionGroup
参数 | 说明 | 类型 | 默认值 |
---|---|---|---|
label | 设置分组的文案 | String/ReactNode | - |
Select.Option
参数 | 说明 | 类型 | 默认值 |
---|---|---|---|
value | 选项值 | any | - |
disabled | 是否禁用 | Boolean | - |
代码示例
演示了Combobox的多种形态.
查看源码在线预览
import { Select, Button } from "@icedesign/base";
const { Combobox } = Select;
const onChange = function(...args) {
console.log(args);
};
class App extends React.Component {
constructor(props) {
super(props);
this.state = {};
}
onToggle() {
this.setState({
disabled: !this.state.disabled
});
}
onSelect(type, value) {
switch (type) {
case "mode":
this.setState({
[type]: value === "multiple"
});
break;
case "search":
case "hasArrow":
case "hasClear":
this.setState({
[type]: value === "yes"
});
break;
default:
this.setState({
[type]: value
});
}
}
onInputUpdate(value) {
console.log(value);
}
onInputBlur(e, value) {
console.log("blur", value);
}
render() {
return (
<div className="demo-ctl">
<p>
<Select
placeholder="选择尺寸"
onChange={this.onSelect.bind(this, "size")}
>
<Option value="small">Small</Option>
<Option value="medium">Medium</Option>
<Option value="large">Large</Option>
</Select>
<Select
placeholder="选择模式"
onChange={this.onSelect.bind(this, "mode")}
>
<Option value="single">单选</Option>
<Option value="multiple">多选</Option>
</Select>
<Select
placeholder="显示箭头"
onChange={this.onSelect.bind(this, "hasArrow")}
>
<Option value="yes">常规</Option>
<Option value="no">无下拉箭头</Option>
</Select>
<Select
placeholder="显示清空按钮"
onChange={this.onSelect.bind(this, "hasClear")}
>
<Option value="no">不显示</Option>
<Option value="yes">显示</Option>
</Select>
<Select
placeholder="填充属性"
onChange={this.onSelect.bind(this, "fillProps")}
>
<Option value="value">value</Option>
<Option value="label">label</Option>
</Select>
<Button onClick={this.onToggle.bind(this)} type="primary">
Toggle disabled
</Button>
</p>
<h4>预览</h4>
<Combobox
size={this.state.size}
multiple={this.state.mode}
hasArrow={this.state.hasArrow}
disabled={this.state.disabled}
fillProps={this.state.fillProps}
hasClear={this.state.hasClear}
onChange={onChange}
onInputBlur={this.onInputBlur.bind(this)}
onInputUpdate={this.onInputUpdate.bind(this)}
>
<Option value="">空</Option>
<Option value="jack">Jack</Option>
<Option value="lucy">Lucy</Option>
<Option value="andy">Andy</Option>
<Option value="disabled" disabled>
Disabled
</Option>
</Combobox>
</div>
);
}
}
ReactDOM.render(<App />, mountNode);
.next-select{
margin-right:10px;
vertical-align: middle;
}
.demo-ctl {
background-color: #f6f6f6;
padding: 20px;
margin-bottom: 10px;
margin-top: 10px;
}
.demo-ctl p {
margin-top:0;
}
演示Combobox的受控状态
查看源码在线预览
import { Select } from "@icedesign/base";
import jsonp from "jsonp";
const { Combobox } = Select;
class App extends React.Component {
state = {
dataSource: [],
value: "test"
};
onInputUpdate(value) {
if (this.searchTimeout) {
clearTimeout(this.searchTimeout);
}
this.searchTimeout = setTimeout(() => {
jsonp(
`https://suggest.taobao.com/sug?code=utf-8&q=${value}`,
(err, data) => {
const dataSource = data.result.map(item => {
return {
label: item[0],
value: item[1]
};
});
this.setState({
dataSource
});
}
);
}, 100);
//单选情况下需要更新input的值
this.setState({
value
});
}
onChange(value) {
this.setState({
value
});
}
render() {
return (
<div className="demo-ctl">
<Combobox
onInputUpdate={this.onInputUpdate.bind(this)}
filterLocal={false}
value={this.state.value}
fillProps="label"
placeholder="请输入淘宝商品"
onChange={this.onChange.bind(this)}
dataSource={this.state.dataSource}
/>
</div>
);
}
}
ReactDOM.render(<App />, mountNode);
.next-select{
margin-right:10px;
vertical-align: middle;
}
.demo-ctl {
background-color: #f6f6f6;
padding: 20px;
margin-bottom: 10px;
margin-top: 10px;
}
.demo-ctl p {
margin-top:0;
}
演示多选情况下Combobox的受控状态
查看源码在线预览
import { Select } from "@icedesign/base";
import jsonp from "jsonp";
const { Combobox } = Select;
class App extends React.Component {
state = {
dataSource: [],
value: "test"
};
//多选情况下不需要更新input的值
onInputUpdate(value) {
if (this.searchTimeout) {
clearTimeout(this.searchTimeout);
}
this.searchTimeout = setTimeout(() => {
jsonp(
`https://suggest.taobao.com/sug?code=utf-8&q=${value}`,
(err, data) => {
const dataSource = data.result.map(item => {
return {
label: item[0],
value: item[1]
};
});
this.setState({
dataSource
});
}
);
}, 100);
}
onChange(value) {
this.setState({
value
});
}
render() {
return (
<div className="demo-ctl">
<Combobox
onInputUpdate={this.onInputUpdate.bind(this)}
filterLocal={false}
value={this.state.value}
fillProps="label"
multiple
placeholder="请输入淘宝商品"
onChange={this.onChange.bind(this)}
dataSource={this.state.dataSource}
/>
</div>
);
}
}
ReactDOM.render(<App />, mountNode);
.next-select{
margin-right:10px;
vertical-align: middle;
}
.demo-ctl {
background-color: #f6f6f6;
padding: 20px;
margin-bottom: 10px;
margin-top: 10px;
}
.demo-ctl p {
margin-top:0;
}
使用动态数据填充Combobox, 设置filterLocal
为false
查看源码在线预览
import { Select } from "@icedesign/base";
import jsonp from "jsonp";
const { Combobox } = Select;
const onChange = function(...args) {
console.log(args);
};
class App extends React.Component {
state = {
dataSource: []
};
onInputUpdate(value) {
if (this.searchTimeout) {
clearTimeout(this.searchTimeout);
}
this.searchTimeout = setTimeout(() => {
jsonp(
`https://suggest.taobao.com/sug?code=utf-8&q=${value}`,
(err, data) => {
const dataSource = data.result.map(item => {
return {
label: item[0],
value: item[1]
};
});
this.setState({
dataSource
});
}
);
}, 100);
}
render() {
return (
<div className="demo-ctl">
<Combobox
onInputUpdate={this.onInputUpdate.bind(this)}
filterLocal={false}
fillProps="label"
placeholder="请输入淘宝商品"
onChange={onChange}
dataSource={this.state.dataSource}
/>
</div>
);
}
}
ReactDOM.render(<App />, mountNode);
.next-select{
margin-right:10px;
vertical-align: middle;
}
.demo-ctl {
background-color: #f6f6f6;
padding: 20px;
margin-bottom: 10px;
margin-top: 10px;
}
.demo-ctl p {
margin-top:0;
}
展示较为复杂的内容
查看源码在线预览
import { Select, Icon } from "@icedesign/base";
import jsonp from "jsonp";
const { Combobox } = Select;
const onChange = function(...args) {
console.log(args);
};
const formatter = function(item) {
return (
<div>
<Icon type="gifts" size="small" /> {item}
</div>
);
};
class App extends React.Component {
state = {
dataSource: []
};
onInputUpdate(value) {
if (this.searchTimeout) {
clearTimeout(this.searchTimeout);
}
this.searchTimeout = setTimeout(() => {
jsonp(
`https://suggest.taobao.com/sug?code=utf-8&q=${value}`,
(err, data) => {
const dataSource = data.result.map(item => {
return {
label: formatter(item[0]),
value: item[1],
originLabel: item[0]
};
});
this.setState({
dataSource
});
}
);
}, 100);
}
// 由于这个时候展示的是富文本,所以fillProps需要设置成展示合理的值(originLabel)
render() {
return (
<div className="demo-ctl">
<Combobox
onInputUpdate={this.onInputUpdate.bind(this)}
filterLocal={false}
fillProps="originLabel"
placeholder="请输入淘宝商品"
onChange={onChange}
dataSource={this.state.dataSource}
/>
</div>
);
}
}
ReactDOM.render(<App />, mountNode);
.next-select{
margin-right:10px;
vertical-align: middle;
}
.demo-ctl {
background-color: #f6f6f6;
padding: 20px;
margin-bottom: 10px;
margin-top: 10px;
}
.demo-ctl p {
margin-top:0;
}
演示combobox的tags功能
查看源码在线预览
import { Select, Button } from "@icedesign/base";
const { Combobox } = Select;
class App extends React.Component {
constructor(props) {
super(props);
this.state = {
dataSource: [
{
value: "测试",
label: "测试",
time: 123
}
],
value: "测试",
visible: false
};
}
render() {
return (
<div className="demo-ctl">
<p>
<Button onClick={this.toggleTags.bind(this)}>切换tags模式</Button>
<Button onClick={this.toggleHiddenSelect.bind(this)}>
切换在多选的时候是否隐藏菜单
</Button>
<Button onClick={this.toggleVisible.bind(this)} ref="visibleElement">
切换显示
</Button>
</p>
<Combobox
size="large"
value={this.state.value}
dataSource={this.state.dataSource}
onChange={this.onChange.bind(this)}
onInputUpdate={this.onInputUpdate.bind(this)}
multiple
tags={this.state.tags}
filterLocal={false}
safeNode={() => this.refs.visibleElement}
visible={this.state.visible}
onInputBlur={() => console.log("blur")}
onVisibleChange={this.onVisibleChange.bind(this)}
hiddenSelected={this.state.hiddenSelected}
/>
</div>
);
}
onInputUpdate(value) {
let options;
if (!value || value.indexOf("@") > 0) {
options = [];
} else {
options = ["126.com", "163.com", "gmail.com"].map(mail => {
return {
label: value + "@" + mail,
value: value + "@" + mail,
time: Math.random()
};
});
}
this.setState({ dataSource: options });
}
onChange(value, item) {
this.setState({ value });
console.log(item);
}
toggleTags() {
this.setState({ tags: !this.state.tags });
}
toggleHiddenSelect() {
this.setState({ hiddenSelected: !this.state.hiddenSelected });
}
toggleVisible() {
this.setState({ visible: !this.state.visible });
}
onVisibleChange(visible) {
this.setState({ visible });
}
}
ReactDOM.render(<App />, mountNode);
.demo-ctl {
background-color: #f6f6f6;
padding: 20px;
margin-bottom: 10px;
margin-top: 10px;
}
.demo-ctl p {
margin-top:0;
}
通过overlay定制select弹层
查看源码在线预览
import { Select } from "@icedesign/base";
import classnames from "classnames";
/* eslint-disable react/prop-types, react/no-multi-comp */
class Menu extends React.Component {
data = [
{
label: "value1",
value: 1
},
{
label: "value2",
value: 2
}
];
render() {
const { className, ...others } = this.props;
const cls = classnames({
"overlay-content": true,
[className]: className
});
return (
<ul className={cls} {...others}>
{this.renderItems()}
</ul>
);
}
renderItems() {
return this.data.map(item => (
<li onClick={this.onClick.bind(this, item)} key={item.value}>
{item.label}
</li>
));
}
onClick(item) {
this.props.onChange(item);
}
}
class App extends React.Component {
constructor(props) {
super(props);
this.state = {
value: null
};
}
onSelect = value => {
this.setState({
value,
visible: false
});
};
onVisibleChange = visible => {
this.setState({
visible
});
};
render() {
const overlay = <Menu onChange={this.onSelect} />;
return (
<div className="demo-ctl">
<Select
placeholder="选择自定义数据"
visible={this.state.visible}
onVisibleChange={this.onVisibleChange}
value={this.state.value}
overlay={overlay}
/>
</div>
);
}
}
ReactDOM.render(<App />, mountNode);
.next-select{
margin-right:10px;
vertical-align: middle;
}
.demo-ctl {
background-color: #f6f6f6;
padding: 20px;
margin-bottom: 10px;
margin-top: 10px;
}
.demo-ctl p {
margin-top:0;
}
.overlay-content{
border:1px solid #ddd;
padding:10px;
background:#fff;
margin:0;
min-width:150px;
font-family: Arial;
box-shadow: 2px 2px 20px rgba(0,0,0,0.15);
}
.overlay-content li{
list-style: none;
line-height:30px;
padding: 0 5px;
cursor: pointer;
}
.overlay-content li:hover{
background:#f8f8f8;
}
.overlay-content li:last-child{
border-width:0;
}
演示了Select的多种形态.
查看源码在线预览
import { Select, Button } from "@icedesign/base";
const { Option } = Select;
const onChange = function(...args) {
console.log(args);
};
class App extends React.Component {
constructor(props) {
super(props);
this.state = {};
}
render() {
return (
<div className="demo-ctl">
<p>
<Select
placeholder="选择尺寸"
onChange={this.onSelect.bind(this, "size")}
>
<Option value="small">Small</Option>
<Option value="medium">Medium</Option>
<Option value="large">Large</Option>
</Select>
<Select
placeholder="选择模式"
onChange={this.onSelect.bind(this, "mode")}
>
<Option value="single">单选</Option>
<Option value="multiple">多选</Option>
</Select>
<Select
placeholder="显示搜索框"
onChange={this.onSelect.bind(this, "search")}
>
<Option value="no">不显示</Option>
<Option value="yes">显示</Option>
</Select>
<Select
placeholder="选择形状"
onChange={this.onSelect.bind(this, "shape")}
>
<Option value="normal">常规</Option>
<Option value="arrow-only">无边框</Option>
</Select>
<Select
placeholder="显示箭头"
onChange={this.onSelect.bind(this, "hasArrow")}
>
<Option value="yes">常规</Option>
<Option value="no">无下拉箭头</Option>
</Select>
<Select
placeholder="显示清空按钮"
onChange={this.onSelect.bind(this, "hasClear")}
>
<Option value="no">不显示</Option>
<Option value="yes">显示</Option>
</Select>
<Button onClick={this.onToggle.bind(this)} type="primary">
Toggle disabled
</Button>
</p>
<h4>预览</h4>
<Select
size={this.state.size}
multiple={this.state.mode}
showSearch={this.state.search}
shape={this.state.shape}
hasArrow={this.state.hasArrow}
disabled={this.state.disabled}
hasClear={this.state.hasClear}
onChange={onChange}
>
<Option value="">空</Option>
<Option value="jack">Jack</Option>
<Option value="lucy">Lucy</Option>
<Option value="andy">Andy</Option>
<Option value="disabled" disabled>
Disabled
</Option>
</Select>
</div>
);
}
onToggle() {
this.setState({
disabled: !this.state.disabled
});
}
onSelect(type, value) {
switch (type) {
case "mode":
this.setState({
[type]: value === "multiple"
});
break;
case "search":
case "hasArrow":
case "hasClear":
this.setState({
[type]: value === "yes"
});
break;
default:
this.setState({
[type]: value
});
}
}
}
ReactDOM.render(<App />, mountNode);
.next-select{
margin-right:10px;
vertical-align: middle;
margin-top:5px;
}
.demo-ctl {
background-color: #f6f6f6;
padding: 20px;
margin-bottom: 10px;
margin-top: 10px;
}
.demo-ctl p {
margin-top:0;
padding:5px 10px 10px 10px;
border:2px dotted #ddd;
border-radius:3px;
}
.demo-ctl p .next-btn{
margin-top:5px;
vertical-align: middle;
}
使用Select构建级联选择框.
查看源码在线预览
import { Select } from "@icedesign/base";
const provinceData = ["浙江", "江苏"];
const cityData = {
浙江: ["杭州", "宁波", "温州"],
江苏: ["南京", "苏州", "镇江"]
};
class App extends React.Component {
constructor(props) {
super(props);
this.state = {
data: [],
disabled: true
};
}
render() {
return (
<div className="demo-ctl">
<Select
placeholder="选择省份"
dataSource={provinceData}
value={this.state.province}
onChange={this.onSelect.bind(this)}
/>
<Select
placeholder="选择城市"
dataSource={this.state.data}
value={this.state.city}
onChange={this.onChange.bind(this)}
disabled={this.state.disabled}
/>
</div>
);
}
onSelect(value) {
const data = cityData[value];
this.setState({ data, province: value, disabled: false });
}
onChange(value) {
this.setState({ city: value });
console.log(this.state.province, value);
}
}
ReactDOM.render(<App />, mountNode);
.next-select{margin-right:10px;}
.demo-ctl {
background-color: #f6f6f6;
padding: 20px;
margin-bottom: 10px;
margin-top: 10px;
}
.demo-ctl p {
margin-top:0;
}
使用OptionGroup针对选项进行分组
查看源码在线预览
import { Select } from "@icedesign/base";
const { Option, OptionGroup } = Select;
const dataSource = [
{
label: "文案1",
children: [
{
label: "文案11",
value: "text11"
}
]
},
{
label: "文案2",
children: [
{
label: "文案22",
value: "text22"
}
]
}
];
ReactDOM.render(
<div className="demo-ctl">
<Select placeholder="选择尺寸" showSearch>
<OptionGroup label="尺寸1">
<Option value="small">Small</Option>
<Option value="medium">Medium</Option>
<Option value="large">Large</Option>
</OptionGroup>
<OptionGroup label="尺寸2">
<Option value="small2">Small2</Option>
<Option value="medium2">Medium2</Option>
<Option value="large2">Large2</Option>
</OptionGroup>
</Select>
<Select dataSource={dataSource} />
</div>,
mountNode
);
.next-select{margin-right:10px;}
.demo-ctl {
background-color: #f6f6f6;
padding: 20px;
margin-bottom: 10px;
margin-top: 10px;
}
使用showSearch显示搜索框
查看源码在线预览
import { Select } from "@icedesign/base";
import jsonp from "jsonp";
class App extends React.Component {
state = {
dataSource: []
};
render() {
return (
<div className="demo-ctl">
<Select
showSearch
dataSource={this.state.dataSource}
onSearch={this.onSearch}
filterLocal={false}
className="temp"
/>
</div>
);
}
onSearch = value => {
if (this.searchTimeout) {
clearTimeout(this.searchTimeout);
}
this.searchTimeout = setTimeout(() => {
jsonp(
`https://suggest.taobao.com/sug?code=utf-8&q=${value}`,
(err, data) => {
const dataSource = data.result.map(item => {
return {
label: item[0],
value: item[1]
};
});
this.setState({
dataSource
});
}
);
}, 100);
};
}
ReactDOM.render(<App />, mountNode);
.next-select.temp{
margin-right:10px;
width:200px;
}
.demo-ctl {
background-color: #f6f6f6;
padding: 20px;
margin-bottom: 10px;
margin-top: 10px;
}