【问题标题】:Building Custom Input Component with states使用状态构建自定义输入组件
【发布时间】:2020-07-12 15:52:58
【问题描述】:

我正在尝试构建一个自定义输入组件,它执行以下操作:

  • 接受道具 - 完成
  • 如果 onFocus 为真,则渲染一个“V”图标 - 完成
  • 验证输入:如果文本输入是“填充文本”,则检查验证是否为真,如果为真:将“V”图标颜色和按钮边框颜色更改为绿色,如果为假:将“V”图标颜色和按钮边框颜色更改为红色,保持这种样式,直到 inputField 再次为空
import React from "react";
import { View, TextInput, StyleSheet, Text } from "react-native";
import {
  widthPercentageToDP as wp,
  heightPercentageToDP as hp,
} from "react-native-responsive-screen";
import { MaterialCommunityIcons, AntDesign } from "@expo/vector-icons";

class RegisterTextBox extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      borderColor: "",
      isFocused: false,
    };
  }
  onBlur() {
    this.setState({ isFocused: false });
  }
  onFocus() {
    this.setState({ isFocused: true });
  }

  render() {
    const { isFocused } = this.state;
    const {
      value,
      placeholder,
      onChangeText,
      secureTextEntry,
      inputStyle,
      viewStyle,
      showIcon = this.state.showIcon,
      eyeIcon = false,
    } = this.props;
    return (
      <View style={[styles.container, viewStyle]}>
        <TextInput
          style={[styles.main, { borderBottomColor: this.state.borderColor }]}
          value={value}
          onChangeText={onChangeText}
          onBlur={() => this.onBlur()}
          onFocus={() => this.onFocus()}
          placeholder={placeholder}
          secureTextEntry={secureTextEntry}
          onChangeText={(val) => this.updateInputVal(val, "confirmPassword")}
        />
        {isFocused ? (
          <AntDesign
            name="checkcircle"
            size={18}
            color="black"
            style={{ paddingTop: 8 }}
          />
        ) : (
          <View />
        )}
        {eyeIcon ? (
          <MaterialCommunityIcons
            name="eye-off"
            size={24}
            color="black"
            style={{ paddingTop: 5, paddingLeft: 5 }}
          />
        ) : (
          <View />
        )}
      </View>
    );
  }
}

const styles = StyleSheet.create({
  container: {
    height: hp(5.4),
    width: wp(65.2),
    borderBottomWidth: 1,
    flexDirection: "row",
    justifyContent: "space-between",
  },
  main: {
    flex: 1,
  },
});

export default RegisterTextBox;

【问题讨论】:

    标签: react-native


    【解决方案1】:

    我建议你保留一个状态值,以便知道TextInput 是否填充了文本。

    constructor(props) {
        super(props);
        this.state = {
          isTextFill: false,
          isFocused: false,
        };
    }
    

    然后在onChangeText 触发时检查输入字段是否已填充文本。我创建了一个函数来保持您的代码的条件和其余部分,这些代码已在 TextInput 中找到 onChangeText

    onChangeTextEvent(text){
       if(text.length > 0){
          this.setState({
             isTextFill : true
          })
       } else {
          this.setState({
             isTextFill : false
          })
       }
       this.updateInputVal(text, "confirmPassword"); //the function that you had called. I don't know why and where that is. 
    }
    

    然后你可以使用条件运算符来管理你的代码。

    return (
          <View style={[styles.container, viewStyle]}>
            <TextInput
              style={[styles.main, { borderBottomColor: this.state.isTextFill ? "green" : "red" }]}
              value={value}
              onBlur={() => this.onBlur()}
              onFocus={() => this.onFocus()}
              placeholder={placeholder}
              secureTextEntry={secureTextEntry}
              onChangeText={this.onChangeTextEvent.bind(this)}
            />
            {isFocused ? (
              <AntDesign
                name="checkcircle"
                size={18}
                color={this.state.isTextFill ? "green" : "red"}
                style={{ paddingTop: 8 }}
              />
            ) : (
              <View />
            )}
            {eyeIcon ? (
              <MaterialCommunityIcons
                name="eye-off"
                size={24}
                color={this.state.isTextFill ? "green" : "red"}
                style={{ paddingTop: 5, paddingLeft: 5 }}
              />
            ) : (
              <View />
            )}
          </View>
        );
    

    【讨论】:

    • 试过了,我得到这个setState不是函数的错误
    • 是的,因为 onChageTextEvent 函数还没有与“this”对象绑定。这应该被解决为 this.OnChangeTextEvent.bind(this)。我也更改了代码。立即尝试。
    • 能否解释一下绑定解决方案?
    • stackoverflow.com/questions/50726014/… 这里有一个更好的解释。通过这个。
    猜你喜欢
    • 1970-01-01
    • 2017-03-24
    • 1970-01-01
    • 1970-01-01
    • 2022-10-08
    • 1970-01-01
    • 1970-01-01
    • 2021-07-16
    • 2021-03-14
    相关资源
    最近更新 更多