Advertisement
Guest User

Untitled

a guest
May 22nd, 2018
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import React from 'react';
  2. import ErrorField from './ErrorField';
  3.  
  4. const inputIcon = {
  5.     text: 'hide',
  6.     password: 'show'
  7. };
  8.  
  9. class InputField extends React.Component {
  10.     constructor(props) {
  11.         super(props);
  12.         this.state = {
  13.             isPassword: props.type === 'password',
  14.             type: props.type || 'text'
  15.         };
  16.     }
  17.  
  18.     getInputIcon = () => {
  19.         const { isPassword, type } = this.state;
  20.         if (!isPassword) return null;
  21.         const className = `input-field-icon input-field-icon-${inputIcon[type]}`;
  22.         return <i className={className} onClick={this.toggleInputType} />;
  23.     };
  24.  
  25.     toggleInputType = ev => {
  26.         ev.preventDefault();
  27.         this.setState({ type: this.state.type === 'password' ? 'text' : 'password' });
  28.     };
  29.  
  30.     render() {
  31.         const { label, icon, input, meta, ...restProps } = this.props;
  32.         return (
  33.             <div className={`input-element ${meta && meta.touched && !meta.valid && 'input-element__error'}`}>
  34.                 <div className="input-label">{label}</div>
  35.                 {icon && <i className={`input-icon ${icon}`} />}
  36.                 <div className="input-field-wrap">
  37.                     <input className="input-field" {...restProps} {...input} type={this.state.type} />
  38.                     {this.getInputIcon()}
  39.                 </div>
  40.                 {meta && <ErrorField touched={meta.touched} error={meta.error} />}
  41.             </div>
  42.         );
  43.     }
  44. }
  45.  
  46. export default InputField;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement