import { mdiEye, mdiEyeOff } from '@mdi/js'; import Icon from '@mdi/react'; import classnames from 'classnames'; import { Component, createRef, InputHTMLAttributes } from 'react'; import injectSheet, { WithStylesProps } from 'react-jss'; import { noop } from 'lodash'; import { IFormField } from '../typings/generic'; import Error from '../error'; import Label from '../label'; import Wrapper from '../wrapper'; import { scorePasswordFunc } from './scorePassword'; import styles from './styles'; interface IData { [index: string]: string; } interface IProps extends InputHTMLAttributes, IFormField, WithStylesProps { focus?: boolean; prefix?: string; suffix?: string; scorePassword?: boolean; showPasswordToggle?: boolean; data?: IData; inputClassName?: string; onEnterKey?: Function; } interface IState { showPassword: boolean; passwordScore: number; } class InputComponent extends Component { private inputRef = createRef(); constructor(props: IProps) { super(props); this.state = { passwordScore: 0, showPassword: false, }; } componentDidMount(): void { const { focus, data = {} } = this.props; if (this.inputRef && this.inputRef.current) { if (focus) { this.inputRef.current.focus(); } for (const key of Object.keys(data)) this.inputRef.current!.dataset[key] = data[key]; } } onChange(e: React.ChangeEvent): void { const { scorePassword, onChange } = this.props; if (onChange) { onChange(e); } if (this.inputRef && this.inputRef.current && scorePassword) { this.setState({ passwordScore: scorePasswordFunc(this.inputRef.current.value), }); } } onInputKeyPress(e: React.KeyboardEvent) { if (e.key === 'Enter') { const { onEnterKey } = this.props; onEnterKey && onEnterKey(); } } render() { const { classes, className, error, id, inputClassName, label, prefix, suffix, value, name, placeholder, spellCheck, min, max, step, required, noMargin, onBlur = noop, onFocus = noop, scorePassword = false, showLabel = true, showPasswordToggle = false, type = 'text', disabled = false, readOnly, } = this.props; const { showPassword, passwordScore } = this.state; const inputType = type === 'password' && showPassword ? 'text' : type; return ( {error && } ); } } export default injectSheet(styles, { injectTheme: true })(InputComponent);