LocationTextView.js 2.46 KB
import React, {Component} from 'react';
import {
    View,
    Text,
    TouchableOpacity,
    NativeModules
} from 'react-native';
import AutoExpandingInput from "./AutoExpandingInput";

export default class LocationTextView extends Component {

    constructor(props) {
        super(props);
        this.state = {
            address: ''
        }
        this.attrData = this.props.attrData;
    }

    getAddress() {
        let _this = this;
        NativeModules.system.getLocation().then(data => {
            console.log('address', data);
            if (!!JSON.parse(data).cityCode) {
                let addressData = JSON.parse(data);
                _this.setState({
                    address: addressData.formattedAddress
                });
                let result = {latitude: addressData.latitude, longitude: addressData.longitude, ip: '', name: addressData.formattedAddress}
                this.attrData.value = JSON.stringify(result);
            } else {
                return;
            }
        });
    }

    render() {
        let {name, code, type, description, isRequired, isPreview} = this.attrData;
        let {placeholder, buttonText} = JSON.parse(this.attrData.data);

        return (
            <View style={{width: '100%', backgroundColor: 'white', paddingHorizontal: 5, paddingVertical: 10, flexDirection: 'row'}}>
                <View style={{flexDirection: 'row'}}>
                    {isRequired&&<Text style={{ color: "#FF3030" }}>* {" "}</Text>}
                    {!isRequired&&<Text style={{ color: "#fff" }}>* {" "}</Text>}
                    <Text style={{fontSize: 16, color: 'rgba(0, 0, 0, 1)', marginBottom: 5}}>{name||""}</Text>
                </View>
                <AutoExpandingInput
                    style={{fontSize: 16, color: 'black', flex: 1, backgroundColor: 'white', textAlign: 'left', paddingVertical: 5}}
                    editable={false}
                    onChange={text => {
                        if (this.onChangeText) {
                            this.onChangeText(text)
                        }
                    }}
                    value={this.state.address}
                    placeholderTextColor={'#999'}
                    placeholder={placeholder}
                />
                <Text style={{fontSize: 16, color: global.homeColor}} onPress={()=>this.getAddress()}>
                    {(this.state.address && this.state.address.length > 0) ? '刷新':'获取'}</Text>
            </View>
        );
    }
}