import {action, observable} from 'mobx'; import {observer} from 'mobx-react'; import * as React from 'react'; import {ErrorMessage} from '../ErrorMessage'; import {Icon, IconName} from '../Icon'; import {Label} from '../Label'; import styles from './TextInputField.scss'; import classNames from 'classnames'; interface TextInputFieldProps { value: string; onChange?: (newValue: string) => void; type?: 'text' | 'password'; name?: string; label?: string; placeholder?: string; disabled?: boolean; leftIcon?: IconName | JSX.Element; rightIcon?: IconName | JSX.Element; maxLength?: number; error?: string | boolean; selectAllOnFocus?: boolean; onFocus?: () => void; onBlur?: () => void; onKeyDown?: (event: React.KeyboardEvent) => void; onPaste?: (event: React.ClipboardEvent) => void; inputRef?: (ref: HTMLInputElement) => void; className?: string; inputClassName?: string; } @observer export class TextInputField extends React.Component { @observable private isFocused = false; private inputReference: HTMLInputElement; private handleChange = (e: React.ChangeEvent) => { this.props.onChange?.(e.target.value); } @action private handleFocus = () => { const {selectAllOnFocus, onFocus} = this.props; this.isFocused = true; if (selectAllOnFocus) { this.inputReference.select(); } onFocus?.(); } @action private handleBlur = () => { this.isFocused = false; this.props.onBlur?.(); } private handleReference = (inputReference: HTMLInputElement) => { const {inputRef} = this.props; this.inputReference = inputReference; inputRef?.(inputReference); } private renderLeftIcon() { const {leftIcon} = this.props; if (!leftIcon) { return null; } const leftIconElement = typeof leftIcon === 'string' ? : leftIcon; return ( <> {leftIconElement}
); } private renderRightIcon() { const {rightIcon} = this.props; if (!rightIcon) { return null; } return typeof rightIcon === 'string' ? : rightIcon; } public render() { const {isFocused} = this; const {value, type, name, label, placeholder, disabled, leftIcon, rightIcon, maxLength, error, onKeyDown, onPaste, className, inputClassName} = this.props; return (
{label &&
); } }