NumberEditView.js 5.03 KB
import React, {Component} from 'react';
import {
    View,
    Text,
    TextInput,
    Image,
} from 'react-native';
import {convertCurrency, validateResult} from "../utils/utils";
const defaultHeight = (70);
const titleViewHeight = (24);
const VPading = (6);
const DescHeight = (30);

export default class NumberEditView extends Component {

    constructor(props) {
        super(props);
        this.state = {
            value: '',
        };
        this.attrData = this.props.attrData;
        this.type = this.props.type;// 金额或数字
        this.onChangeText = this.props.onChangeText;
        this.inputType = this.props.inputType || 'numeric';// 输入类型
        this.extendData = (this.attrData.data && this.attrData.data.length > 0)?JSON.parse(this.attrData.data):{};
    }

    componentWillMount() {
        if (this.attrData.value && this.attrData.value.length > 0) {
            this.setState({
                value: this.attrData.value
            })
        }
    }

    render() {
        let {name, code, type, uom, description, isRequired, isPreview} = this.attrData;
        // precision: 精度
        let {placeholder, showChinese, precision} = this.extendData;
        let displayUom = ''
        if(!!uom){
            displayUom = '(' + uom + ')';
        }
        let calcHeight = defaultHeight;
        if(showChinese){
            calcHeight += DescHeight
        }
        return(
            <View style={{width: '100%', backgroundColor: 'white', paddingTop: VPading,paddingLeft: 10, paddingRight: 15,height:calcHeight}}>
                <View style={{flexDirection: 'row',height:titleViewHeight,justifyContent:'flex-start',alignItems:'center'}}>
                    {isRequired&&<Text style={{ color: "#FF3030" }}>* </Text>}
                    {!isRequired&&<Text style={{ color: "#fff" }}>* </Text>}
                    <Text style={{fontSize: 16, color: 'rgba(0, 0, 0, 1)'}}>{name||""}{displayUom}</Text>
                </View>
                <View style={{flexDirection: 'row', alignItems: 'center'}}>
                    <TextInput
                        style={{
                            fontSize: 14,
                            color: 'black',
                            height: 30,
                            width: '100%',
                            marginLeft: 10,
                            backgroundColor: '#fff',
                            textAlign: 'left',
                            marginTop:4,
                            marginBottom:4,
                            padding:0
                        }}
                        // 身份证输入18位限制
                        maxLength={type === 'IDCARD'?18:(precision||16)}
                        multiline={false}
                        keyboardType={this.inputType}
                        onChangeText={text => {
                            if (type === 'IDCARD') {
                                // 身份证直接赋值
                                this.attrData.value = text;
                                this.setState({
                                    value:text,
                                })
                                if (this.onChangeText) {
                                    this.onChangeText(text, type)
                                }
                            } else {
                                // 计算精度
                                this.calculatePrecision(text, precision, type);
                            }
                        }}
                        value = {this.state.inputValue}
                        defaultValue={this.state.value}
                        placeholderTextColor={'#999'}
                        placeholder={placeholder||'请输入'}
                        underlineColorAndroid="transparent"
                    />
                </View>
                {showChinese &&
                    <View style={{height:DescHeight, paddingBottom:VPading, flexDirection: 'row', alignItems:'center', backgroundColor:'#fff',
                        marginLeft:10, marginRight:15
                    }}>
                        <Text style={{fontSize: 14, color: 'rgba(0, 0, 0, 1)'}}>大写</Text>
                        <Text style={{fontSize: 16, color: 'rgba(0, 0, 0, 1)',marginLeft:6,marginRight:4}}>
                            {!!this.state.inputValue?convertCurrency(this.state.inputValue):''}</Text>
                    </View>
                }
            </View>
        )
    }

    calculatePrecision(text, precision, type) {
        let newText = '';
        if (precision === undefined || precision === null || parseInt(precision) === 0) {
            // 取整数
            newText = text.replace(/[^\d]+/, '');
            this.setState({inputValue: newText})
        } else {
            // 取小数点
            this.setState({inputValue: text})
            newText = parseFloat(text).toFixed(parseInt(precision))
        }
        console.log("number====", newText);
        if (this.onChangeText) {
            this.onChangeText(newText, type)
        }
        this.attrData.value = newText;
    }
}