PhoneEditView.js 5.52 KB
import React, {Component} from 'react';
import {Text, TextInput, View,} from 'react-native';
// 默认高度
const defaultHeight = (70);
// 标题默认高度
const titleViewHeight = (24);
// 默认垂直内边距
const VPading = (6);

/*
电话输入框组件(type:'PHONE')
 */
export default class PhoneEditView extends Component {
    // 构造方法
    constructor(props) {
        super(props);
        this.state = {
            inputValue: ''//输入内容
        };
        // 传入参数
        this.attrData = this.props.attrData;
        // json解析后的传入参数
        this.extendData = (this.attrData.data && this.attrData.data.length > 0) ? JSON.parse(this.attrData.data) : {};
        // 用于带出输入内容的callback回调
        this.onChangeText = this.props.onChangeText;
        // 键盘格式
        this.inputType = this.props.inputType || 'numeric';
    }

    // 生命周期-组件将要展示
    componentWillMount() {
        // 设置默认值
        if (this.attrData.value && this.attrData.value.length > 0) {
            this.setState({
                inputValue: this.attrData.value
            })
        }
    }

    // 绘制UI
    render() {
        /*
        name:标题
        code:代码编号
        type:控件类型(PHONE)
        description:描述
        isRequired:是否必须
        isPreview:是否预览
        */
        let {name, code, type, description, isRequired, isPreview} = this.attrData;
        // 输入提示文字
        let {placeholder} = this.extendData;
        // 校验类型 取值固定:ALL=手机和固话|MOBILE=手机|PHONE=固话
        let tabType = this.extendData.type;

        return (
            // 最外层样式区域
            <View style={{
                // 宽度
                width: '100%',
                backgroundColor: 'white',
                height: defaultHeight,
                paddingVertical: VPading,
                paddingLeft: 10,
                paddingRight: 15,
            }}>
                {/*标题显示区域*/}
                <View style={{
                    height: titleViewHeight,
                    flexDirection: 'row',
                    justifyContent: 'flex-start',
                    alignItems: 'center',
                    backgroundColor: '#fff'
                }}>
                    {/*是否必须UI设定*/}
                    {isRequired && <Text style={{color: "#FF3030"}}>* </Text>}
                    {!isRequired && <Text style={{color: "#fff"}}>* </Text>}
                    {/*标题设定*/}
                    <Text style={{fontSize: 16, color: 'rgba(0, 0, 0, 1)'}}>{name || ""}</Text>
                </View>
                {/*输入框*/}
                <View style={{flexDirection: 'row', width: '100%', justifyContent: 'center', marginLeft: 10}}>
                    {/*是否同时支持手机+固话两种输入*/}
                    {tabType !== 'ALL' &&
                    <View style={{flexDirection: 'row'}}>
                        {/*手机前+86,固话前显示'固话'字样*/}
                        <Text style={{
                            fontSize: 16,
                            color: 'rgba(0, 0, 0, 1)'
                        }}>{tabType === 'MOBILE' ? '+86  >  ' : '固话'}</Text>
                        <View style={{width: 1, height: 24, backgroundColor: '#f3f4f5', marginHorizontal: 5}}/>
                    </View>
                    }

                    {/*输入框*/}
                    <TextInput
                        style={{
                            // 字号
                            fontSize: 14,
                            // 字体颜色
                            color: 'black',
                            // 宽度
                            width: '100%',
                            // 背景色
                            backgroundColor: '#fff',
                            // 字体对齐样式
                            textAlign: 'left',
                            // 高度
                            height: 30,
                            // 上外边距
                            marginTop: 4,
                            // 下内边距
                            marginBottom: 4,
                            //
                            padding: 0
                        }}
                        {/*最大输入字数*/}
                        maxLength={11}
                        {/*键盘样式*/}
                        keyboardType={this.inputType}
                        {/*显示内容*/}
                        value={this.state.inputValue}
                        {/*输入框内容发生变化的触发回调*/}
                        onChangeText={text => {
                            // 输入内容正则处理,去除非数字
                            const newText = text.replace(/[^\d]+/, '');
                            this.setState({inputValue: newText})
                            if (this.onChangeText) {
                                this.onChangeText(newText)
                            }
                            this.attrData.value = newText;
                        }}
                        {/*提示文字颜色*/}
                        placeholderTextColor={'#999'}
                        {/*提示文字*/}
                        placeholder={placeholder || '请输入'}
                        {/*去除android输入框下面的横线,和ios保持一致*/}
                        underlineColorAndroid="transparent"
                    />
                </View>
            </View>
        );
    }
}