SelectBox选择框

表单控件。

何时使用

  • 平铺选项便于用户选择操作,作用同Select

代码演示

SelectBox 选择框 - 图1

基本使用

基本使用。

  1. import { SelectBox, 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 } = SelectBox;
  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. <SelectBox 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. </SelectBox>
  24. </Col>
  25. <Col span={4}>
  26. <Button onClick={this.handleClick}>修改选项</Button>
  27. </Col>
  28. <Col span={12}>
  29. <SelectBox disabled>
  30. <Option value="jack">Jack</Option>
  31. <Option value="lucy">Lucy</Option>
  32. <Option value="wu">Wu</Option>
  33. </SelectBox>
  34. </Col>
  35. </Row>
  36. );
  37. }
  38. }
  39. ReactDOM.render(<App />, mountNode);

SelectBox 选择框 - 图2

绑定数据源

绑定数据源。

  1. import { DataSet, SelectBox, Row, Col, Form } 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. console.log(record.toJSONData())
  5. }
  6. const { Option } = SelectBox;
  7. const optionData = [
  8. { text: 'Jack', value: 'jack' },
  9. { text: 'Huazhen', value: 'huazhen' },
  10. { text: 'Lucy', value: 'lucy' },
  11. { text: 'Niu', value: 'jiaqin' },
  12. { text: 'Shao', value: 'shao' },
  13. ];
  14. const data = [{
  15. 'first-name': 'huazhen',
  16. 'last-name': 'wu',
  17. }];
  18. class App extends React.Component {
  19. optionDs = new DataSet({
  20. data: optionData,
  21. selection: 'single',
  22. });
  23. ds = new DataSet({
  24. data,
  25. fields: [
  26. { name: 'first-name', type: 'string', label: '名', textField: 'text', valueField: 'value', options: this.optionDs },
  27. { name: 'last-name', type: 'string', label: '姓', multiple: ',' },
  28. ],
  29. events: {
  30. update: handleDataSetChange,
  31. },
  32. });
  33. render() {
  34. return (
  35. <Form dataSet={this.ds}>
  36. <Row gutter={10}>
  37. <Col span={12}>
  38. <SelectBox name="first-name" />
  39. </Col>
  40. <Col span={12}>
  41. <SelectBox name="last-name">
  42. <Option value="jack">Jack</Option>
  43. <Option value="lucy">Lucy</Option>
  44. <Option value="wu">Wu</Option>
  45. </SelectBox>
  46. </Col>
  47. </Row>
  48. </Form>
  49. );
  50. }
  51. }
  52. ReactDOM.render(
  53. <App />,
  54. mountNode
  55. );

SelectBox 选择框 - 图3

多选

通过属性multiple设置多选。

  1. import { DataSet, SelectBox, 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 } = SelectBox;
  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={12}>
  26. <SelectBox dataSet={this.ds} name="user">
  27. <Option value="jack">Jack</Option>
  28. <Option value="lucy">Lucy</Option>
  29. <Option value="wu">Wu</Option>
  30. </SelectBox>
  31. </Col>
  32. <Col span={12}>
  33. <SelectBox multiple onChange={handleChange}>
  34. <Option value="jack">Jack</Option>
  35. <Option value="lucy">Lucy</Option>
  36. <Option value="wu">Wu</Option>
  37. </SelectBox>
  38. </Col>
  39. </Row>
  40. );
  41. }
  42. }
  43. ReactDOM.render(
  44. <App />,
  45. mountNode
  46. );

SelectBox 选择框 - 图4

垂直排列

垂直排列。

  1. import { SelectBox, Row, Col } from 'choerodon-ui/pro';
  2. function handleChange(value, oldValue) {
  3. console.log('[button new]', value, '[button old]', oldValue);
  4. }
  5. const { Option } = SelectBox;
  6. ReactDOM.render(
  7. <Row gutter={10}>
  8. <Col span={12}>
  9. <SelectBox vertical mode="button" onChange={handleChange}>
  10. <Option value="jack">Jack</Option>
  11. <Option value="lucy">Lucy</Option>
  12. <Option value="wu">Wu</Option>
  13. </SelectBox>
  14. </Col>
  15. <Col span={12}>
  16. <SelectBox vertical mode="button" multiple onChange={handleChange}>
  17. <Option value="jack">Jack</Option>
  18. <Option value="lucy">Lucy</Option>
  19. <Option value="wu">Wu</Option>
  20. </SelectBox>
  21. </Col>
  22. </Row>,
  23. mountNode
  24. );

SelectBox 选择框 - 图5

受控按钮选则框

受控按钮选则框。

  1. import { SelectBox } from 'choerodon-ui/pro';
  2. const { Option } = SelectBox;
  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]', 'newValue', value, '[oldValue]', oldValue);
  12. this.setState({
  13. value,
  14. });
  15. }
  16. render() {
  17. return (
  18. <SelectBox 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. </SelectBox>
  23. );
  24. }
  25. }
  26. ReactDOM.render(
  27. <App />,
  28. mountNode
  29. );

SelectBox 选择框 - 图6

值列表代码

值列表代码。

  1. import { DataSet, SelectBox, 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', defaultValue: 'F' },
  23. { name: 'sex2', type: 'string', lookupUrl: '/common/code/HR.EMPLOYEE_GENDER/' },
  24. { name: 'lov', type: 'string', lovCode: 'LOV_CODE', defaultValue: 'SYS.PROFILE_LEVEL_ID' },
  25. {
  26. name: 'lov2',
  27. type: 'string',
  28. lovCode: 'LOV_CODE',
  29. defaultValue: ['SYS.PROFILE_LEVEL_ID', 'SYS.RESOURCE_TYPE'],
  30. multiple: true,
  31. },
  32. ],
  33. events: {
  34. update: handleDataSetChange,
  35. },
  36. });
  37. changeLookupCode = () => {
  38. this.flag = !this.flag;
  39. this.ds.getField('sex').set('lookupCode', this.flag ? 'SYS.USER_STATUS' : 'HR.EMPLOYEE_GENDER');
  40. };
  41. render() {
  42. return (
  43. <Row gutter={10}>
  44. <Col span={6}>
  45. <SelectBox dataSet={this.ds} name="sex" placeholder="请选择" onOption={handleOption} />
  46. </Col>
  47. <Col span={6}>
  48. <Button onClick={this.changeLookupCode}>修改lookupCode</Button>
  49. </Col>
  50. <Col span={12}>
  51. <SelectBox dataSet={this.ds} name="sex2" placeholder="请选择" />
  52. </Col>
  53. <Col span={12}>
  54. <SelectBox dataSet={this.ds} name="lov" placeholder="请选择" />
  55. </Col>
  56. <Col span={12}>
  57. <SelectBox dataSet={this.ds} name="lov2" placeholder="请选择" />
  58. </Col>
  59. </Row>
  60. );
  61. }
  62. }
  63. ReactDOM.render(<App />, mountNode);

SelectBox 选择框 - 图7

按钮视图

按钮视图。

  1. import { SelectBox, Row, Col } from 'choerodon-ui/pro';
  2. function handleChange(value, oldValue) {
  3. console.log('[button new]', value, '[button old]', oldValue);
  4. }
  5. const { Option } = SelectBox;
  6. ReactDOM.render(
  7. <Row gutter={10}>
  8. <Col span={12}>
  9. <SelectBox mode="button" onChange={handleChange}>
  10. <Option value="jack">Jack</Option>
  11. <Option value="lucy">Lucy</Option>
  12. <Option value="wu">Wu</Option>
  13. </SelectBox>
  14. </Col>
  15. <Col span={12}>
  16. <SelectBox mode="button" multiple onChange={handleChange}>
  17. <Option value="jack">Jack</Option>
  18. <Option value="lucy">Lucy</Option>
  19. <Option value="wu">Wu</Option>
  20. </SelectBox>
  21. </Col>
  22. </Row>,
  23. mountNode
  24. );

SelectBox 选择框 - 图8

只读

只读。

  1. import { DataSet, SelectBox, Row, Col } from 'choerodon-ui/pro';
  2. const { Option } = SelectBox;
  3. const optionData = [
  4. { text: 'Jack', value: 'jack' },
  5. { text: 'Huazhen', value: 'huazhen' },
  6. { text: 'Lucy', value: 'lucy' },
  7. { text: 'Niu', value: 'jiaqin' },
  8. { text: 'Shao', value: 'shao' },
  9. ];
  10. const data = [{
  11. name: 'huazhen',
  12. }];
  13. class App extends React.Component {
  14. optionDs = new DataSet({
  15. data: optionData,
  16. selection: 'single',
  17. });
  18. ds = new DataSet({
  19. data,
  20. fields: [
  21. { name: 'name', textField: 'text', valueField: 'value', options: this.optionDs, readOnly: true },
  22. ],
  23. });
  24. render() {
  25. return (
  26. <Row gutter={10}>
  27. <Col span={12}>
  28. <SelectBox readOnly>
  29. <Option value="jack">Jack</Option>
  30. <Option value="lucy">Lucy</Option>
  31. <Option value="wu">Wu</Option>
  32. </SelectBox>
  33. </Col>
  34. <Col span={12}>
  35. <SelectBox dataSet={this.ds} name="name" />
  36. </Col>
  37. </Row>
  38. );
  39. }
  40. }
  41. ReactDOM.render(
  42. <App />,
  43. mountNode
  44. );

API

SelectBox

参数说明类型默认值
mode选择器的展现形式,可选值 box buttonstring
vertical是否垂直显示booleanfalse

更多属性请参考 Select

SelectBox.OptGroup

该属性未实装。

SelectBox.Option

该属性请参考 Select.Option