Select选择框
表单控件。
何时使用
弹出一个下拉菜单给用户选择操作,用于代替原生的选择器,或者需要一个更优雅的多选器时。
当选项少时(少于 5 项),建议直接将选项平铺,使用 SelectBox 是更好的选择。
代码演示
下拉选择器。
import { Select, Button, Row, Col } from 'choerodon-ui/pro';
function handleChange(value, oldValue) {
console.log('[basic new]', value, '[basic old]', oldValue);
}
const { Option } = Select;
class App extends React.Component {
state = {
visible: true,
};
handleClick = () => this.setState({ visible: !this.state.visible });
render() {
return (
<Row gutter={10}>
<Col span={8}>
<Select placeholder="请选择" onChange={handleChange}>
<Option value="jack">Jack</Option>
<Option value="lucy">Lucy</Option>
{this.state.visible && (
<Option value="wu" disabled>
Wu
</Option>
)}
</Select>
</Col>
<Col span={4}>
<Button onClick={this.handleClick}>修改选项</Button>
</Col>
<Col span={12}>
<Select placeholder="请选择" disabled>
<Option value="jack">Jack</Option>
<Option value="lucy">Lucy</Option>
<Option value="wu">Wu</Option>
</Select>
</Col>
</Row>
);
}
}
ReactDOM.render(<App />, mountNode);
值列表代码。
import { DataSet, Select, Button, Row, Col } from 'choerodon-ui/pro';
function handleDataSetChange({ record, name, value, oldValue }) {
console.log(
'[dataset newValue]',
value,
'[oldValue]',
oldValue,
`[record.get('${name}')]`,
record.get(name),
);
}
function handleOption({ record }) {
return {
disabled: record.index === 0,
};
}
class App extends React.Component {
flag = false;
ds = new DataSet({
autoCreate: true,
fields: [
{ name: 'sex', type: 'string', lookupCode: 'HR.EMPLOYEE_GENDER' },
{
name: 'sex2',
type: 'string',
dynamicProps: {
lookupAxiosConfig: ({ record }) => ({
url: record.get('sex') ? '/common/code/HR.EMPLOYEE_GENDER/' : null,
transformResponse(data) {
console.log('transformResponse', data);
return data;
},
}),
},
},
{ name: 'lov', type: 'string', lovCode: 'LOV_CODE', defaultValue: 'SYS.PROFILE_LEVEL_ID' },
{
name: 'lov2',
type: 'string',
lovCode: 'LOV_CODE',
defaultValue: ['SYS.PROFILE_LEVEL_ID', 'SYS.RESOURCE_TYPE'],
multiple: true,
},
],
events: {
update: handleDataSetChange,
},
});
changeLookupCode = () => {
this.flag = !this.flag;
this.ds.getField('sex').set('lookupCode', this.flag ? 'SYS.USER_STATUS' : 'HR.EMPLOYEE_GENDER');
};
render() {
return (
<Row gutter={10}>
<Col span={6}>
<Select dataSet={this.ds} name="sex" placeholder="请选择" onOption={handleOption} />
</Col>
<Col span={6}>
<Button onClick={this.changeLookupCode}>修改lookupCode</Button>
</Col>
<Col span={12}>
<Select dataSet={this.ds} name="sex2" placeholder="请选择" />
</Col>
<Col span={12}>
<Select dataSet={this.ds} name="lov" placeholder="请选择" />
</Col>
<Col span={12}>
<Select dataSet={this.ds} name="lov2" placeholder="请选择" />
</Col>
</Row>
);
}
}
ReactDOM.render(<App />, mountNode);
数据源选项。
import { DataSet, Select, Row, Col, Button } from 'choerodon-ui/pro';
function handleDataSetChange({ record, name, value, oldValue }) {
console.log(
'[dataset newValue]',
value,
'[oldValue]',
oldValue,
`[record.get('${name}')]`,
record.get(name),
);
}
class App extends React.Component {
optionDs = new DataSet({
selection: 'single',
queryUrl: '/dataset/user/queries',
autoQuery: true,
});
ds = new DataSet({
fields: [
{
name: 'user',
type: 'string',
textField: 'name',
valueField: 'userid',
label: '用户',
options: this.optionDs,
},
],
events: {
update: handleDataSetChange,
},
});
changeOptions = () => {
this.ds.addField('account', {
name: 'account',
type: 'string',
textField: 'name',
valueField: 'userid',
label: '账户',
options: this.optionDs,
});
};
render() {
return (
<Row gutter={10}>
<Col span={8}>
<Select
multiple
optionsFilter={record => record.get('sex') === 'F'}
dataSet={this.ds}
name="user"
/>
</Col>
<Col span={8}>
<Select multiple dataSet={this.ds} name="account" />
</Col>
<Col span={8}>
<Button onClick={this.changeOptions}>切换选项</Button>
</Col>
</Row>
);
}
}
ReactDOM.render(<App />, mountNode);
通过属性optionsFilter
过滤选项。
import { DataSet, Select, Button } from 'choerodon-ui/pro';
const { Option } = Select;
const data = [{
'last-name': 'huazhen',
}];
class App extends React.Component {
state = {
key: 'a',
};
ds = new DataSet({
data,
fields: [
{ name: 'last-name', type: 'string', label: '姓' },
],
});
handleClick = () => {
this.setState({ key: this.state.key === 'a' ? 'c' : 'a' });
};
optionsFilter = (record) => {
return record.get('meaning').toLowerCase().indexOf(this.state.key) !== -1;
};
render() {
return (
<div>
<Select dataSet={this.ds} name="last-name" optionsFilter={this.optionsFilter}>
<Option value="jack">Jack</Option>
<Option value="lucy">Lucy</Option>
<Option value="huazhen">Huazhen</Option>
</Select>
<Button onClick={this.handleClick}>切换过滤条件</Button>
</div>
);
}
}
ReactDOM.render(
<App />,
mountNode
);
只读。
import { DataSet, Select, Row, Col } from 'choerodon-ui/pro';
const { Option } = Select;
const data = [{
'first-name': 'huazhen',
}];
class App extends React.Component {
ds = new DataSet({
data,
fields: [
{ name: 'first-name', readOnly: true },
],
});
render() {
return (
<Row gutter={10}>
<Col span={12}>
<Select name="last-name" placeholder="请选择" readOnly defaultValue="jack">
<Option value="jack">Jack</Option>
<Option value="lucy">Lucy</Option>
<Option value="wu">Wu</Option>
</Select>
</Col>
<Col span={12}>
<Select dataSet={this.ds} name="first-name" />
</Col>
</Row>
);
}
}
ReactDOM.render(
<App />,
mountNode
);
通过属性multiple
设置为多值。
import { DataSet, Select, Row, Col } from 'choerodon-ui/pro';
function handleChange(value, oldValue) {
console.log('[multiple]', value, '[oldValue]', oldValue);
}
function handleDataSetChange({ record, name, value, oldValue }) {
console.log('[dataset multiple]', value, '[oldValue]', oldValue, `[record.get('${name}')]`, record.get(name));
}
const { Option } = Select;
const data = [{
user: ['wu'],
}];
class App extends React.Component {
ds = new DataSet({
data,
fields: [
{ name: 'user', type: 'string', textField: 'text', label: '用户', multiple: true },
],
events: {
update: handleDataSetChange,
},
});
render() {
return (
<Row gutter={10}>
<Col span={24}>
<Select dataSet={this.ds} name="user" placeholder="数据源多选" maxTagCount={2} maxTagTextLength={3} maxTagPlaceholder={restValues => `+${restValues.length}...`}>
<Option value="jack">Jack</Option>
<Option value="lucy">Lucy</Option>
<Option value="wu">Wu</Option>
<Option value="a1">a1</Option>
<Option value="b2">b2</Option>
<Option value="c3">c3</Option>
</Select>
</Col>
<Col span={12}>
<Select multiple placeholder="多选" onChange={handleChange} defaultValue={['jack', 'wu']}>
<Option value="jack">Jack</Option>
<Option value="lucy">Lucy</Option>
<Option value="wu">Wu</Option>
</Select>
</Col>
<Col span={12}>
<Select multiple searchable placeholder="多选+搜索" onChange={handleChange} style={{ height: 30 }}>
<Option value="jack">Jack</Option>
<Option value="lucy">Lucy</Option>
<Option value="wu">Wu</Option>
<Option value="a1">a1</Option>
<Option value="b2">b2</Option>
<Option value="c3">c3</Option>
</Select>
</Col>
<Col span={12}>
<Select multiple combo placeholder="多选+复合" onChange={handleChange}>
<Option value="jack">Jack</Option>
<Option value="lucy">Lucy</Option>
<Option value="wu">Wu</Option>
<Option value="a1">a1</Option>
<Option value="b2">b2</Option>
<Option value="c3">c3</Option>
</Select>
</Col>
<Col span={12}>
<Select multiple combo searchable placeholder="多选+复合+过滤" onChange={handleChange}>
<Option value="jack">Jack</Option>
<Option value="lucy">Lucy</Option>
<Option value="wu">Wu</Option>
<Option value="a1">a1</Option>
<Option value="b2">b2</Option>
<Option value="c3">c3</Option>
</Select>
</Col>
<Col span={12}>
<Select multiple placeholder="多选+禁用" disabled defaultValue={['jack', 'wu']}>
<Option value="jack">Jack</Option>
<Option value="lucy">Lucy</Option>
<Option value="wu">Wu</Option>
<Option value="a1">a1</Option>
<Option value="b2">b2</Option>
<Option value="c3">c3</Option>
</Select>
</Col>
<Col span={12}>
<Select multiple placeholder="多选+只读" readOnly defaultValue={['jack', 'wu']}>
<Option value="jack">Jack</Option>
<Option value="lucy">Lucy</Option>
<Option value="wu">Wu</Option>
<Option value="a1">a1</Option>
<Option value="b2">b2</Option>
<Option value="c3">c3</Option>
</Select>
</Col>
</Row>
);
}
}
ReactDOM.render(
<App />,
mountNode
);
非原始值。
import { Select } from 'choerodon-ui/pro';
function handleChange(value, oldValue) {
console.log('[primitive new]', value, '[basic old]', oldValue);
}
const { Option } = Select;
ReactDOM.render(
<Select placeholder="请选择" primitiveValue={false} onChange={handleChange}>
<Option value="jack">Jack</Option>
<Option value="lucy">Lucy</Option>
<Option value="wu">Wu</Option>
</Select>,
mountNode
);
受控下拉选择器。
import { Select } from 'choerodon-ui/pro';
const { Option } = Select;
class App extends React.Component {
constructor(props) {
super(props);
this.state = {
value: 'wu',
};
}
handleChange = (value, oldValue) => {
console.log('[constrolled]', 'value', value, 'oldValue', oldValue);
this.setState({
value,
});
}
render() {
return (
<Select name="last-name" placeholder="请选择" value={this.state.value} onChange={this.handleChange}>
<Option value="jack">Jack</Option>
<Option value="lucy">Lucy</Option>
<Option value="wu">Wu</Option>
</Select>
);
}
}
ReactDOM.render(
<App />,
mountNode
);
下拉选择器。
import { DataSet, Select } from 'choerodon-ui/pro';
function handleDataSetChange({ record, name, value, oldValue }) {
console.log('[dataset newValue]', value, '[oldValue]', oldValue, `[record.get('${name}')]`, record.get(name));
}
const { Option } = Select;
const data = [{
user: 'wu',
}];
class App extends React.Component {
ds = new DataSet({
data,
fields: [
{ name: 'user', type: 'string', textField: 'text', label: '用户' },
],
events: {
update: handleDataSetChange,
},
});
render() {
return (
<Select dataSet={this.ds} name="user">
<Option value="jack">Jack</Option>
<Option value="lucy">Lucy</Option>
<Option value="wu">Wu</Option>
</Select>
);
}
}
ReactDOM.render(
<App />,
mountNode
);
有两种方式进行选项分组:
用
OptGroup
进行选项分组。为OptionDs的Field添加group属性,属性值为从1开始的序数,用来指定分组的优先级。
import { Select } from 'choerodon-ui/pro';
const { Option, OptGroup } = Select;
ReactDOM.render(
<Select>
<OptGroup label="Manager">
<Option value="jack">Jack</Option>
<Option value="lucy">Lucy</Option>
</OptGroup>
<OptGroup label="Engineer">
<Option value="wu">Wu</Option>
</OptGroup>
</Select>,
mountNode
);
可搜索。
import { DataSet, Select, Row, Col } from 'choerodon-ui/pro';
function handleDataSetChange({ record, name, value, oldValue }) {
console.log(
'[searchable]',
value,
'[oldValue]',
oldValue,
`[record.get('${name}')]`,
record.get(name),
);
}
const { Option } = Select;
const data = [
{
'last-name': 'huazhen',
},
];
function searchMatcher({ record, text }) {
return record.get('value').indexOf(text) !== -1;
}
class App extends React.Component {
ds = new DataSet({
data,
fields: [
{ name: 'last-name', type: 'string' },
{ name: 'first-name', type: 'string' },
{ name: 'sex', type: 'string', lookupCode: 'HR.EMPLOYEE_GENDER' },
],
events: {
update: handleDataSetChange,
},
});
render() {
return (
<Row>
<Col span={8}>
<Select dataSet={this.ds} name="last-name" searchable>
<Option value="jack">Jack</Option>
<Option value="lucy">Lucy</Option>
<Option value="huazhen">Huazhen</Option>
<Option value="aaa">Huazhen</Option>
</Select>
</Col>
<Col span={8}>
<Select dataSet={this.ds} name="first-name" searchable searchMatcher={searchMatcher}>
<Option value="jack">Jack</Option>
<Option value="lucy">Lucy</Option>
<Option value="huazhen">Huazhen</Option>
<Option value="aaa">Huazhen</Option>
</Select>
</Col>
<Col span={8}>
<Select dataSet={this.ds} name="sex" searchable searchMatcher="key" />
</Col>
</Row>
);
}
}
ReactDOM.render(<App />, mountNode);
复合下拉框。
import { DataSet, Select, Row, Col } from 'choerodon-ui/pro';
function handleChange(value) {
console.log('[combo]', value);
}
const { Option } = Select;
class App extends React.Component {
state = {
value: 'fox',
}
ds = new DataSet({
autoCreate: true,
fields: [
{ name: 'name', defaultValue: 'fox2' },
],
});
handleChange = (value) => {
console.log('[combo]', value);
this.setState({
value,
});
this.ds.current.set('name', value);
}
render() {
return (
<Row gutter={10}>
<Col span={12}>
<Select placeholder="复合" onChange={this.handleChange} combo value={this.state.value}>
<Option value="jack">Jack</Option>
<Option value="lucy">Lucy</Option>
<Option value="wu">Wu</Option>
</Select>
</Col>
<Col span={12}>
<Select dataSet={this.ds} name="name" placeholder="复合+可搜索" onChange={handleChange} combo searchable>
<Option value="jack">Jack</Option>
<Option value="lucy">Lucy</Option>
<Option value="wu">Wu</Option>
</Select>
</Col>
</Row>
);
}
}
ReactDOM.render(
<App />,
mountNode
);
选项级联。
import { DataSet, Select, Row, Col } from 'choerodon-ui/pro';
function handleDataSetChange({ record, value, oldValue }) {
console.log('[dataset]', value, '[oldValue]', oldValue, '[record.toJSONData()]', record.toJSONData());
}
class App extends React.Component {
ds = new DataSet({
autoCreate: true,
fields: [
{
name: 'sheng',
label: '省',
lookupCode: 'SHENG',
valueField: 'codeValueId',
defaultValue: '10206',
},
{
name: 'shi',
type: 'number',
valueField: 'codeValueId',
label: '市',
lookupCode: 'SHI',
cascadeMap: { parentCodeValueId: 'sheng' },
},
],
events: {
update: handleDataSetChange,
},
});
render() {
return (
<Row gutter={10}>
<Col span={12}>
<Select dataSet={this.ds} name="sheng" />
</Col>
<Col span={12}>
<Select dataSet={this.ds} name="shi" />
</Col>
</Row>
);
}
}
ReactDOM.render(
<App />,
mountNode
);
使用optionRenderer
属性。
import { DataSet, Select, Button, Row, Col, Menu, Tooltip, Icon } from 'choerodon-ui/pro';
const Item = Menu.Item;
const App = () => {
const optionDs = new DataSet({
selection: 'single',
queryUrl: '/dataset/user/queries',
autoQuery: true,
});
const ds = new DataSet({
fields: [
{
name: 'user',
type: 'string',
textField: 'name',
valueField: 'userid',
label: '用户',
options: optionDs,
},
],
});
const renderer = ({ text }) => (
<div style={{ width: '100%' }}>
{text && <Icon type="people" />} {text}
</div>
);
const optionRenderer = ({ text }) => (
<Tooltip title={text} placement="left">
{renderer({ text })}
</Tooltip>
);
return (
<Row gutter={10}>
<Col span={8}>
<Select dataSet={ds} name="user" optionRenderer={optionRenderer} renderer={renderer} />
</Col>
</Row>
);
};
ReactDOM.render(<App />, mountNode);
API
Select
参数 | 说明 | 类型 | 默认值 |
---|---|---|---|
combo | 复合输入值 | boolean | false |
searchable | 是否可搜索 | boolean | false |
searchMatcher | 搜索器。当为字符串时,作为 lookup 的参数名来重新请求值列表。 | string | ({ record, text, textField, valueField }) => boolean | ({ record, text, textField }) => record.get(textField).indexOf(text) !== -1 |
optionsFilter | 选项过滤 | (record) => boolean | |
checkValueOnOptionsChange | 当选项改变时,检查并清除不在选项中的值 | boolean | true |
dropdownMatchSelectWidth | 下拉框匹配输入框宽度 | boolean | true |
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 |