CYInput.js 4.38 KB
import React, {Component} from "react";

import {Image, StyleSheet, TextInput, TouchableOpacity, View} from "react-native";

import {width, zoomH, zoomW} from "../utils/getSize";
import PropTypes from 'prop-types';
export default class CYInput extends Component {
    // 传递参数属性定义
    static PropTypes = {
    onButtonClick: PropTypes.func.isRequired,
    onTextEmpty: PropTypes.func.isRequired
    };

    // 构造
    constructor(props) {
        super(props);
        // 初始状态
        this.state = {
        inputValue: ""
        };
        this.onChange = this._onChange.bind(this);
    }

    _isNull(str) {
        let result = true;
        if (str === "" || str === undefined) {
            result = true;
        }

        if (str.length > 0) {
            result = false;
        }
        return result;
    }

    render() {
        let { inputValue } = this.state;
        return (
                <View style={styles.container}>
                <TextInput
                underlineColorAndroid="transparent"
                numberOfLines={1}
                clearButtonMode={"never"}
                maxLength={50}
                value={inputValue}
                returnKeyType={"search"}
                onChangeText={this.onChange}
                onSubmitEditing={event => {
                const { onSubmitEditing } = this.props;
                onSubmitEditing(event);
                }}
                onFocus={() => {
                const { onFocus } = this.props;
                this.onFocus();
                }}
                {...this.props}
                />
                {this._isNull(inputValue)
                ? this._getRightEmptyView()
                : this._getRightButtonView()}
                <Image
                source={require("../img/searchB.png")}
                style={styles.findImgStyle}
                resizeMode="contain"
                />
                </View>
                );
    }

    _getRightButtonView() {
        //右侧按钮图
        //自定义 按钮图
        let source = this.props.source
        ? this.props.source
        : require("../img/input_clear.png");
        return (
                <TouchableOpacity
                activeOpacity={0.5}
                style={styles.closeOpacityStyle}
                onPress={() => {
                this.props.onButtonClick();
                }}
                >
                <Image style={styles.closeStyle} source={source} resizeMode="contain" />
                </TouchableOpacity>
                );
    }

    _getRightEmptyView() {
        return (
                <TouchableOpacity activeOpacity={0.5} style={styles.closeOpacityStyle} />
                );
    }

    //清除输入款中的值
    clearInputValue() {
        this.setState({ inputValue: "" });
    }

    //获取输入款中的值
    getInputValue() {
        return this.state.inputValue;
    }

    _onChange(changeText) {
        this.setState({
                      inputValue: changeText
                      });
        if (changeText == "") {
            this.props.onTextEmpty();
        }
    }
}

const styles = StyleSheet.create({
                                 container: {
                                 flex: 1,
                                 flexDirection: "row",
                                 alignItems: "center"
                                 },
                                 closeStyle: {
                                 height: 18 / zoomW,
                                 width: 18 / zoomW
                                 },
                                 closeOpacityStyle: {
                                 justifyContent: "center",
                                 backgroundColor: "#00000000",
                                 position:'absolute',
                                 right:0,
                                 height: width * 0.1,
                                 width: 24,
                                 borderTopRightRadius: 8,
                                 borderBottomRightRadius: 8
                                 },
                                 findImgStyle: {
                                 width: 14,
                                 height: 14,
                                 position: "absolute",
                                 // top: 18,
                                 left: 27
                                 }
                                 });