import { ChangeEvent, ChangeEventHandler, Component, createRef, ReactElement, RefObject, } from 'react'; import { observer } from 'mobx-react'; import classnames from 'classnames'; import { defineMessages, injectIntl, WrappedComponentProps } from 'react-intl'; import { mdiEye, mdiEyeOff } from '@mdi/js'; import { noop } from 'lodash'; import { scorePassword as scorePasswordFunc } from '../../helpers/password-helpers'; import Icon from './icon'; import { Field } from '../../@types/mobx-form.types'; const messages = defineMessages({ passwordToggle: { id: 'settings.app.form.passwordToggle', defaultMessage: 'Password toggle', }, }); interface IProps extends WrappedComponentProps { field: Field; className?: string; focus?: boolean; showPasswordToggle?: boolean; showLabel?: boolean; scorePassword?: boolean; prefix?: string; suffix?: string; placeholder?: string; onChange?: ChangeEventHandler; } interface IState { showPassword: boolean; passwordScore: number; } // Can this file be merged into the './input/index.tsx' file? @observer class Input extends Component { private inputElement: RefObject = createRef(); constructor(props: IProps) { super(props); this.state = { showPassword: false, passwordScore: 0, }; } componentDidMount(): void { const { focus = false } = this.props; if (focus) { this.focus(); } } onChange(e: ChangeEvent): void { const { field, scorePassword, onChange = noop } = this.props; if (field.onChange) { onChange(e); field.onChange(e); } if (scorePassword) { this.setState({ passwordScore: scorePasswordFunc(field.value as string), }); } } focus() { if (this.inputElement && this.inputElement.current) { this.inputElement.current!.focus(); } } render(): ReactElement { const { field, className = null, showPasswordToggle = false, showLabel = true, scorePassword = false, prefix = '', suffix = '', intl, } = this.props; const { passwordScore } = this.state; let { type } = field; if (type === 'password' && this.state.showPassword) { type = 'text'; } return (
{prefix && {prefix}} this.onChange(e)} onBlur={field.onBlur} onFocus={field.onFocus} ref={this.inputElement} disabled={field.disabled} /> {suffix && {suffix}} {showPasswordToggle && ( )} {scorePassword && (
{/* */}
)}
{field.label && showLabel && ( )} {field.error &&
{field.error}
}
); } } export default injectIntl(Input);