Select 选择框
下拉选择器表单控件。
何时使用
- 弹出一个下拉菜单给用户选择操作,用于代替原生的选择器,或者需要一个更优雅的多选器时。
- 当选项少时(少于 5 项),建议直接将选项平铺,使用 SelectBox 是更好的选择。
数据源使用相关教程:Select
代码演示
基本使用
下拉选择器。
import React from 'react';
import ReactDOM from 'react-dom';
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>
)}
受控下拉选择器
受控下拉选择器。
import React from 'react';
import ReactDOM from 'react-dom';
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>
);
值列表代码
值列表代码。
import React from 'react';
import ReactDOM from 'react-dom';
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',
绑定数据源
下拉选择器。
import React from 'react';
import ReactDOM from 'react-dom';
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>
数据源选项
数据源选项。
import React from 'react';
import ReactDOM from 'react-dom';
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',
分组
有两种方式进行选项分组:
- 用
OptGroup
进行选项分组。 - 为OptionDs的Field添加group属性,属性值为从1开始的序数,用来指定分组的优先级。
import React from 'react';
import ReactDOM from 'react-dom';
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>,
document.getElementById('container')
);
过滤
通过属性optionsFilter
过滤选项。
import React from 'react';
import ReactDOM from 'react-dom';
import { DataSet, Select, Button } from 'choerodon-ui/pro';
const { Option } = Select;
const data = [
{
'last-name': 'zhangsan',
},
];
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;
};
可搜索
可搜索。
import React from 'react';
import ReactDOM from 'react-dom';
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': 'zhangsan',
},
];
function searchMatcher({ record, text }) {
return record.get('value').indexOf(text) !== -1;
}
class App extends React.Component {
ds = new DataSet({
只读
只读。
import React from 'react';
import ReactDOM from 'react-dom';
import { DataSet, Select, Row, Col } from 'choerodon-ui/pro';
const { Option } = Select;
const data = [
{
'first-name': 'zhangsan',
},
];
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>
复合下拉框
复合下拉框。
import React from 'react';
import ReactDOM from 'react-dom';
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);
}
多值
通过属性multiple
设置为多值。
import React from 'react';
import ReactDOM from 'react-dom';
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: '用户',
选项级联
选项级联。
import React from 'react';
import ReactDOM from 'react-dom';
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: {
非原始值
非原始值。
import React from 'react';
import ReactDOM from 'react-dom';
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>,
document.getElementById('container')
);
optionRenderer 输入属性
使用optionRenderer
属性。
import React from 'react';
import ReactDOM from 'react-dom';
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}
常用项
设置常用项。
import React from 'react';
import ReactDOM from 'react-dom';
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 maxCommonTagTextLength={5} style={{ width: 300 }} dataSet={this.ds} name="user" commonItem={['jack', 'wu', 'jack22222aaa']}>
<Option value="jack">Jack</Option>
<Option value="lucy">Lucy</Option>
<Option value="jack22222aaa">Kack22222aaasss</Option>
<Option value="wu">Wu</Option>
</Select>
DataSet group 分组
DataSet group 分组。
import React from 'react';
import ReactDOM from 'react-dom';
import { DataSet, Select, Row, Col } from 'choerodon-ui/pro';
const App = () => {
const optionDs = new DataSet({
selection: 'single',
queryUrl: '/common/lov/dataset/LOV_CODE',
fields: [{ name: 'enabledFlag', type: 'string', group: 0 }],
autoQuery: true,
});
const ds = new DataSet({
fields: [
{
name: 'code',
type: 'string',
textField: 'description',
valueField: 'code',
label: '用户',
options: optionDs,
},
],
});
return (
<Row gutter={10}>
<Col span={8}>
<Select dataSet={ds} name="code" />
</Col>
</Row>
);
API
Select
参数 | 说明 | 类型 | 默认值 |
---|---|---|---|
combo | 复合输入值 | boolean | false |
searchable | 是否可搜索 | boolean | false |
reverse | 是否显示多选反向 | boolean | true |
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 | ||
commonItem | 设置常用项 | Array<string> | |
maxCommonTagPlaceholder | 设置常用项标签超出最大数量时的占位描述 | ReactNode | (restValues) => ReactNode | |
maxCommonTagCount | 设置常用项标签最大数量 | number | |
maxCommonTagTextLength | 设置常用项标签文案最大长度 | number | |
noCache | 下拉时自动重新查询,不缓存下拉数据源 | boolean | |
selectAllButton | 多选模式下,是否显示全选按钮 | boolean | true |
更多属性请参考 TriggerField。
Select.OptGroup
参数 | 说明 | 类型 |
---|---|---|
label | 选项组标题 | string |
Select.Option
参数 | 说明 | 类型 |
---|---|---|
value | 选项值 | any |
disabled | 禁用 | boolean |
当前内容版权归 Choerodon UI 或其关联方所有,如需对内容或内容相关联开源项目进行关注与资助,请访问 Choerodon UI .