Select 选择框

下拉选择器表单控件。

何时使用

  • 弹出一个下拉菜单给用户选择操作,用于代替原生的选择器,或者需要一个更优雅的多选器时。
  • 当选项少时(少于 5 项),建议直接将选项平铺,使用 SelectBox 是更好的选择。

数据源使用相关教程:Select

代码演示

基本使用

下拉选择器。

Select选择框 - 图1

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

受控下拉选择器

受控下拉选择器。

Select选择框 - 图2

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

值列表代码

值列表代码。

Select选择框 - 图3

  1. import React from 'react';
  2. import ReactDOM from 'react-dom';
  3. import { DataSet, Select, Button, Row, Col } from 'choerodon-ui/pro';
  4. function handleDataSetChange({ record, name, value, oldValue }) {
  5. console.log(
  6. '[dataset newValue]',
  7. value,
  8. '[oldValue]',
  9. oldValue,
  10. `[record.get('${name}')]`,
  11. record.get(name),
  12. );
  13. }
  14. function handleOption({ record }) {
  15. return {
  16. disabled: record.index === 0,
  17. };
  18. }
  19. class App extends React.Component {
  20. flag = false;
  21. ds = new DataSet({
  22. autoCreate: true,
  23. fields: [
  24. { name: 'sex', type: 'string', lookupCode: 'HR.EMPLOYEE_GENDER' },
  25. {
  26. name: 'sex2',
  27. type: 'string',

绑定数据源

下拉选择器。

Select选择框 - 图4

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

数据源选项

数据源选项。

Select选择框 - 图5

  1. import React from 'react';
  2. import ReactDOM from 'react-dom';
  3. import { DataSet, Select, Row, Col, Button } from 'choerodon-ui/pro';
  4. function handleDataSetChange({ record, name, value, oldValue }) {
  5. console.log(
  6. '[dataset newValue]',
  7. value,
  8. '[oldValue]',
  9. oldValue,
  10. `[record.get('${name}')]`,
  11. record.get(name),
  12. );
  13. }
  14. class App extends React.Component {
  15. optionDs = new DataSet({
  16. selection: 'single',
  17. queryUrl: '/dataset/user/queries',
  18. autoQuery: true,
  19. });
  20. ds = new DataSet({
  21. fields: [
  22. {
  23. name: 'user',
  24. type: 'string',
  25. textField: 'name',
  26. valueField: 'userid',

分组

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

  1. OptGroup 进行选项分组。
  2. 为OptionDs的Field添加group属性,属性值为从1开始的序数,用来指定分组的优先级。

Select选择框 - 图6

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

过滤

通过属性optionsFilter过滤选项。

Select选择框 - 图7

  1. import React from 'react';
  2. import ReactDOM from 'react-dom';
  3. import { DataSet, Select, Button } from 'choerodon-ui/pro';
  4. const { Option } = Select;
  5. const data = [
  6. {
  7. 'last-name': 'zhangsan',
  8. },
  9. ];
  10. class App extends React.Component {
  11. state = {
  12. key: 'a',
  13. };
  14. ds = new DataSet({
  15. data,
  16. fields: [{ name: 'last-name', type: 'string', label: '姓' }],
  17. });
  18. handleClick = () => {
  19. this.setState({ key: this.state.key === 'a' ? 'c' : 'a' });
  20. };
  21. optionsFilter = (record) => {
  22. return record.get('meaning').toLowerCase().indexOf(this.state.key) !== -1;
  23. };

可搜索

可搜索。

Select选择框 - 图8

  1. import React from 'react';
  2. import ReactDOM from 'react-dom';
  3. import { DataSet, Select, Row, Col } from 'choerodon-ui/pro';
  4. function handleDataSetChange({ record, name, value, oldValue }) {
  5. console.log(
  6. '[searchable]',
  7. value,
  8. '[oldValue]',
  9. oldValue,
  10. `[record.get('${name}')]`,
  11. record.get(name),
  12. );
  13. }
  14. const { Option } = Select;
  15. const data = [
  16. {
  17. 'last-name': 'zhangsan',
  18. },
  19. ];
  20. function searchMatcher({ record, text }) {
  21. return record.get('value').indexOf(text) !== -1;
  22. }
  23. class App extends React.Component {
  24. ds = new DataSet({

只读

只读。

Select选择框 - 图9

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

复合下拉框

复合下拉框。

Select选择框 - 图10

  1. import React from 'react';
  2. import ReactDOM from 'react-dom';
  3. import { DataSet, Select, Row, Col } from 'choerodon-ui/pro';
  4. function handleChange(value) {
  5. console.log('[combo]', value);
  6. }
  7. const { Option } = Select;
  8. class App extends React.Component {
  9. state = {
  10. value: 'fox',
  11. }
  12. ds = new DataSet({
  13. autoCreate: true,
  14. fields: [
  15. { name: 'name', defaultValue: 'fox2' },
  16. ],
  17. });
  18. handleChange = (value) => {
  19. console.log('[combo]', value);
  20. this.setState({
  21. value,
  22. });
  23. this.ds.current.set('name', value);
  24. }

多值

通过属性multiple设置为多值。

Select选择框 - 图11

  1. import React from 'react';
  2. import ReactDOM from 'react-dom';
  3. import { DataSet, Select, Row, Col } from 'choerodon-ui/pro';
  4. function handleChange(value, oldValue) {
  5. console.log('[multiple]', value, '[oldValue]', oldValue);
  6. }
  7. function handleDataSetChange({ record, name, value, oldValue }) {
  8. console.log(
  9. '[dataset multiple]',
  10. value,
  11. '[oldValue]',
  12. oldValue,
  13. `[record.get('${name}')]`,
  14. record.get(name),
  15. );
  16. }
  17. const { Option } = Select;
  18. const data = [
  19. {
  20. user: ['wu'],
  21. },
  22. ];
  23. class App extends React.Component {
  24. ds = new DataSet({
  25. data,
  26. fields: [
  27. {
  28. name: 'user',
  29. type: 'string',
  30. textField: 'text',
  31. label: '用户',

选项级联

选项级联。

Select选择框 - 图12

  1. import React from 'react';
  2. import ReactDOM from 'react-dom';
  3. import { DataSet, Select, Row, Col } from 'choerodon-ui/pro';
  4. function handleDataSetChange({ record, value, oldValue }) {
  5. console.log('[dataset]', value, '[oldValue]', oldValue, '[record.toJSONData()]', record.toJSONData());
  6. }
  7. class App extends React.Component {
  8. ds = new DataSet({
  9. autoCreate: true,
  10. fields: [
  11. {
  12. name: 'sheng',
  13. label: '省',
  14. lookupCode: 'SHENG',
  15. valueField: 'codeValueId',
  16. defaultValue: '10206',
  17. },
  18. {
  19. name: 'shi',
  20. type: 'number',
  21. valueField: 'codeValueId',
  22. label: '市',
  23. lookupCode: 'SHI',
  24. cascadeMap: { parentCodeValueId: 'sheng' },
  25. },
  26. ],
  27. events: {

非原始值

非原始值。

Select选择框 - 图13

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

optionRenderer 输入属性

使用optionRenderer属性。

Select选择框 - 图14

  1. import React from 'react';
  2. import ReactDOM from 'react-dom';
  3. import { DataSet, Select, Button, Row, Col, Menu, Tooltip, Icon } from 'choerodon-ui/pro';
  4. const Item = Menu.Item;
  5. const App = () => {
  6. const optionDs = new DataSet({
  7. selection: 'single',
  8. queryUrl: '/dataset/user/queries',
  9. autoQuery: true,
  10. });
  11. const ds = new DataSet({
  12. fields: [
  13. {
  14. name: 'user',
  15. type: 'string',
  16. textField: 'name',
  17. valueField: 'userid',
  18. label: '用户',
  19. options: optionDs,
  20. },
  21. ],
  22. });
  23. const renderer = ({ text }) => (
  24. <div style={{ width: '100%' }}>
  25. {text && <Icon type="people" />} {text}

常用项

设置常用项。

Select选择框 - 图15

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

DataSet group 分组

DataSet group 分组。

Select选择框 - 图16

  1. import React from 'react';
  2. import ReactDOM from 'react-dom';
  3. import { DataSet, Select, Row, Col } from 'choerodon-ui/pro';
  4. const App = () => {
  5. const optionDs = new DataSet({
  6. selection: 'single',
  7. queryUrl: '/common/lov/dataset/LOV_CODE',
  8. fields: [{ name: 'enabledFlag', type: 'string', group: 0 }],
  9. autoQuery: true,
  10. });
  11. const ds = new DataSet({
  12. fields: [
  13. {
  14. name: 'code',
  15. type: 'string',
  16. textField: 'description',
  17. valueField: 'code',
  18. label: '用户',
  19. options: optionDs,
  20. },
  21. ],
  22. });
  23. return (
  24. <Row gutter={10}>
  25. <Col span={8}>
  26. <Select dataSet={ds} name="code" />
  27. </Col>
  28. </Row>
  29. );

API

Select

参数说明类型默认值
combo复合输入值booleanfalse
searchable是否可搜索booleanfalse
reverse是否显示多选反向booleantrue
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
commonItem设置常用项Array<string>
maxCommonTagPlaceholder设置常用项标签超出最大数量时的占位描述ReactNode | (restValues) => ReactNode
maxCommonTagCount设置常用项标签最大数量number
maxCommonTagTextLength设置常用项标签文案最大长度number
noCache下拉时自动重新查询,不缓存下拉数据源boolean
selectAllButton多选模式下,是否显示全选按钮booleantrue

更多属性请参考 TriggerField

Select.OptGroup

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

Select.Option

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