Picker选择器 - 图1

Picker 选择器

基本用法

  1. import { Cell, Button, Picker, Toast } from 'zarm';
  2. const SINGLE_DATA = [
  3. { value: '1', label: '选项一' },
  4. { value: '2', label: '选项二' },
  5. ];
  6. // 普通多列数据
  7. const MULTI_DATA = [
  8. [
  9. { value: '1', label: '选项一' },
  10. { value: '2', label: '选项二' },
  11. ],
  12. [
  13. { value: '3', label: '选项A' },
  14. { value: '4', label: '选项B' },
  15. ],
  16. ];
  17. // 级联数据
  18. const CASCADE_DATA = [
  19. {
  20. code: '1',
  21. label: '北京市',
  22. children: [
  23. { code: '11', label: '海淀区' },
  24. { code: '12', label: '西城区' },
  25. ],
  26. },
  27. {
  28. code: '2',
  29. label: '上海市',
  30. children: [
  31. { code: '21', label: '杨浦区' },
  32. { code: '22', label: '静安区' },
  33. ],
  34. },
  35. ];
  36. const DIY_DATA = [
  37. {
  38. value: '1',
  39. name: '北京市',
  40. children: [
  41. { value: '11', name: '海淀区' },
  42. { value: '12', name: '西城区' },
  43. ],
  44. },
  45. {
  46. value: '2',
  47. name: '上海市',
  48. children: [
  49. { value: '21', name: '黄埔区' },
  50. { value: '22', name: '虹口区' },
  51. ],
  52. },
  53. ];
  54. class Demo extends React.Component {
  55. state = {
  56. single: {
  57. visible: false,
  58. value: '',
  59. dataSource: SINGLE_DATA,
  60. },
  61. multi: {
  62. visible: false,
  63. value: [],
  64. dataSource: MULTI_DATA,
  65. },
  66. cascade: {
  67. visible: false,
  68. value: [],
  69. dataSource: [],
  70. },
  71. diy: {
  72. visible: false,
  73. value: [],
  74. dataSource: DIY_DATA,
  75. },
  76. specDOM: {
  77. visible: false,
  78. value: '',
  79. dataSource: SINGLE_DATA,
  80. },
  81. count: 0,
  82. };
  83. myRef = React.createRef();
  84. componentDidMount() {
  85. // this.interval = setInterval(
  86. // () =>
  87. // this.setState(prevState => ({
  88. // count: prevState.count + 1
  89. // })),
  90. // 1000
  91. // );
  92. // 异步加载数据源测试
  93. setTimeout(() => {
  94. const { cascade } = this.state;
  95. cascade.dataSource = CASCADE_DATA;
  96. cascade.value = ['1', '12'];
  97. cascade.valueMember = "code";
  98. this.setState({ cascade });
  99. }, 0);
  100. }
  101. toggle = (key) => {
  102. const state = this.state[key];
  103. state.visible = !state.visible;
  104. this.setState({ [`${key}`]: state });
  105. }
  106. render() {
  107. const { single, multi, cascade, diy, specDOM } = this.state;
  108. return (
  109. <>
  110. <Cell
  111. description={
  112. <Button size="xs" onClick={() => this.toggle('single')}>选择</Button>
  113. }
  114. >
  115. 单列
  116. </Cell>
  117. <Cell
  118. description={
  119. <Button size="xs" onClick={() => this.toggle('multi')}>选择</Button>
  120. }
  121. >
  122. 多列
  123. </Cell>
  124. <Cell
  125. description={
  126. <Button size="xs" onClick={() => this.toggle('cascade')}>选择</Button>
  127. }
  128. >
  129. 级联
  130. </Cell>
  131. <Cell
  132. description={
  133. <Button size="xs" onClick={() => this.toggle('diy')}>选择</Button>
  134. }
  135. >
  136. 自定义
  137. </Cell>
  138. <Cell
  139. description={
  140. <Button size="xs" onClick={() => this.toggle('specDOM')}>选择</Button>
  141. }
  142. >
  143. 挂载到指定dom节点
  144. </Cell>
  145. <Picker
  146. visible={single.visible}
  147. value={single.value}
  148. dataSource={single.dataSource}
  149. onOk={(selected) => {
  150. console.log('Picker onOk: ', selected);
  151. single.value = selected.map(item => item.value);
  152. this.setState({ single });
  153. Toast.show(JSON.stringify(selected));
  154. this.toggle('single');
  155. }}
  156. onCancel={() => this.toggle('single')}
  157. />
  158. <Picker
  159. visible={multi.visible}
  160. value={multi.value}
  161. dataSource={multi.dataSource}
  162. onOk={(selected) => {
  163. console.log('Picker onOk: ', selected);
  164. multi.value = selected.map(item => item.value);
  165. this.setState({ multi });
  166. Toast.show(JSON.stringify(selected));
  167. this.toggle('multi');
  168. }}
  169. onCancel={() => this.toggle('multi')}
  170. />
  171. <Picker
  172. visible={cascade.visible}
  173. value={cascade.value}
  174. dataSource={cascade.dataSource}
  175. valueMember={cascade.valueMember}
  176. onOk={(selected) => {
  177. console.log('Picker onOk: ', selected);
  178. cascade.value = selected.map(item => item.code);
  179. this.setState({ cascade });
  180. Toast.show(JSON.stringify(selected));
  181. this.toggle('cascade');
  182. }}
  183. onCancel={() => this.toggle('cascade')}
  184. />
  185. <Picker
  186. visible={diy.visible}
  187. title="custom title"
  188. cancelText="Cancel"
  189. okText="Ok"
  190. dataSource={diy.dataSource}
  191. value={diy.value}
  192. valueMember="value"
  193. itemRender={data => data.name}
  194. onOk={(selected) => {
  195. console.log('Picker onOk: ', selected);
  196. diy.value = selected.map(item => item.value);
  197. this.setState({
  198. diy,
  199. });
  200. Toast.show(JSON.stringify(selected));
  201. this.toggle('diy');
  202. }}
  203. onCancel={() => this.toggle('diy')}
  204. />
  205. <Picker
  206. visible={specDOM.visible}
  207. value={specDOM.value}
  208. dataSource={specDOM.dataSource}
  209. onOk={(selected) => {
  210. console.log('Picker onOk: ', selected);
  211. specDOM.value = selected.map(item => item.value);
  212. this.setState({ specDOM });
  213. Toast.show(JSON.stringify(selected));
  214. this.toggle('specDOM');
  215. }}
  216. onCancel={() => this.toggle('specDOM')}
  217. getContainer={() => this.myRef.current}
  218. />
  219. <div
  220. id="test-div"
  221. style={{ position: 'relative', zIndex: 1 }}
  222. ref={this.myRef}
  223. />
  224. </>
  225. )
  226. }
  227. }
  228. ReactDOM.render(<Demo />, mountNode);

Select 表单选择器

  1. import { Select, Cell, Icon } from 'zarm';
  2. // 级联数据
  3. const CASCADE_DATA = [
  4. {
  5. value: '1',
  6. label: '北京市',
  7. children: [
  8. { value: '11', label: '海淀区' },
  9. { value: '12', label: '西城区' },
  10. ],
  11. },
  12. {
  13. value: '2',
  14. label: '上海市',
  15. children: [
  16. { value: '21', label: '杨浦区' },
  17. { value: '22', label: '静安区' },
  18. ],
  19. },
  20. ];
  21. class Demo extends React.Component {
  22. state = {
  23. value: [],
  24. dataSource: [],
  25. };
  26. componentDidMount() {
  27. // 异步加载数据源测试
  28. setTimeout(() => {
  29. this.setState({
  30. dataSource: CASCADE_DATA,
  31. value: ['1', '12'],
  32. });
  33. }, 0);
  34. }
  35. render() {
  36. const { value, dataSource } = this.state;
  37. return (
  38. <Cell title="城市">
  39. <Select
  40. value={value}
  41. dataSource={dataSource}
  42. onOk={(selected) => {
  43. console.log('Select onOk: ', selected);
  44. this.setState({
  45. value: selected.map(item => item.value),
  46. });
  47. }}
  48. />
  49. </Cell>
  50. )
  51. }
  52. }
  53. ReactDOM.render(<Demo />, mountNode);

PickerView 平铺选择器

  1. import { PickerView } from 'zarm';
  2. // 级联数据
  3. const CASCADE_DATA = [];
  4. class Demo extends React.Component {
  5. state = {
  6. value: [],
  7. dataSource: CASCADE_DATA,
  8. };
  9. componentDidMount() {
  10. setTimeout(() => {
  11. this.setState({
  12. value: ['1', '12'],
  13. dataSource: [
  14. {
  15. code: '1',
  16. label: '北京市',
  17. children: [
  18. { code: '11', label: '海淀区' },
  19. { code: '12', label: '西城区' },
  20. ],
  21. },
  22. {
  23. code: '2',
  24. label: '上海市',
  25. children: [
  26. { code: '21', label: '杨浦区' },
  27. { code: '22', label: '静安区' },
  28. ],
  29. },
  30. ],
  31. valueMember: 'code'
  32. })
  33. }, 0)
  34. }
  35. render() {
  36. return (
  37. <PickerView
  38. value={this.state.value}
  39. valueMember="code"
  40. dataSource={this.state.dataSource}
  41. onChange={selected => {
  42. this.setState({
  43. value: selected.map(item => item.code),
  44. });
  45. console.log('PickerView onChange: ', selected);
  46. }}
  47. />
  48. )
  49. }
  50. }
  51. ReactDOM.render(<Demo />, mountNode);

API

属性类型默认值说明
dataSourceobject[][]数据源
valuestring | string[] | number[]-
defaultValuestring | string[] | number[]-初始值
valueMemberstring'value'值字段对应的key
itemRender(data?: object) => data.label(data?: object) => data.label单个选项的展示
disabledbooleanfalse是否禁用
colsnumberInfinity级联选择器的级数
onChange(selected?: object) => void-值变化时触发的回调函数

仅 Picker & Select 支持的属性

属性类型默认值说明
titlestring'请选择'选择器标题
okTextstring'确定'确定栏文字
cancelTextstring'取消'取消栏文字
maskClosablebooleantrue是否点击遮罩层时关闭,需要和onCancel一起使用
destroybooleanfalse弹层关闭后是否移除节点
onOk(selected?: object) => void-点击确定时触发的回调函数
onCancel() => void-点击取消时触发的回调函数
getContainerHTMLElement | () => HTMLElementdocument.body指定 Picker 挂载的 HTML 节点

仅 Picker 支持的属性

属性类型默认值说明
visiblebooleanfalse是否展示

仅 Select 支持的属性

属性类型默认值说明
placeholderstring'请选择'输入提示信息
hasArrowbooleantrue是否显示箭头
displayRender(selected?: object) => stringselected => selected.map(item => item.label)所选值的展示