Picker 选择器
在一组预设数据中进行选择,e.g. 省市区选择。规则
尽量使用 Picker 来帮助用户完成输入,避免用户通过键盘直接输入。
DatePicker 是 Picker 的特定模式。
代码演示
数据级联选择示例. (rc-form 文档)
import { Picker, List, WhiteSpace } from 'antd-mobile';
import { createForm } from 'rc-form';
import arrayTreeFilter from 'array-tree-filter';
import { district, provinceLite } from 'antd-mobile-demo-data';
// 如果不是使用 List.Item 作为 children
const CustomChildren = props => (
<div
onClick={props.onClick}
style={{ backgroundColor: '#fff', paddingLeft: 15 }}
>
<div className="test" style={{ display: 'flex', height: '45px', lineHeight: '45px' }}>
<div style={{ flex: 1, overflow: 'hidden', textOverflow: 'ellipsis', whiteSpace: 'nowrap' }}>{props.children}</div>
<div style={{ textAlign: 'right', color: '#888', marginRight: 15 }}>{props.extra}</div>
</div>
</div>
);
const seasons = [
[
{
label: '2013',
value: '2013',
},
{
label: '2014',
value: '2014',
},
],
[
{
label: '春',
value: '春',
},
{
label: '夏',
value: '夏',
},
],
];
const colorStyle = {
display: 'inline-block',
verticalAlign: 'middle',
width: '16px',
height: '16px',
marginRight: '10px',
};
const colors = [
{
label:
(<div>
<span
style={{ ...colorStyle, backgroundColor: '#FF0000' }}
/>
<span>红色</span>
</div>),
value: '#FF0000',
},
{
label:
(<div>
<span
style={{ ...colorStyle, backgroundColor: '#00FF00' }}
/>
<span>绿色</span>
</div>),
value: '#00FF00',
},
{
label:
(<div>
<span
style={{ ...colorStyle, backgroundColor: '#0000FF' }}
/>
<span>蓝色</span>
</div>),
value: '#0000FF',
},
];
class Test extends React.Component {
state = {
data: [],
cols: 1,
pickerValue: [],
asyncValue: [],
sValue: ['2013', '春'],
visible: false,
colorValue: ['#00FF00'],
};
onClick = () => {
setTimeout(() => {
this.setState({
data: provinceLite,
});
}, 120);
};
onPickerChange = (val) => {
console.log(val);
let colNum = 1;
const d = [...this.state.data];
const asyncValue = [...val];
if (val[0] === 'zj') {
d.forEach((i) => {
if (i.value === 'zj') {
colNum = 2;
if (!i.children) {
i.children = [{
value: 'zj-nb',
label: '宁波',
}, {
value: 'zj-hz',
label: '杭州',
}];
asyncValue.push('zj-nb');
} else if (val[1] === 'zj-hz') {
i.children.forEach((j) => {
if (j.value === 'zj-hz') {
j.children = [{
value: 'zj-hz-xh',
label: '西湖区',
}];
asyncValue.push('zj-hz-xh');
}
});
colNum = 3;
}
}
});
} else {
colNum = 1;
}
this.setState({
data: d,
cols: colNum,
asyncValue,
});
};
getSel() {
const value = this.state.pickerValue;
if (!value) {
return '';
}
const treeChildren = arrayTreeFilter(district, (c, level) => c.value === value[level]);
return treeChildren.map(v => v.label).join(',');
}
// setVal() {
// this.props.form.setFieldsValue({
// district: ['340000', '340800', '340822'],
// });
// },
onChangeColor = (color) => {
this.setState({
colorValue: color,
});
};
render() {
const { getFieldProps } = this.props.form;
return (<div>
<WhiteSpace size="lg" />
<List style={{ backgroundColor: 'white' }} className="picker-list">
<Picker extra="请选择(可选)"
data={district}
title="Areas"
{...getFieldProps('district', {
initialValue: ['340000', '341500', '341502'],
})}
onOk={e => console.log('ok', e)}
onDismiss={e => console.log('dismiss', e)}
>
<List.Item arrow="horizontal">Multiple & cascader</List.Item>
</Picker>
<Picker
data={seasons}
title="选择季节"
cascade={false}
extra="请选择(可选)"
value={this.state.sValue}
onChange={v => this.setState({ sValue: v })}
onOk={v => this.setState({ sValue: v })}
>
<List.Item arrow="horizontal">Multiple</List.Item>
</Picker>
<Picker data={district} cols={1} {...getFieldProps('district3')} className="forss">
<List.Item arrow="horizontal">Single</List.Item>
</Picker>
<Picker
data={this.state.data}
cols={this.state.cols}
value={this.state.asyncValue}
onPickerChange={this.onPickerChange}
onOk={v => console.log(v)}
>
<List.Item arrow="horizontal" onClick={this.onClick}>Multiple & async</List.Item>
</Picker>
<Picker
title="选择地区"
extra="请选择(可选)"
data={district}
value={this.state.pickerValue}
onChange={v => this.setState({ pickerValue: v })}
onOk={v => this.setState({ pickerValue: v })}
>
<CustomChildren>Customized children</CustomChildren>
</Picker>
<Picker
visible={this.state.visible}
data={district}
value={this.state.pickerValue}
onChange={v => this.setState({ pickerValue: v })}
onOk={() => this.setState({ visible: false })}
onDismiss={() => this.setState({ visible: false })}
>
<List.Item extra={this.getSel()} onClick={() => this.setState({ visible: true })}>
Visible state
</List.Item>
</Picker>
<Picker
data={colors}
value={this.state.colorValue}
cols={1}
onChange={this.onChangeColor}
>
<List.Item arrow="horizontal">Complex Labels</List.Item>
</Picker>
</List>
</div>);
}
}
const TestWrapper = createForm()(Test);
ReactDOM.render(<TestWrapper />, mountNode);
.picker-list .am-list-item .am-list-line .am-list-extra {
flex-basis: initial;
}
.test {
position: relative;
border-bottom: 0;
}
.test:after {
content: '';
position: absolute;
background-color: #ddd;
display: block;
z-index: 1;
top: auto;
right: auto;
bottom: 0;
left: 0;
width: 100%;
height: 1PX;
-webkit-transform-origin: 50% 100%;
-ms-transform-origin: 50% 100%;
transform-origin: 50% 100%;
-webkit-transform: scaleY(0.5);
-ms-transform: scaleY(0.5);
transform: scaleY(0.5);
}
API
属性 | 说明 | 类型 | 默认值 |
---|---|---|---|
data | 数据源 | Array<{value, label, children: Array}> | - |
value | 值, 格式是[value1, value2, value3] , 对应数据源的相应级层value | Array | - |
format | 格式化选中目标的函数 | (labels: React.ReactNode[]): any | if label is string: (labels) => { return labels.join(',');} else: (labels) => { return labels;} |
cols | 列数 | Number | 3 |
onChange | 选中后的回调,可使用rc-form | (val): void | - |
onPickerChange | 每列数据选择变化后的回调函数 | (val): void | - |
onVisibleChange | 当显隐状态变化时回调函数 | (visible: bool): void | - |
itemStyle | 每列样式 | Object | - |
indicatorStyle | indicator 样式 | Object | - |
children | 通常是 List.Item | Object | List.Item |
okText | 选中的文案 | String | 确定 |
dismissText | 取消选中的文案 | String | 取消 |
onOk | 点击选中时执行的回调 | (val): void | 无 |
onDismiss | 点击取消时执行的回调 | (): void | 无 |
title | 大标题 | String | - |
extra | Picker children 建议是 List.Item , 如果不是,需要是自定义组件(组件内需处理onClick /extra 属性) | String | 请选择 |
disabled | 是否不可用 | Boolean | false |
cascade | 是否联动 | Boolean | true |