import { mdiEye, mdiEyeOff } from '@mdi/js'; import Icon from '@mdi/react'; import classnames from 'classnames'; import React, { Component, createRef } from 'react'; import injectSheet from 'react-jss'; import { IFormField, IWithStyle } 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 React.InputHTMLAttributes, IFormField, IWithStyle { 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 { static defaultProps = { focus: false, onChange: () => {}, onBlur: () => {}, onFocus: () => {}, scorePassword: false, showLabel: true, showPasswordToggle: false, type: 'text', disabled: false, }; state = { passwordScore: 0, showPassword: false, }; private inputRef = createRef(); componentDidMount() { const { focus, data } = this.props; if (this.inputRef && this.inputRef.current) { if (focus) { this.inputRef.current.focus(); } if (data) { Object.keys(data).map(key => this.inputRef.current!.dataset[key] = data[key]); } } } onChange(e: React.ChangeEvent) { 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, disabled, error, id, inputClassName, label, prefix, scorePassword, suffix, showLabel, showPasswordToggle, type, value, name, placeholder, spellCheck, onBlur, onFocus, min, max, step, } = this.props; const { showPassword, passwordScore, } = this.state; const inputType = type === 'password' && showPassword ? 'text' : type; return ( {error && ( )} ); } } export const Input = injectSheet(styles)(InputComponent);