Select 选择器

如果项目中使用的是 1.x 版本的基础组件(@alifd/next),请在左侧导航顶部切换组件版本。

安装方法

  1. 在命令行中执行以下命令npm install @icedesign/base@latest -S

Guide

何时使用

Select

如果你不期望用户输入的值生效而仅仅是选择,那么使用Select. 同时可以使用Select的showSearch属性进行过滤。

Combobox

唯一和Select不一致的点在于Combobox会保留用户输入的值。

常见问题

出现类似的

Select默认使用value作为菜单项的key,如果没有设置key值,则使用默认的序列index作为key值,确保这些值不会发生重复。

Combobox与Field配合使用

由于Field会让组件变成受控组件,所以如果你发现你的输入框无法输入值,记得监听下onInputUpdate,然后调用FieldsetValue方法进行赋值。

伪代码如下

  1. this.field.setValue(name, value);

dataSource的使用

Select同时支持children和在props中传入dataSource作为数据源,如果同时设置,则以children为准.

注意: Select默认使用value作为渲染的菜单项的key值,所以value不能重复, 否则无法渲染下拉菜单

  • children的方式
  1. <Select>
  2. <Select.Option value="option1">option1</Select.Option>
  3. <Select.Option value="option2">option2</Select.Option>
  4. <Select.Option disabled>disabled</Select.Option>
  5. </Select>
  • props的方式
  1. const dataSource = [
  2. {label:'option1', value:'option1'},
  3. {label:'option2', value:'option2'},
  4. {label:'disabled', disabled:true}
  5. ]
  6. <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是否禁用Booleanfalse
dataSource传入的数据,可以动态渲染子项,详见dataSource的使用Array-
multiple是否是多选Booleanfalse
fillProps填充到选择框里面的值String'label'
showSearch是否显示顶部的搜索框Booleanfalse
filterBy在输入的时候过滤的函数,仅在filterLocal为true时候有效签名:Function(filterValue: String, option: Object) => Boolean参数:filterValue: {String} 筛选值option: {Object} 选项返回值:{Boolean} 是否保留当前选项Function-
filterLocal是否使用本地过滤,在数据源为远程的时候需要关闭此选项Booleantrue
hasArrow是否显示右侧的箭头Booleantrue
autoWidth下拉菜单是否与选择器对齐Booleantrue
onChangeSelect发生改变的时候触发的回调签名:Function(value: String, option: Object) => void参数:value: {String} 数据option: {Object} 选项Function() => {}
onSearch在搜索框中输入触发的事件,仅在showSearch为true时候有效签名:Function(value: String) => void参数:value: {String} 搜索值Function() => {}
hasClear是否显示清空按钮,该按钮可以清空select的value, 该属性仅在单选模式下有效Booleanfalse
popupClassName弹出层的样式类String-
popupProps弹出层的属性Object-
onVisibleChange弹层显示或隐藏的时候触发的事件签名:Function(visible: Boolean) => void参数:visible: {Boolean} 弹层是否隐藏和显示Function() => {}
onOpen弹层显示时触发的事件签名:Function() => voidFunction() => {}
onClose弹层隐藏时触发的事件签名:Function() => voidFunction() => {}
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是否禁用Booleanfalse
dataSource传入的数据,可以动态渲染子项,详见dataSource的使用Array-
multiple是否是多选Booleanfalse
tags是否启用标签模式, 仅在多选时有效,效果是除了自动提示外的选项, 也可以使用输入的文字作为标签Boolean-
fillProps填充到选择框里面的值String'value'
showSearch是否显示顶部的搜索框Booleanfalse
filterBy在输入的时候过滤的函数,仅在filterLocal为true时候有效签名:Function(filterValue: String, option: Object) => Boolean参数:filterValue: {String} 筛选值option: {Object} 选项返回值:{Boolean} 是否保留当前选项Function-
filterLocal是否使用本地过滤,在数据源为远程的时候需要关闭此选项Booleantrue
hasArrow是否显示右侧的箭头Booleantrue
autoWidth下拉菜单是否与选择器对齐Booleantrue
hiddenSelected选择后是否立即隐藏菜单,单选是默认隐藏的,因此该选项只在多选的时候有效Booleanfalse
onInputUpdateCombobox在用户输入的时候触发的回调签名:Function(value: String) => void参数:value: {String} 输入值Function() => {}
onInputFocusCombobox获取焦点的时候触发的回调签名:Function(e: Event, clickByUser: Boolean) => void参数:e: {Event} DOM事件对象clickByUser: {Boolean} 是否用户点击Function() => {}
onInputBlurCombobox失去焦点的时候触发的回调签名:Function(e: Event, inputValue: String) => void参数:e: {Event} DOM事件对象inputValue: {String} 输入值Function() => {}
onInputEnterCombobox回车的时候触发的回调签名:Function(e: Event) => void参数:e: {Event} DOM事件对象Function() => {}
onChangeCombobox发生改变的时候触发的回调, 注意在输入的时候该事件不会被触发,如果需要监听输入的事件请使用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, 该属性仅在单选模式下有效Booleanfalse
popupClassName弹出层的样式类String-
popupProps弹出层的属性Object-
onVisibleChange弹层显示或隐藏的时候触发的事件签名:Function(visible: Boolean) => void参数:visible: {Boolean} 弹层是否隐藏和显示Function() => {}
onOpen弹层显示时触发的事件签名:Function() => voidFunction() => {}
onClose弹层隐藏时触发的事件签名:Function() => voidFunction() => {}
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

演示了Combobox的多种形态.

Select 选择器 - 图1

查看源码在线预览

  1. import { Select, Button } from "@icedesign/base";
  2. const { Combobox } = Select;
  3. const onChange = function(...args) {
  4. console.log(args);
  5. };
  6. class App extends React.Component {
  7. constructor(props) {
  8. super(props);
  9. this.state = {};
  10. }
  11. onToggle() {
  12. this.setState({
  13. disabled: !this.state.disabled
  14. });
  15. }
  16. onSelect(type, value) {
  17. switch (type) {
  18. case "mode":
  19. this.setState({
  20. [type]: value === "multiple"
  21. });
  22. break;
  23. case "search":
  24. case "hasArrow":
  25. case "hasClear":
  26. this.setState({
  27. [type]: value === "yes"
  28. });
  29. break;
  30. default:
  31. this.setState({
  32. [type]: value
  33. });
  34. }
  35. }
  36. onInputUpdate(value) {
  37. console.log(value);
  38. }
  39. onInputBlur(e, value) {
  40. console.log("blur", value);
  41. }
  42. render() {
  43. return (
  44. <div className="demo-ctl">
  45. <p>
  46. <Select
  47. placeholder="选择尺寸"
  48. onChange={this.onSelect.bind(this, "size")}
  49. >
  50. <Option value="small">Small</Option>
  51. <Option value="medium">Medium</Option>
  52. <Option value="large">Large</Option>
  53. </Select>
  54. <Select
  55. placeholder="选择模式"
  56. onChange={this.onSelect.bind(this, "mode")}
  57. >
  58. <Option value="single">单选</Option>
  59. <Option value="multiple">多选</Option>
  60. </Select>
  61. <Select
  62. placeholder="显示箭头"
  63. onChange={this.onSelect.bind(this, "hasArrow")}
  64. >
  65. <Option value="yes">常规</Option>
  66. <Option value="no">无下拉箭头</Option>
  67. </Select>
  68. <Select
  69. placeholder="显示清空按钮"
  70. onChange={this.onSelect.bind(this, "hasClear")}
  71. >
  72. <Option value="no">不显示</Option>
  73. <Option value="yes">显示</Option>
  74. </Select>
  75. <Select
  76. placeholder="填充属性"
  77. onChange={this.onSelect.bind(this, "fillProps")}
  78. >
  79. <Option value="value">value</Option>
  80. <Option value="label">label</Option>
  81. </Select>
  82. <Button onClick={this.onToggle.bind(this)} type="primary">
  83. Toggle disabled
  84. </Button>
  85. </p>
  86. <h4>预览</h4>
  87. <Combobox
  88. size={this.state.size}
  89. multiple={this.state.mode}
  90. hasArrow={this.state.hasArrow}
  91. disabled={this.state.disabled}
  92. fillProps={this.state.fillProps}
  93. hasClear={this.state.hasClear}
  94. onChange={onChange}
  95. onInputBlur={this.onInputBlur.bind(this)}
  96. onInputUpdate={this.onInputUpdate.bind(this)}
  97. >
  98. <Option value="">空</Option>
  99. <Option value="jack">Jack</Option>
  100. <Option value="lucy">Lucy</Option>
  101. <Option value="andy">Andy</Option>
  102. <Option value="disabled" disabled>
  103. Disabled
  104. </Option>
  105. </Combobox>
  106. </div>
  107. );
  108. }
  109. }
  110. ReactDOM.render(<App />, mountNode);
  1. .next-select{
  2. margin-right:10px;
  3. vertical-align: middle;
  4. }
  5. .demo-ctl {
  6. background-color: #f6f6f6;
  7. padding: 20px;
  8. margin-bottom: 10px;
  9. margin-top: 10px;
  10. }
  11. .demo-ctl p {
  12. margin-top:0;
  13. }

受控Combo

演示Combobox的受控状态

Select 选择器 - 图2

查看源码在线预览

  1. import { Select } from "@icedesign/base";
  2. import jsonp from "jsonp";
  3. const { Combobox } = Select;
  4. class App extends React.Component {
  5. state = {
  6. dataSource: [],
  7. value: "test"
  8. };
  9. onInputUpdate(value) {
  10. if (this.searchTimeout) {
  11. clearTimeout(this.searchTimeout);
  12. }
  13. this.searchTimeout = setTimeout(() => {
  14. jsonp(
  15. `https://suggest.taobao.com/sug?code=utf-8&q=${value}`,
  16. (err, data) => {
  17. const dataSource = data.result.map(item => {
  18. return {
  19. label: item[0],
  20. value: item[1]
  21. };
  22. });
  23. this.setState({
  24. dataSource
  25. });
  26. }
  27. );
  28. }, 100);
  29. //单选情况下需要更新input的值
  30. this.setState({
  31. value
  32. });
  33. }
  34. onChange(value) {
  35. this.setState({
  36. value
  37. });
  38. }
  39. render() {
  40. return (
  41. <div className="demo-ctl">
  42. <Combobox
  43. onInputUpdate={this.onInputUpdate.bind(this)}
  44. filterLocal={false}
  45. value={this.state.value}
  46. fillProps="label"
  47. placeholder="请输入淘宝商品"
  48. onChange={this.onChange.bind(this)}
  49. dataSource={this.state.dataSource}
  50. />
  51. </div>
  52. );
  53. }
  54. }
  55. ReactDOM.render(<App />, mountNode);
  1. .next-select{
  2. margin-right:10px;
  3. vertical-align: middle;
  4. }
  5. .demo-ctl {
  6. background-color: #f6f6f6;
  7. padding: 20px;
  8. margin-bottom: 10px;
  9. margin-top: 10px;
  10. }
  11. .demo-ctl p {
  12. margin-top:0;
  13. }

多选受控

演示多选情况下Combobox的受控状态

Select 选择器 - 图3

查看源码在线预览

  1. import { Select } from "@icedesign/base";
  2. import jsonp from "jsonp";
  3. const { Combobox } = Select;
  4. class App extends React.Component {
  5. state = {
  6. dataSource: [],
  7. value: "test"
  8. };
  9. //多选情况下不需要更新input的值
  10. onInputUpdate(value) {
  11. if (this.searchTimeout) {
  12. clearTimeout(this.searchTimeout);
  13. }
  14. this.searchTimeout = setTimeout(() => {
  15. jsonp(
  16. `https://suggest.taobao.com/sug?code=utf-8&q=${value}`,
  17. (err, data) => {
  18. const dataSource = data.result.map(item => {
  19. return {
  20. label: item[0],
  21. value: item[1]
  22. };
  23. });
  24. this.setState({
  25. dataSource
  26. });
  27. }
  28. );
  29. }, 100);
  30. }
  31. onChange(value) {
  32. this.setState({
  33. value
  34. });
  35. }
  36. render() {
  37. return (
  38. <div className="demo-ctl">
  39. <Combobox
  40. onInputUpdate={this.onInputUpdate.bind(this)}
  41. filterLocal={false}
  42. value={this.state.value}
  43. fillProps="label"
  44. multiple
  45. placeholder="请输入淘宝商品"
  46. onChange={this.onChange.bind(this)}
  47. dataSource={this.state.dataSource}
  48. />
  49. </div>
  50. );
  51. }
  52. }
  53. ReactDOM.render(<App />, mountNode);
  1. .next-select{
  2. margin-right:10px;
  3. vertical-align: middle;
  4. }
  5. .demo-ctl {
  6. background-color: #f6f6f6;
  7. padding: 20px;
  8. margin-bottom: 10px;
  9. margin-top: 10px;
  10. }
  11. .demo-ctl p {
  12. margin-top:0;
  13. }

动态数据

使用动态数据填充Combobox, 设置filterLocal为false

Select 选择器 - 图4

查看源码在线预览

  1. import { Select } from "@icedesign/base";
  2. import jsonp from "jsonp";
  3. const { Combobox } = Select;
  4. const onChange = function(...args) {
  5. console.log(args);
  6. };
  7. class App extends React.Component {
  8. state = {
  9. dataSource: []
  10. };
  11. onInputUpdate(value) {
  12. if (this.searchTimeout) {
  13. clearTimeout(this.searchTimeout);
  14. }
  15. this.searchTimeout = setTimeout(() => {
  16. jsonp(
  17. `https://suggest.taobao.com/sug?code=utf-8&q=${value}`,
  18. (err, data) => {
  19. const dataSource = data.result.map(item => {
  20. return {
  21. label: item[0],
  22. value: item[1]
  23. };
  24. });
  25. this.setState({
  26. dataSource
  27. });
  28. }
  29. );
  30. }, 100);
  31. }
  32. render() {
  33. return (
  34. <div className="demo-ctl">
  35. <Combobox
  36. onInputUpdate={this.onInputUpdate.bind(this)}
  37. filterLocal={false}
  38. fillProps="label"
  39. placeholder="请输入淘宝商品"
  40. onChange={onChange}
  41. dataSource={this.state.dataSource}
  42. />
  43. </div>
  44. );
  45. }
  46. }
  47. ReactDOM.render(<App />, mountNode);
  1. .next-select{
  2. margin-right:10px;
  3. vertical-align: middle;
  4. }
  5. .demo-ctl {
  6. background-color: #f6f6f6;
  7. padding: 20px;
  8. margin-bottom: 10px;
  9. margin-top: 10px;
  10. }
  11. .demo-ctl p {
  12. margin-top:0;
  13. }

图文展示

展示较为复杂的内容

Select 选择器 - 图5

查看源码在线预览

  1. import { Select, Icon } from "@icedesign/base";
  2. import jsonp from "jsonp";
  3. const { Combobox } = Select;
  4. const onChange = function(...args) {
  5. console.log(args);
  6. };
  7. const formatter = function(item) {
  8. return (
  9. <div>
  10. <Icon type="gifts" size="small" />&nbsp;{item}
  11. </div>
  12. );
  13. };
  14. class App extends React.Component {
  15. state = {
  16. dataSource: []
  17. };
  18. onInputUpdate(value) {
  19. if (this.searchTimeout) {
  20. clearTimeout(this.searchTimeout);
  21. }
  22. this.searchTimeout = setTimeout(() => {
  23. jsonp(
  24. `https://suggest.taobao.com/sug?code=utf-8&q=${value}`,
  25. (err, data) => {
  26. const dataSource = data.result.map(item => {
  27. return {
  28. label: formatter(item[0]),
  29. value: item[1],
  30. originLabel: item[0]
  31. };
  32. });
  33. this.setState({
  34. dataSource
  35. });
  36. }
  37. );
  38. }, 100);
  39. }
  40. // 由于这个时候展示的是富文本,所以fillProps需要设置成展示合理的值(originLabel)
  41. render() {
  42. return (
  43. <div className="demo-ctl">
  44. <Combobox
  45. onInputUpdate={this.onInputUpdate.bind(this)}
  46. filterLocal={false}
  47. fillProps="originLabel"
  48. placeholder="请输入淘宝商品"
  49. onChange={onChange}
  50. dataSource={this.state.dataSource}
  51. />
  52. </div>
  53. );
  54. }
  55. }
  56. ReactDOM.render(<App />, mountNode);
  1. .next-select{
  2. margin-right:10px;
  3. vertical-align: middle;
  4. }
  5. .demo-ctl {
  6. background-color: #f6f6f6;
  7. padding: 20px;
  8. margin-bottom: 10px;
  9. margin-top: 10px;
  10. }
  11. .demo-ctl p {
  12. margin-top:0;
  13. }

tags

演示combobox的tags功能

Select 选择器 - 图6

查看源码在线预览

  1. import { Select, Button } from "@icedesign/base";
  2. const { Combobox } = Select;
  3. class App extends React.Component {
  4. constructor(props) {
  5. super(props);
  6. this.state = {
  7. dataSource: [
  8. {
  9. value: "测试",
  10. label: "测试",
  11. time: 123
  12. }
  13. ],
  14. value: "测试",
  15. visible: false
  16. };
  17. }
  18. render() {
  19. return (
  20. <div className="demo-ctl">
  21. <p>
  22. <Button onClick={this.toggleTags.bind(this)}>切换tags模式</Button>&nbsp;
  23. <Button onClick={this.toggleHiddenSelect.bind(this)}>
  24. 切换在多选的时候是否隐藏菜单
  25. </Button>&nbsp;
  26. <Button onClick={this.toggleVisible.bind(this)} ref="visibleElement">
  27. 切换显示
  28. </Button>&nbsp;
  29. </p>
  30. <Combobox
  31. size="large"
  32. value={this.state.value}
  33. dataSource={this.state.dataSource}
  34. onChange={this.onChange.bind(this)}
  35. onInputUpdate={this.onInputUpdate.bind(this)}
  36. multiple
  37. tags={this.state.tags}
  38. filterLocal={false}
  39. safeNode={() => this.refs.visibleElement}
  40. visible={this.state.visible}
  41. onInputBlur={() => console.log("blur")}
  42. onVisibleChange={this.onVisibleChange.bind(this)}
  43. hiddenSelected={this.state.hiddenSelected}
  44. />
  45. </div>
  46. );
  47. }
  48. onInputUpdate(value) {
  49. let options;
  50. if (!value || value.indexOf("@") > 0) {
  51. options = [];
  52. } else {
  53. options = ["126.com", "163.com", "gmail.com"].map(mail => {
  54. return {
  55. label: value + "@" + mail,
  56. value: value + "@" + mail,
  57. time: Math.random()
  58. };
  59. });
  60. }
  61. this.setState({ dataSource: options });
  62. }
  63. onChange(value, item) {
  64. this.setState({ value });
  65. console.log(item);
  66. }
  67. toggleTags() {
  68. this.setState({ tags: !this.state.tags });
  69. }
  70. toggleHiddenSelect() {
  71. this.setState({ hiddenSelected: !this.state.hiddenSelected });
  72. }
  73. toggleVisible() {
  74. this.setState({ visible: !this.state.visible });
  75. }
  76. onVisibleChange(visible) {
  77. this.setState({ visible });
  78. }
  79. }
  80. ReactDOM.render(<App />, mountNode);
  1. .demo-ctl {
  2. background-color: #f6f6f6;
  3. padding: 20px;
  4. margin-bottom: 10px;
  5. margin-top: 10px;
  6. }
  7. .demo-ctl p {
  8. margin-top:0;
  9. }

弹层定制

通过overlay定制select弹层

Select 选择器 - 图7

查看源码在线预览

  1. import { Select } from "@icedesign/base";
  2. import classnames from "classnames";
  3. /* eslint-disable react/prop-types, react/no-multi-comp */
  4. class Menu extends React.Component {
  5. data = [
  6. {
  7. label: "value1",
  8. value: 1
  9. },
  10. {
  11. label: "value2",
  12. value: 2
  13. }
  14. ];
  15. render() {
  16. const { className, ...others } = this.props;
  17. const cls = classnames({
  18. "overlay-content": true,
  19. [className]: className
  20. });
  21. return (
  22. <ul className={cls} {...others}>
  23. {this.renderItems()}
  24. </ul>
  25. );
  26. }
  27. renderItems() {
  28. return this.data.map(item => (
  29. <li onClick={this.onClick.bind(this, item)} key={item.value}>
  30. {item.label}
  31. </li>
  32. ));
  33. }
  34. onClick(item) {
  35. this.props.onChange(item);
  36. }
  37. }
  38. class App extends React.Component {
  39. constructor(props) {
  40. super(props);
  41. this.state = {
  42. value: null
  43. };
  44. }
  45. onSelect = value => {
  46. this.setState({
  47. value,
  48. visible: false
  49. });
  50. };
  51. onVisibleChange = visible => {
  52. this.setState({
  53. visible
  54. });
  55. };
  56. render() {
  57. const overlay = <Menu onChange={this.onSelect} />;
  58. return (
  59. <div className="demo-ctl">
  60. <Select
  61. placeholder="选择自定义数据"
  62. visible={this.state.visible}
  63. onVisibleChange={this.onVisibleChange}
  64. value={this.state.value}
  65. overlay={overlay}
  66. />
  67. </div>
  68. );
  69. }
  70. }
  71. 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的多种形态.

Select 选择器 - 图8

查看源码在线预览

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构建级联选择框.

Select 选择器 - 图9

查看源码在线预览

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针对选项进行分组

Select 选择器 - 图10

查看源码在线预览

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显示搜索框

Select 选择器 - 图11

查看源码在线预览

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;
}

相关区块

Select 选择器 - 图12

暂无相关区块