1 | import React, { Component } from 'react'
|
2 | import { InputItem } from '@sishuguojixuefu/antd-mobile-rn'
|
3 | import stringWidth from 'string-width'
|
4 | import omit from 'lodash.omit'
|
5 | import { InputPropsType } from '../utils/PropTypes'
|
6 | import getFieldDecorator from '../utils/getFieldDecorator'
|
7 | import Label from './helper/Label'
|
8 | import ErrorTip from './helper/ErrorTip'
|
9 |
|
10 | export default class Input extends Component<InputPropsType, any> {
|
11 | private fieldDecorator: any
|
12 | static defaultProps = {
|
13 | required: false,
|
14 | placeholder: '请输入',
|
15 | textAlign: 'right',
|
16 | maxLength: 0,
|
17 | last: false,
|
18 | multiline: false,
|
19 | }
|
20 |
|
21 | componentWillMount() {
|
22 | const { form, id, initialValue, rules } = this.props
|
23 | const defaultStrValue = initialValue && initialValue.toString()
|
24 | this.fieldDecorator = getFieldDecorator(form, id, defaultStrValue, rules)
|
25 | }
|
26 |
|
27 | private _onChange = (value: string) => {
|
28 | const { onChange } = this.props
|
29 | onChange && onChange(value)
|
30 | }
|
31 |
|
32 | render() {
|
33 | const { icon, placeholder, label, required, form, id, textAlign, maxLength, last, multiline, height } = this.props
|
34 | const omitProps = omit(this.props, ['error', 'labelNumber'])
|
35 | return (
|
36 | <ErrorTip error={form.getFieldError(id)} last={last}>
|
37 | {this.fieldDecorator(
|
38 | <InputItem
|
39 | multiline={multiline}
|
40 | {...omitProps}
|
41 | last
|
42 | clear
|
43 | style={
|
44 | height
|
45 | ? { height, marginLeft: 5, fontSize: 16, color: '#6c6c6c' }
|
46 | : { marginLeft: 5, fontSize: 16, color: '#6c6c6c' }
|
47 | }
|
48 | itemStyle={height ? { height, marginLeft: 0 } : { marginLeft: 0 }}
|
49 | textAlign={textAlign}
|
50 | onChange={this._onChange}
|
51 | labelNumber={label ? stringWidth(label) / 2 + 1 : 0}
|
52 | placeholder={placeholder}
|
53 | maxLength={maxLength && maxLength > 0 ? maxLength : undefined}
|
54 | >
|
55 | <Label label={label} icon={icon} required={required} />
|
56 | </InputItem>
|
57 | )}
|
58 | </ErrorTip>
|
59 | )
|
60 | }
|
61 | }
|