UNPKG

1.85 kBTypeScriptView Raw
1import React, { Component } from 'react'
2import { TextareaItem } from '@sishuguojixuefu/antd-mobile-rn'
3import omit from 'lodash.omit'
4import { TextareaPropsType } from '../utils/PropTypes'
5import getFieldDecorator from '../utils/getFieldDecorator'
6import TextareaWrap from './helper/TextareaWrap'
7
8export default class Textarea extends Component<TextareaPropsType, {}> {
9 private fieldDecorator: any
10 private inputed = 0
11 static defaultProps = {
12 required: false,
13 placeholder: '请输入',
14 count: 0,
15 last: false,
16 }
17
18 componentWillMount() {
19 const { form, id, initialValue, rules } = this.props
20 this.inputed = initialValue ? initialValue.length : 0
21 this.fieldDecorator = getFieldDecorator(form, id, initialValue, rules)
22 }
23
24 private _onChange = (value?: string) => {
25 const { onChange } = this.props
26 this.inputed = value ? value.length : 0
27 onChange && onChange(value)
28 }
29
30 render() {
31 const { placeholder, label, required, form, id, count, last } = this.props
32 const omitProps = omit(this.props, ['error', 'labelNumber'])
33 return (
34 <TextareaWrap
35 error={form.getFieldError(id)}
36 label={label}
37 required={required}
38 count={count} // 计数功能,兼具最大长度,默认为0,代表不开启计数功能
39 inputed={this.inputed || (form.getFieldValue(id) && form.getFieldValue(id).length) || 0}
40 last={last}
41 >
42 {this.fieldDecorator(
43 <TextareaItem
44 {...omitProps}
45 last
46 clear // 注意:antd没有实现label、没有实现安卓的clear
47 autoHeight
48 style={{ marginHorizontal: 15, paddingHorizontal: 0 }}
49 count={count}
50 placeholder={placeholder}
51 onChange={this._onChange}
52 />
53 )}
54 </TextareaWrap>
55 )
56 }
57}