1 | import React, { Component } from 'react'
|
2 | import { InputItem } from '@sishuguojixuefu/antd-mobile-rn'
|
3 | import { View, Text, StyleSheet } from 'react-native'
|
4 | import stringWidth from 'string-width'
|
5 | import nzh from 'nzh'
|
6 | import omit from 'lodash.omit'
|
7 | import { SsAmountProps } from '../utils/PropTypes'
|
8 | import getFieldDecorator from '../utils/getFieldDecorator'
|
9 | import Label from './helper/Label'
|
10 | import ErrorTip from './helper/ErrorTip'
|
11 |
|
12 | const nzhCn = nzh.cn
|
13 |
|
14 | export default class SsAmount extends Component<SsAmountProps, {}> {
|
15 | private fieldDecorator: any
|
16 | private chineseAmount = ''
|
17 | static defaultProps = {
|
18 | required: false,
|
19 | placeholder: '请输入',
|
20 | textAlign: 'right',
|
21 | upper: true,
|
22 | }
|
23 |
|
24 | componentWillMount() {
|
25 | const { form, id, initialValue, rules } = this.props
|
26 | const defaultStrValue = initialValue && initialValue.toString()
|
27 | this._getChineseAmount(defaultStrValue)
|
28 | this.fieldDecorator = getFieldDecorator(form, id, defaultStrValue, rules ? [...rules, 'money'] : ['money'])
|
29 | }
|
30 |
|
31 | private _onChange = (value: string) => {
|
32 | const { onChange, upper } = this.props
|
33 | if (upper) {
|
34 | this._getChineseAmount(value)
|
35 | }
|
36 | onChange && onChange(value)
|
37 | }
|
38 |
|
39 | private _getChineseAmount = (value: string = '') => {
|
40 | if (isNaN(Number(value))) {
|
41 | this.chineseAmount = ''
|
42 | } else if (value.length > 16) {
|
43 | this.chineseAmount = '大写转换最多支持16位数的金额!'
|
44 | } else {
|
45 | this.chineseAmount = nzhCn.toMoney(value, { outSymbol: false })
|
46 | }
|
47 | }
|
48 |
|
49 | render() {
|
50 | const { placeholder, label, required, form, id, textAlign, upper } = this.props
|
51 | const omitProps = omit(this.props, ['error', 'labelNumber'])
|
52 | return (
|
53 | <ErrorTip error={form.getFieldError(id)}>
|
54 | {this.fieldDecorator(
|
55 | <InputItem
|
56 | {...omitProps}
|
57 | last
|
58 | clear
|
59 | extra="¥"
|
60 | type="number"
|
61 | itemStyle={{ marginLeft: 0 }}
|
62 | textAlign={textAlign}
|
63 | onChange={this._onChange}
|
64 | labelNumber={label ? stringWidth(label) / 2 + 1 : 0}
|
65 | placeholder={placeholder}
|
66 | >
|
67 | <Label required={required} label={label} />
|
68 | </InputItem>
|
69 | )}
|
70 | {upper && (
|
71 | <View style={styles.amountText}>
|
72 | <Text style={styles.amountLabel}>大写</Text>
|
73 | <Text style={[styles.amountValue]} numberOfLines={1}>
|
74 | {this.chineseAmount}
|
75 | </Text>
|
76 | </View>
|
77 | )}
|
78 | </ErrorTip>
|
79 | )
|
80 | }
|
81 | }
|
82 |
|
83 | const styles = StyleSheet.create({
|
84 | amountLabel: {
|
85 | flex: 1,
|
86 | textAlign: 'left',
|
87 | },
|
88 | amountText: {
|
89 | color: '#888888',
|
90 | flexDirection: 'row',
|
91 | fontSize: 14,
|
92 | paddingBottom: 5,
|
93 | paddingHorizontal: 15,
|
94 | },
|
95 | amountValue: {
|
96 | flex: 4,
|
97 | textAlign: 'right',
|
98 | },
|
99 | })
|