UNPKG

1.73 kBTypeScriptView Raw
1import React, { Component } from 'react'
2import { List, Picker } from '@sishuguojixuefu/antd-mobile-rn'
3// @ts-ignore
4// import { Picker } from 'antd-mobile-rn'
5import { SsSelectPropsType } from '../utils/PropTypes'
6import ErrorTip from './helper/ErrorTip'
7import Label from './helper/Label'
8import getFieldDecorator from '../utils/getFieldDecorator'
9
10const { Item } = List
11
12export default class SsSelect extends Component<SsSelectPropsType, {}> {
13 private fieldDecorator: any
14 static defaultProps = {
15 required: false,
16 placeholder: '请输入',
17 cols: 1,
18 }
19
20 componentWillMount() {
21 const { form, id, initialValue, rules } = this.props
22 this.fieldDecorator = getFieldDecorator(form, id, initialValue && [initialValue], rules)
23 }
24
25 private _onChange = (value?: React.ReactText[]): void => {
26 const { onChange } = this.props
27 onChange && onChange(value)
28 }
29
30 private _getData = () => {
31 const { options, cols } = this.props
32 if (cols === 1) {
33 return options.map((item: any) => {
34 return {
35 label: item.label || item,
36 value: item.value || item,
37 }
38 })
39 }
40 return options
41 }
42
43 render() {
44 const { icon, placeholder, label, required, form, id, cols, last } = this.props
45 return (
46 <ErrorTip error={form.getFieldError(id)} last={last}>
47 {this.fieldDecorator(
48 // @ts-ignore
49 <Picker {...this.props} cols={cols} onChange={this._onChange} extra={placeholder} data={this._getData()}>
50 <Item arrow="horizontal" style={{ paddingLeft: 0 }} last wrap>
51 <Label required={required} label={label} icon={icon} />
52 </Item>
53 </Picker>
54 )}
55 </ErrorTip>
56 )
57 }
58}