Search code examples
javascriptreactjsecmascript-6babeljsreact-dom

React changing input type via event


I'm trying to extend a React input component, which needs to be type password and after certain event on the element or element next to it - it needs to toggle the type of the input (type="text/password").

How this can be handled by React?

I have this as class for my component:

import { React, Component } from 'lib' 

export class PasswordInput extends Component {
constructor(props, context){
    super(props, context)
    const { type, validate, password } = this.props

    if(context.onValidate && password) {
        context.onValidate(password, validate)
    }

    if(context.showHide && password) {
        context.onValidate(password, validate)
    }
}

render() {
    let inputType = 'password'

    return (
        <div className="form">
            <label htmlFor="password">{ this.props.label }</label>
            <input {...this.constructor.filterProps(this.props)} type={ inputType } />
            { this.props.touched && this.props.error && <div className="error">{ this.props.error }</div> }
            <button onClick={ showHide() }>{ this.props.btnLabel }</button>
        </div>
    )
}

showHide(field) {
    return function(event){
        console.log(`state before is ${this.state}`)
    }
}

// the meld is
// export function meld(...objects) {
//     return Object.assign({}, ...objects)
// }

  static filterProps(props) {
      const result = meld(props)
      delete(result.validate)
      return result
  }
}

PasswordInput.contextTypes = {
    onValidate: React.PropTypes.func
}

EDIT I've edited the render method and added a function that handles the event, but I'm now getting:

Warning: setState(...): Cannot update during an existing state transition (such as within render). Render methods should be a pure function of props and state.

And my browser crashes.

.....

    render() {
    return (
        <div className="form__group">
            <label htmlFor="password">{ this.props.label }</label>
            <input {...this.constructor.filterProps(this.props)} type={ this.state.inputType } />
            { this.props.touched && this.props.error && <div     className="form__message form__message_error">{ this.props.error }</div> }
            <button onClick={ this.handleClick() }>{ this.props.btnLabel }</button>
        </div>
    )
}

handleClick(){
    this.setState({ inputType: this.state.inputType === 'password' ? 'text' : 'password' })
}

.....


Solution

  • You can configure the type of the input according to component's state, and set it on some event, for example:

    <input {...this.constructor.filterProps(this.props)} type={ this.state.inputType } onChange={ event => this.onChange(event) } />

    And implement the onChange method:

    onChange(event) {
        var length = event.currentTarget.value.length;
        this.setState({ inputType: length < 5 ? 'text' : 'password' });
    }
    

    You might have to bind the onChange function.