Picker 选择器

在一组预设数据中进行选择,e.g. 省市区选择。

规则

  • 尽量使用 Picker 来帮助用户完成输入,避免用户通过键盘直接输入。

  • DatePicker 是 Picker 的特定模式。

代码演示

基本

数据级联选择示例. (rc-form 文档)

  1. import { Picker, List, WhiteSpace } from 'antd-mobile';
  2. import { createForm } from 'rc-form';
  3. import arrayTreeFilter from 'array-tree-filter';
  4. import { district, provinceLite as province } from 'antd-mobile-demo-data';
  5. // 如果不是使用 List.Item 作为 children
  6. const CustomChildren = props => (
  7. <div
  8. onClick={props.onClick}
  9. style={{ backgroundColor: '#fff', paddingLeft: '0.3rem' }}
  10. >
  11. <div style={{ display: 'flex', height: '0.9rem', lineHeight: '0.9rem', borderBottom: '1PX solid #ddd' }}>
  12. <div style={{ flex: 1, overflow: 'hidden', textOverflow: 'ellipsis', whiteSpace: 'nowrap' }}>{props.children}</div>
  13. <div style={{ textAlign: 'right', color: '#888', marginRight: '0.3rem' }}>{props.extra}</div>
  14. </div>
  15. </div>
  16. );
  17. const seasons = [
  18. [
  19. {
  20. label: '2013',
  21. value: '2013',
  22. },
  23. {
  24. label: '2014',
  25. value: '2014',
  26. },
  27. ],
  28. [
  29. {
  30. label: '春',
  31. value: '春',
  32. },
  33. {
  34. label: '夏',
  35. value: '夏',
  36. },
  37. ],
  38. ];
  39. class Test extends React.Component {
  40. state = {
  41. data: [],
  42. cols: 1,
  43. pickerValue: [],
  44. asyncValue: [],
  45. sValue: ['2013', '春'],
  46. visible: false,
  47. };
  48. onClick = () => {
  49. setTimeout(() => {
  50. this.setState({
  51. data: province,
  52. });
  53. }, 120);
  54. };
  55. onPickerChange = (val) => {
  56. console.log(val);
  57. let colNum = 1;
  58. const d = [...this.state.data];
  59. const asyncValue = [...val];
  60. if (val[0] === 'zj') {
  61. d.forEach((i) => {
  62. if (i.value === 'zj') {
  63. colNum = 2;
  64. if (!i.children) {
  65. i.children = [{
  66. value: 'zj-nb',
  67. label: '宁波',
  68. }, {
  69. value: 'zj-hz',
  70. label: '杭州',
  71. }];
  72. asyncValue.push('zj-nb');
  73. } else if (val[1] === 'zj-hz') {
  74. i.children.forEach((j) => {
  75. if (j.value === 'zj-hz') {
  76. j.children = [{
  77. value: 'zj-hz-xh',
  78. label: '西湖区',
  79. }];
  80. asyncValue.push('zj-hz-xh');
  81. }
  82. });
  83. colNum = 3;
  84. }
  85. }
  86. });
  87. } else {
  88. colNum = 1;
  89. }
  90. this.setState({
  91. data: d,
  92. cols: colNum,
  93. asyncValue,
  94. });
  95. };
  96. getSel() {
  97. const value = this.state.pickerValue;
  98. if (!value) {
  99. return '';
  100. }
  101. const treeChildren = arrayTreeFilter(district, (c, level) => c.value === value[level]);
  102. return treeChildren.map(v => v.label).join(',');
  103. }
  104. // setVal() {
  105. // this.props.form.setFieldsValue({
  106. // district: ['340000', '340800', '340822'],
  107. // });
  108. // },
  109. render() {
  110. const { getFieldProps } = this.props.form;
  111. return (<div>
  112. <WhiteSpace size="lg" />
  113. <List style={{ backgroundColor: 'white' }} className="picker-list">
  114. <Picker extra="请选择(可选)"
  115. data={district}
  116. title="选择地区"
  117. {...getFieldProps('district', {
  118. initialValue: ['340000', '341500', '341502'],
  119. })}
  120. onOk={e => console.log('ok', e)}
  121. onDismiss={e => console.log('dismiss', e)}
  122. >
  123. <List.Item arrow="horizontal">选择地区(多列,联动)</List.Item>
  124. </Picker>
  125. <Picker
  126. data={seasons}
  127. title="选择季节"
  128. cascade={false}
  129. extra="请选择(可选)"
  130. value={this.state.sValue}
  131. onChange={v => this.setState({ sValue: v })}
  132. >
  133. <List.Item arrow="horizontal">选择季节(多列,不联动)</List.Item>
  134. </Picker>
  135. <Picker data={district} cols={1} {...getFieldProps('district3')} className="forss">
  136. <List.Item arrow="horizontal">选择地区(单列)</List.Item>
  137. </Picker>
  138. <Picker
  139. data={this.state.data}
  140. cols={this.state.cols}
  141. value={this.state.asyncValue}
  142. onPickerChange={this.onPickerChange}
  143. >
  144. <List.Item arrow="horizontal" onClick={this.onClick}>选择地区(多列,异步加载)</List.Item>
  145. </Picker>
  146. <Picker
  147. title="选择地区"
  148. extra="请选择(可选)"
  149. data={district}
  150. value={this.state.pickerValue}
  151. onChange={v => this.setState({ pickerValue: v })}
  152. >
  153. <CustomChildren>选择地区(自定义 children)</CustomChildren>
  154. </Picker>
  155. <List.Item extra={this.getSel()}>
  156. <div onClick={() => this.setState({ visible: true })}>外部控制 visible</div>
  157. </List.Item>
  158. <Picker
  159. visible={this.state.visible}
  160. data={district}
  161. value={this.state.pickerValue}
  162. onChange={v => this.setState({ pickerValue: v })}
  163. onOk={() => this.setState({ visible: false })}
  164. onDismiss={() => this.setState({ visible: false })}
  165. />
  166. </List>
  167. </div>);
  168. }
  169. }
  170. const TestWrapper = createForm()(Test);
  171. ReactDOM.render(<TestWrapper />, mountNode);
  1. .picker-list .am-list-item .am-list-line .am-list-extra {
  2. flex-basis: initial;
  3. }

Picker选择器 - 图1

API

适用平台:WEB、React-Native
属性说明类型默认值
data数据源Array<{value, label, children: Array}>-
value值, 格式是[value1, value2, value3], 对应数据源的相应级层valueArray-
format格式化选中值的函数(val): void(values) => { return values.join(','); }
cols列数Number3
onChange选中后的回调,可使用rc-form(val): void-
onPickerChange每列数据选择变化后的回调函数(val): void-
indicatorStyleindicator 样式Object-
itemStyle每列样式Object-
children通常是 List.ItemObjectList.Item
okText选中的文案String确定
dismissText取消选中的文案String取消
onOk点击选中时执行的回调(val): void
onDismiss点击取消时执行的回调(): void
title大标题String-
extraPicker children 建议是 List.Item, 如果不是,需要是自定义组件(组件内需处理onClick/extra属性)String请选择
disabled是否不可用Booleanfalse