import React, { Component } from 'react'; import PropTypes from 'prop-types'; import { observer } from 'mobx-react'; import { Field } from 'mobx-react-form'; import classnames from 'classnames'; import { scorePassword as scorePasswordFunc } from '../../helpers/password-helpers'; export default @observer class Input extends Component { static propTypes = { field: PropTypes.instanceOf(Field).isRequired, className: PropTypes.string, focus: PropTypes.bool, showPasswordToggle: PropTypes.bool, showLabel: PropTypes.bool, scorePassword: PropTypes.bool, prefix: PropTypes.string, suffix: PropTypes.string, }; static defaultProps = { className: null, focus: false, showPasswordToggle: false, showLabel: true, scorePassword: false, prefix: '', suffix: '', }; state = { showPassword: false, passwordScore: 0, } inputElement = null; componentDidMount() { if (this.props.focus) { this.focus(); } } onChange(e) { const { field, scorePassword } = this.props; field.onChange(e); if (scorePassword) { this.setState({ passwordScore: scorePasswordFunc(field.value) }); } } focus() { this.inputElement.focus(); } render() { const { field, className, showPasswordToggle, showLabel, scorePassword, prefix, suffix, } = this.props; const { passwordScore } = this.state; let type = field.type; if (type === 'password' && this.state.showPassword) { type = 'text'; } return (
{prefix && ( {prefix} )} this.onChange(e)} onBlur={field.onBlur} onFocus={field.onFocus} ref={(element) => { this.inputElement = element; }} disabled={field.disabled} /> {suffix && ( {suffix} )} {showPasswordToggle && (
{field.label && showLabel && ( )} {field.error && (
{field.error}
)}
); } }