【问题标题】:In Reactjs : How can I change bottomBorderColor of Parent Div?在 Reactjs 中:如何更改父 Div 的 bottomBorderColor?
【发布时间】:2017-07-16 16:17:57
【问题描述】:

代码:

class RegistrationPage extends Component {
        constructor() {
        super();

    this.state = {
      style : {
        borderBottomColor : ""
      }
    }

    this.handleOnClick = this.handleOnClick.bind(this);
  }

  handleOnFocus() {
    this.setState({
      style : {
        borderBottomColor : "orange"
      }
    })
  }

  render() {
    return (
      <div id="reg_div">

          <h3 id="heading">Sign Up</h3>

          <form>

          <label htmlFor="userName">ENTER USERNAME</label>
          <br />
          <div className="userInputBox" style={this.state.style}>
            <input type="text" name="userName" className="inputStyle" placeholder="Enter Here" onFocus={this.handleOnFocus} />
          </div>
          <br />
          <label htmlFor="key">ENTER KEY</label>
          <br />
          <div className="userInputBox" style={this.state.style}>
            <input type="password" name="key" className="inputStyle" placeholder="Enter Here" onFocus={this.handleOnFocus} />
          </div>

          <button id="sign_up">REGISTER</button>

          </form>

          <div id="btn">
              <button id="sign_in_page">I already have an account</button>
          </div>

      </div>
    )
  }
}

问题: 我有两个输入字段,一个用于用户名,另一个用于键。每个都有其特定的父 div。当我关注任何输入字段时,它会更改两个 div 的状态,而我想更改输入字段所关注的特定 div 的状态。

【问题讨论】:

  • 每个输入都有状态,passwordInputStyle 和 usernameInputStyle

标签: reactjs ecmascript-6


【解决方案1】:

好的,我已经编辑了您的代码。请尝试以下代码:

    import React, { Component } from 'react';
    import { render } from 'react-dom';

    class RegistrationPage extends Component {
    constructor() {
      super();
      this.state = {
        userNameStyle: '',
        keyStyle: '',
      };
    }

    handleOnFocus(inputFocussed) {
      if (inputFocussed == 'username') {
        this.setState({
          userNameStyle: 'orange',
          keyStyle: ''
      });
    } else if (inputFocussed == 'key') {
      this.setState({
          userNameStyle: '',
          keyStyle: 'orange',
      });
     }
    }
    render() {
     return (
      <div id="reg_div">
        <h3 id="heading">Sign Up</h3>

        <form>
          <label htmlFor="userName">ENTER USERNAME</label>
          <br />
          <div
            className="userNameInputBox"
            style={{ borderBottomWidth: 3,borderBottomStyle:"solid", borderBottomColor: this.state.userNameStyle }}
          >
            <input
              type="text"
              name="userName"
              className="inputStyle"
              placeholder="Enter Here"
              onFocus={() => this.handleOnFocus('username')}
            />
          </div>
          <br />
          <label htmlFor="key">ENTER KEY</label>
          <br />
          <div
            className="keyInputBox"
            style={{ borderBottomWidth: 3,borderBottomStyle:"solid",borderBottomColor:this.state.keyStyle }}
          >
            <input
              type="password"
              name="key"
              className="inputStyle"
              placeholder="Enter Here"
              onFocus={() => this.handleOnFocus('key')}
            />
          </div>

          <button id="sign_up">REGISTER</button>
        </form>

        <div id="btn">
          <button id="sign_in_page">I already have an account</button>
        </div>
      </div>
      );
     }
    }

 render(<RegistrationPage />, document.getElementById('root'));

【讨论】:

  • 这段代码有效。它还帮助我更好地理解状态的使用。所以,非常感谢您的帮助。
【解决方案2】:

我相信你在使用 react 时应该有一点不同的想法,因为 react 反应的工作方式不同。

你最好有一个单独的输入组件,这样它的状态就可以自己处理了。

我已经稍微修改了你的代码来完成你想要的。

class RegistrationPage extends Component {
     constructor() {
     super();
}

render() {
    return (
       <div id="reg_div">
         <h3 id="heading">Sign Up</h3>

         <form>
            <label htmlFor="userName">ENTER USERNAME</label>
            <br />
            <InputComponent divClassName="userInputBox" type="text" name="userName" id="userName" initialBorderColor="white" focusBorderColor="orange" />
            <br />
            <label htmlFor="key">ENTER KEY</label>
            <br />
            <InputComponent divClassName="userInputBox" type="password" name="key" id="key" initialBorderColor="white" focusBorderColor="orange"/>
            <br/>
            <button id="sign_up">REGISTER</button>
         </form>
         <div id="btn">
           <button id="sign_in_page">I already have an account</button>
         </div>  
      </div>
    )
  }

}

class InputComponent extends React.Component{
    constructor(){
        super();
        this.state = {
            borderColor : "",
        }
        this.handleOnFocus = this.handleOnFocus.bind(this);
        this.handleOnBlur = this.handleOnBlur.bind(this);

}

componentDidMount(){
    this.initialBorderColor = this.props.initialBorderColor;
    this.focusBorderColor = this.props.focusBorderColor;
    this.setState({borderColor: this.props.initialBorderColor})
}

handleOnFocus() {
    this.setState({borderColor : this.focusBorderColor});
}

handleOnBlur() {
    this.setState({borderColor : this.initialBorderColor});
}

render(){

    let borderStyle = {display:'inline-block', borderBottomWidth: "3px", borderBottomStyle:"solid", borderBottomColor: this.state.borderColor}
    return(
        <div className={this.props.divClassName} style={borderStyle}>
            <input type={this.props.type} name={this.props.name} id={this.props.id} onFocus={this.handleOnFocus} onBlur={this.handleOnBlur}/>
         </div>
    )
}
}

【讨论】:

  • 它对我有用。它还清除了我遇到麻烦的一些概念。所以,非常感谢您的帮助。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2010-10-11
  • 1970-01-01
  • 2014-11-29
  • 2021-11-25
  • 2017-12-03
  • 1970-01-01
  • 2013-07-28
相关资源
最近更新 更多