Select选择框

表单控件。

何时使用

  • 弹出一个下拉菜单给用户选择操作,用于代替原生的选择器,或者需要一个更优雅的多选器时。

  • 当选项少时(少于 5 项),建议直接将选项平铺,使用 SelectBox 是更好的选择。

代码演示

Select 选择框 - 图1

基本使用

下拉选择器。

  1. import { Select, Button, Row, Col } from 'choerodon-ui/pro';
  2. function handleChange(value, oldValue) {
  3. console.log('[basic new]', value, '[basic old]', oldValue);
  4. }
  5. const { Option } = Select;
  6. class App extends React.Component {
  7. state = {
  8. visible: true,
  9. };
  10. handleClick = () => this.setState({ visible: !this.state.visible });
  11. render() {
  12. return (
  13. <Row gutter={10}>
  14. <Col span={8}>
  15. <Select placeholder="请选择" onChange={handleChange}>
  16. <Option value="jack">Jack</Option>
  17. <Option value="lucy">Lucy</Option>
  18. {this.state.visible && (
  19. <Option value="wu" disabled>
  20. Wu
  21. </Option>
  22. )}
  23. </Select>
  24. </Col>
  25. <Col span={4}>
  26. <Button onClick={this.handleClick}>修改选项</Button>
  27. </Col>
  28. <Col span={12}>
  29. <Select placeholder="请选择" disabled>
  30. <Option value="jack">Jack</Option>
  31. <Option value="lucy">Lucy</Option>
  32. <Option value="wu">Wu</Option>
  33. </Select>
  34. </Col>
  35. </Row>
  36. );
  37. }
  38. }
  39. ReactDOM.render(<App />, mountNode);

Select 选择框 - 图2

值列表代码

值列表代码。

  1. import { DataSet, Select, Button, Row, Col } from 'choerodon-ui/pro';
  2. function handleDataSetChange({ record, name, value, oldValue }) {
  3. console.log(
  4. '[dataset newValue]',
  5. value,
  6. '[oldValue]',
  7. oldValue,
  8. `[record.get('${name}')]`,
  9. record.get(name),
  10. );
  11. }
  12. function handleOption({ record }) {
  13. return {
  14. disabled: record.index === 0,
  15. };
  16. }
  17. class App extends React.Component {
  18. flag = false;
  19. ds = new DataSet({
  20. autoCreate: true,
  21. fields: [
  22. { name: 'sex', type: 'string', lookupCode: 'HR.EMPLOYEE_GENDER' },
  23. {
  24. name: 'sex2',
  25. type: 'string',
  26. dynamicProps: {
  27. lookupAxiosConfig: ({ record }) => ({
  28. url: record.get('sex') ? '/common/code/HR.EMPLOYEE_GENDER/' : null,
  29. transformResponse(data) {
  30. console.log('transformResponse', data);
  31. return data;
  32. },
  33. }),
  34. },
  35. },
  36. { name: 'lov', type: 'string', lovCode: 'LOV_CODE', defaultValue: 'SYS.PROFILE_LEVEL_ID' },
  37. {
  38. name: 'lov2',
  39. type: 'string',
  40. lovCode: 'LOV_CODE',
  41. defaultValue: ['SYS.PROFILE_LEVEL_ID', 'SYS.RESOURCE_TYPE'],
  42. multiple: true,
  43. },
  44. ],
  45. events: {
  46. update: handleDataSetChange,
  47. },
  48. });
  49. changeLookupCode = () => {
  50. this.flag = !this.flag;
  51. this.ds.getField('sex').set('lookupCode', this.flag ? 'SYS.USER_STATUS' : 'HR.EMPLOYEE_GENDER');
  52. };
  53. render() {
  54. return (
  55. <Row gutter={10}>
  56. <Col span={6}>
  57. <Select dataSet={this.ds} name="sex" placeholder="请选择" onOption={handleOption} />
  58. </Col>
  59. <Col span={6}>
  60. <Button onClick={this.changeLookupCode}>修改lookupCode</Button>
  61. </Col>
  62. <Col span={12}>
  63. <Select dataSet={this.ds} name="sex2" placeholder="请选择" />
  64. </Col>
  65. <Col span={12}>
  66. <Select dataSet={this.ds} name="lov" placeholder="请选择" />
  67. </Col>
  68. <Col span={12}>
  69. <Select dataSet={this.ds} name="lov2" placeholder="请选择" />
  70. </Col>
  71. </Row>
  72. );
  73. }
  74. }
  75. ReactDOM.render(<App />, mountNode);

Select 选择框 - 图3

数据源选项

数据源选项。

  1. import { DataSet, Select, Row, Col, Button } from 'choerodon-ui/pro';
  2. function handleDataSetChange({ record, name, value, oldValue }) {
  3. console.log(
  4. '[dataset newValue]',
  5. value,
  6. '[oldValue]',
  7. oldValue,
  8. `[record.get('${name}')]`,
  9. record.get(name),
  10. );
  11. }
  12. class App extends React.Component {
  13. optionDs = new DataSet({
  14. selection: 'single',
  15. queryUrl: '/dataset/user/queries',
  16. autoQuery: true,
  17. });
  18. ds = new DataSet({
  19. fields: [
  20. {
  21. name: 'user',
  22. type: 'string',
  23. textField: 'name',
  24. valueField: 'userid',
  25. label: '用户',
  26. options: this.optionDs,
  27. },
  28. ],
  29. events: {
  30. update: handleDataSetChange,
  31. },
  32. });
  33. changeOptions = () => {
  34. this.ds.addField('account', {
  35. name: 'account',
  36. type: 'string',
  37. textField: 'name',
  38. valueField: 'userid',
  39. label: '账户',
  40. options: this.optionDs,
  41. });
  42. };
  43. render() {
  44. return (
  45. <Row gutter={10}>
  46. <Col span={8}>
  47. <Select
  48. multiple
  49. optionsFilter={record => record.get('sex') === 'F'}
  50. dataSet={this.ds}
  51. name="user"
  52. />
  53. </Col>
  54. <Col span={8}>
  55. <Select multiple dataSet={this.ds} name="account" />
  56. </Col>
  57. <Col span={8}>
  58. <Button onClick={this.changeOptions}>切换选项</Button>
  59. </Col>
  60. </Row>
  61. );
  62. }
  63. }
  64. ReactDOM.render(<App />, mountNode);

Select 选择框 - 图4

过滤

通过属性optionsFilter过滤选项。

  1. import { DataSet, Select, Button } from 'choerodon-ui/pro';
  2. const { Option } = Select;
  3. const data = [{
  4. 'last-name': 'huazhen',
  5. }];
  6. class App extends React.Component {
  7. state = {
  8. key: 'a',
  9. };
  10. ds = new DataSet({
  11. data,
  12. fields: [
  13. { name: 'last-name', type: 'string', label: '姓' },
  14. ],
  15. });
  16. handleClick = () => {
  17. this.setState({ key: this.state.key === 'a' ? 'c' : 'a' });
  18. };
  19. optionsFilter = (record) => {
  20. return record.get('meaning').toLowerCase().indexOf(this.state.key) !== -1;
  21. };
  22. render() {
  23. return (
  24. <div>
  25. <Select dataSet={this.ds} name="last-name" optionsFilter={this.optionsFilter}>
  26. <Option value="jack">Jack</Option>
  27. <Option value="lucy">Lucy</Option>
  28. <Option value="huazhen">Huazhen</Option>
  29. </Select>
  30. <Button onClick={this.handleClick}>切换过滤条件</Button>
  31. </div>
  32. );
  33. }
  34. }
  35. ReactDOM.render(
  36. <App />,
  37. mountNode
  38. );

Select 选择框 - 图5

只读

只读。

  1. import { DataSet, Select, Row, Col } from 'choerodon-ui/pro';
  2. const { Option } = Select;
  3. const data = [{
  4. 'first-name': 'huazhen',
  5. }];
  6. class App extends React.Component {
  7. ds = new DataSet({
  8. data,
  9. fields: [
  10. { name: 'first-name', readOnly: true },
  11. ],
  12. });
  13. render() {
  14. return (
  15. <Row gutter={10}>
  16. <Col span={12}>
  17. <Select name="last-name" placeholder="请选择" readOnly defaultValue="jack">
  18. <Option value="jack">Jack</Option>
  19. <Option value="lucy">Lucy</Option>
  20. <Option value="wu">Wu</Option>
  21. </Select>
  22. </Col>
  23. <Col span={12}>
  24. <Select dataSet={this.ds} name="first-name" />
  25. </Col>
  26. </Row>
  27. );
  28. }
  29. }
  30. ReactDOM.render(
  31. <App />,
  32. mountNode
  33. );

Select 选择框 - 图6

多值

通过属性multiple设置为多值。

  1. import { DataSet, Select, Row, Col } from 'choerodon-ui/pro';
  2. function handleChange(value, oldValue) {
  3. console.log('[multiple]', value, '[oldValue]', oldValue);
  4. }
  5. function handleDataSetChange({ record, name, value, oldValue }) {
  6. console.log('[dataset multiple]', value, '[oldValue]', oldValue, `[record.get('${name}')]`, record.get(name));
  7. }
  8. const { Option } = Select;
  9. const data = [{
  10. user: ['wu'],
  11. }];
  12. class App extends React.Component {
  13. ds = new DataSet({
  14. data,
  15. fields: [
  16. { name: 'user', type: 'string', textField: 'text', label: '用户', multiple: true },
  17. ],
  18. events: {
  19. update: handleDataSetChange,
  20. },
  21. });
  22. render() {
  23. return (
  24. <Row gutter={10}>
  25. <Col span={24}>
  26. <Select dataSet={this.ds} name="user" placeholder="数据源多选" maxTagCount={2} maxTagTextLength={3} maxTagPlaceholder={restValues => `+${restValues.length}...`}>
  27. <Option value="jack">Jack</Option>
  28. <Option value="lucy">Lucy</Option>
  29. <Option value="wu">Wu</Option>
  30. <Option value="a1">a1</Option>
  31. <Option value="b2">b2</Option>
  32. <Option value="c3">c3</Option>
  33. </Select>
  34. </Col>
  35. <Col span={12}>
  36. <Select multiple placeholder="多选" onChange={handleChange} defaultValue={['jack', 'wu']}>
  37. <Option value="jack">Jack</Option>
  38. <Option value="lucy">Lucy</Option>
  39. <Option value="wu">Wu</Option>
  40. </Select>
  41. </Col>
  42. <Col span={12}>
  43. <Select multiple searchable placeholder="多选+搜索" onChange={handleChange} style={{ height: 30 }}>
  44. <Option value="jack">Jack</Option>
  45. <Option value="lucy">Lucy</Option>
  46. <Option value="wu">Wu</Option>
  47. <Option value="a1">a1</Option>
  48. <Option value="b2">b2</Option>
  49. <Option value="c3">c3</Option>
  50. </Select>
  51. </Col>
  52. <Col span={12}>
  53. <Select multiple combo placeholder="多选+复合" onChange={handleChange}>
  54. <Option value="jack">Jack</Option>
  55. <Option value="lucy">Lucy</Option>
  56. <Option value="wu">Wu</Option>
  57. <Option value="a1">a1</Option>
  58. <Option value="b2">b2</Option>
  59. <Option value="c3">c3</Option>
  60. </Select>
  61. </Col>
  62. <Col span={12}>
  63. <Select multiple combo searchable placeholder="多选+复合+过滤" onChange={handleChange}>
  64. <Option value="jack">Jack</Option>
  65. <Option value="lucy">Lucy</Option>
  66. <Option value="wu">Wu</Option>
  67. <Option value="a1">a1</Option>
  68. <Option value="b2">b2</Option>
  69. <Option value="c3">c3</Option>
  70. </Select>
  71. </Col>
  72. <Col span={12}>
  73. <Select multiple placeholder="多选+禁用" disabled defaultValue={['jack', 'wu']}>
  74. <Option value="jack">Jack</Option>
  75. <Option value="lucy">Lucy</Option>
  76. <Option value="wu">Wu</Option>
  77. <Option value="a1">a1</Option>
  78. <Option value="b2">b2</Option>
  79. <Option value="c3">c3</Option>
  80. </Select>
  81. </Col>
  82. <Col span={12}>
  83. <Select multiple placeholder="多选+只读" readOnly defaultValue={['jack', 'wu']}>
  84. <Option value="jack">Jack</Option>
  85. <Option value="lucy">Lucy</Option>
  86. <Option value="wu">Wu</Option>
  87. <Option value="a1">a1</Option>
  88. <Option value="b2">b2</Option>
  89. <Option value="c3">c3</Option>
  90. </Select>
  91. </Col>
  92. </Row>
  93. );
  94. }
  95. }
  96. ReactDOM.render(
  97. <App />,
  98. mountNode
  99. );

Select 选择框 - 图7

非原始值

非原始值。

  1. import { Select } from 'choerodon-ui/pro';
  2. function handleChange(value, oldValue) {
  3. console.log('[primitive new]', value, '[basic old]', oldValue);
  4. }
  5. const { Option } = Select;
  6. ReactDOM.render(
  7. <Select placeholder="请选择" primitiveValue={false} onChange={handleChange}>
  8. <Option value="jack">Jack</Option>
  9. <Option value="lucy">Lucy</Option>
  10. <Option value="wu">Wu</Option>
  11. </Select>,
  12. mountNode
  13. );

Select 选择框 - 图8

受控下拉选择器

受控下拉选择器。

  1. import { Select } from 'choerodon-ui/pro';
  2. const { Option } = Select;
  3. class App extends React.Component {
  4. constructor(props) {
  5. super(props);
  6. this.state = {
  7. value: 'wu',
  8. };
  9. }
  10. handleChange = (value, oldValue) => {
  11. console.log('[constrolled]', 'value', value, 'oldValue', oldValue);
  12. this.setState({
  13. value,
  14. });
  15. }
  16. render() {
  17. return (
  18. <Select name="last-name" placeholder="请选择" value={this.state.value} onChange={this.handleChange}>
  19. <Option value="jack">Jack</Option>
  20. <Option value="lucy">Lucy</Option>
  21. <Option value="wu">Wu</Option>
  22. </Select>
  23. );
  24. }
  25. }
  26. ReactDOM.render(
  27. <App />,
  28. mountNode
  29. );

Select 选择框 - 图9

绑定数据源

下拉选择器。

  1. import { DataSet, Select } from 'choerodon-ui/pro';
  2. function handleDataSetChange({ record, name, value, oldValue }) {
  3. console.log('[dataset newValue]', value, '[oldValue]', oldValue, `[record.get('${name}')]`, record.get(name));
  4. }
  5. const { Option } = Select;
  6. const data = [{
  7. user: 'wu',
  8. }];
  9. class App extends React.Component {
  10. ds = new DataSet({
  11. data,
  12. fields: [
  13. { name: 'user', type: 'string', textField: 'text', label: '用户' },
  14. ],
  15. events: {
  16. update: handleDataSetChange,
  17. },
  18. });
  19. render() {
  20. return (
  21. <Select dataSet={this.ds} name="user">
  22. <Option value="jack">Jack</Option>
  23. <Option value="lucy">Lucy</Option>
  24. <Option value="wu">Wu</Option>
  25. </Select>
  26. );
  27. }
  28. }
  29. ReactDOM.render(
  30. <App />,
  31. mountNode
  32. );

Select 选择框 - 图10

分组

有两种方式进行选项分组:

  • OptGroup 进行选项分组。

  • 为OptionDs的Field添加group属性,属性值为从1开始的序数,用来指定分组的优先级。

  1. import { Select } from 'choerodon-ui/pro';
  2. const { Option, OptGroup } = Select;
  3. ReactDOM.render(
  4. <Select>
  5. <OptGroup label="Manager">
  6. <Option value="jack">Jack</Option>
  7. <Option value="lucy">Lucy</Option>
  8. </OptGroup>
  9. <OptGroup label="Engineer">
  10. <Option value="wu">Wu</Option>
  11. </OptGroup>
  12. </Select>,
  13. mountNode
  14. );

Select 选择框 - 图11

可搜索

可搜索。

  1. import { DataSet, Select, Row, Col } from 'choerodon-ui/pro';
  2. function handleDataSetChange({ record, name, value, oldValue }) {
  3. console.log(
  4. '[searchable]',
  5. value,
  6. '[oldValue]',
  7. oldValue,
  8. `[record.get('${name}')]`,
  9. record.get(name),
  10. );
  11. }
  12. const { Option } = Select;
  13. const data = [
  14. {
  15. 'last-name': 'huazhen',
  16. },
  17. ];
  18. function searchMatcher({ record, text }) {
  19. return record.get('value').indexOf(text) !== -1;
  20. }
  21. class App extends React.Component {
  22. ds = new DataSet({
  23. data,
  24. fields: [
  25. { name: 'last-name', type: 'string' },
  26. { name: 'first-name', type: 'string' },
  27. { name: 'sex', type: 'string', lookupCode: 'HR.EMPLOYEE_GENDER' },
  28. ],
  29. events: {
  30. update: handleDataSetChange,
  31. },
  32. });
  33. render() {
  34. return (
  35. <Row>
  36. <Col span={8}>
  37. <Select dataSet={this.ds} name="last-name" searchable>
  38. <Option value="jack">Jack</Option>
  39. <Option value="lucy">Lucy</Option>
  40. <Option value="huazhen">Huazhen</Option>
  41. <Option value="aaa">Huazhen</Option>
  42. </Select>
  43. </Col>
  44. <Col span={8}>
  45. <Select dataSet={this.ds} name="first-name" searchable searchMatcher={searchMatcher}>
  46. <Option value="jack">Jack</Option>
  47. <Option value="lucy">Lucy</Option>
  48. <Option value="huazhen">Huazhen</Option>
  49. <Option value="aaa">Huazhen</Option>
  50. </Select>
  51. </Col>
  52. <Col span={8}>
  53. <Select dataSet={this.ds} name="sex" searchable searchMatcher="key" />
  54. </Col>
  55. </Row>
  56. );
  57. }
  58. }
  59. ReactDOM.render(<App />, mountNode);

Select 选择框 - 图12

复合下拉框

复合下拉框。

  1. import { DataSet, Select, Row, Col } from 'choerodon-ui/pro';
  2. function handleChange(value) {
  3. console.log('[combo]', value);
  4. }
  5. const { Option } = Select;
  6. class App extends React.Component {
  7. state = {
  8. value: 'fox',
  9. }
  10. ds = new DataSet({
  11. autoCreate: true,
  12. fields: [
  13. { name: 'name', defaultValue: 'fox2' },
  14. ],
  15. });
  16. handleChange = (value) => {
  17. console.log('[combo]', value);
  18. this.setState({
  19. value,
  20. });
  21. this.ds.current.set('name', value);
  22. }
  23. render() {
  24. return (
  25. <Row gutter={10}>
  26. <Col span={12}>
  27. <Select placeholder="复合" onChange={this.handleChange} combo value={this.state.value}>
  28. <Option value="jack">Jack</Option>
  29. <Option value="lucy">Lucy</Option>
  30. <Option value="wu">Wu</Option>
  31. </Select>
  32. </Col>
  33. <Col span={12}>
  34. <Select dataSet={this.ds} name="name" placeholder="复合+可搜索" onChange={handleChange} combo searchable>
  35. <Option value="jack">Jack</Option>
  36. <Option value="lucy">Lucy</Option>
  37. <Option value="wu">Wu</Option>
  38. </Select>
  39. </Col>
  40. </Row>
  41. );
  42. }
  43. }
  44. ReactDOM.render(
  45. <App />,
  46. mountNode
  47. );

Select 选择框 - 图13

选项级联

选项级联。

  1. import { DataSet, Select, Row, Col } from 'choerodon-ui/pro';
  2. function handleDataSetChange({ record, value, oldValue }) {
  3. console.log('[dataset]', value, '[oldValue]', oldValue, '[record.toJSONData()]', record.toJSONData());
  4. }
  5. class App extends React.Component {
  6. ds = new DataSet({
  7. autoCreate: true,
  8. fields: [
  9. {
  10. name: 'sheng',
  11. label: '省',
  12. lookupCode: 'SHENG',
  13. valueField: 'codeValueId',
  14. defaultValue: '10206',
  15. },
  16. {
  17. name: 'shi',
  18. type: 'number',
  19. valueField: 'codeValueId',
  20. label: '市',
  21. lookupCode: 'SHI',
  22. cascadeMap: { parentCodeValueId: 'sheng' },
  23. },
  24. ],
  25. events: {
  26. update: handleDataSetChange,
  27. },
  28. });
  29. render() {
  30. return (
  31. <Row gutter={10}>
  32. <Col span={12}>
  33. <Select dataSet={this.ds} name="sheng" />
  34. </Col>
  35. <Col span={12}>
  36. <Select dataSet={this.ds} name="shi" />
  37. </Col>
  38. </Row>
  39. );
  40. }
  41. }
  42. ReactDOM.render(
  43. <App />,
  44. mountNode
  45. );

Select 选择框 - 图14

optionRenderer 输入属性

使用optionRenderer属性。

  1. import { DataSet, Select, Button, Row, Col, Menu, Tooltip, Icon } from 'choerodon-ui/pro';
  2. const Item = Menu.Item;
  3. const App = () => {
  4. const optionDs = new DataSet({
  5. selection: 'single',
  6. queryUrl: '/dataset/user/queries',
  7. autoQuery: true,
  8. });
  9. const ds = new DataSet({
  10. fields: [
  11. {
  12. name: 'user',
  13. type: 'string',
  14. textField: 'name',
  15. valueField: 'userid',
  16. label: '用户',
  17. options: optionDs,
  18. },
  19. ],
  20. });
  21. const renderer = ({ text }) => (
  22. <div style={{ width: '100%' }}>
  23. {text && <Icon type="people" />} {text}
  24. </div>
  25. );
  26. const optionRenderer = ({ text }) => (
  27. <Tooltip title={text} placement="left">
  28. {renderer({ text })}
  29. </Tooltip>
  30. );
  31. return (
  32. <Row gutter={10}>
  33. <Col span={8}>
  34. <Select dataSet={ds} name="user" optionRenderer={optionRenderer} renderer={renderer} />
  35. </Col>
  36. </Row>
  37. );
  38. };
  39. ReactDOM.render(<App />, mountNode);

API

Select

参数说明类型默认值
combo复合输入值booleanfalse
searchable是否可搜索booleanfalse
searchMatcher搜索器。当为字符串时,作为 lookup 的参数名来重新请求值列表。string | ({ record, text, textField, valueField }) => boolean({ record, text, textField }) => record.get(textField).indexOf(text) !== -1
optionsFilter选项过滤(record) => boolean
checkValueOnOptionsChange当选项改变时,检查并清除不在选项中的值booleantrue
dropdownMatchSelectWidth下拉框匹配输入框宽度booleantrue
dropdownMenuStyle下拉框菜单样式名object
options下拉框选项数据源DataSet
primitiveValue是否为原始值(建议以绑定的数据源 Field 的 type 来决定值类型,如 type 设为 object 相当于 primitiveValue 设为 false)true - 选项中 valueField 对应的值 false - 选项值对象boolean
optionRenderer渲染 Option 本文的钩子({ record, text, value }) => ReactNode
notFoundContent当下拉列表为空时显示的内容ReactNode
onOption设置选项属性,如 disabled({ dataSet, record })) => object

更多属性请参考 TriggerField

Select.OptGroup

参数说明类型默认值
label选项组标题string

Select.Option

参数说明类型默认值
value选项值any
disabled禁用boolean