import React, { Component } from 'react'; import { bool, func, object, shape, string } from 'prop-types'; import { Field } from 'react-final-form'; import classNames from 'classnames'; import { ValidationError, ExpandingTextarea } from '../../components'; import css from './FieldTextInput.css'; const CONTENT_MAX_LENGTH = 5000; class FieldTextInputComponent extends Component { render() { /* eslint-disable no-unused-vars */ const { rootClassName, className, inputRootClass, customErrorText, id, label, input, meta, onUnmount, isUncontrolled, inputRef, ...rest } = this.props; /* eslint-enable no-unused-vars */ if (label && !id) { throw new Error('id required when a label is given'); } const { valid, invalid, touched, error } = meta; const isTextarea = input.type === 'textarea'; const errorText = customErrorText || error; // Error message and input error styles are only shown if the // field has been touched and the validation has failed. const hasError = !!customErrorText || !!(touched && invalid && error); const fieldMeta = { touched: hasError, error: errorText }; // Textarea doesn't need type. const { type, ...inputWithoutType } = input; // Uncontrolled input uses defaultValue instead of value. const { value: defaultValue, ...inputWithoutValue } = input; // Use inputRef if it is passed as prop. const refMaybe = inputRef ? { ref: inputRef } : {}; const inputClasses = inputRootClass || classNames(css.input, { [css.inputSuccess]: valid, [css.inputError]: hasError, [css.textarea]: isTextarea, }); const maxLength = CONTENT_MAX_LENGTH; const inputProps = isTextarea ? { className: inputClasses, id, rows: 1, maxLength, ...refMaybe, ...inputWithoutType, ...rest, } : isUncontrolled ? { className: inputClasses, id, type, defaultValue, ...refMaybe, ...inputWithoutValue, ...rest, } : { className: inputClasses, id, type, ...refMaybe, ...input, ...rest }; const classes = classNames(rootClassName || css.root, className); return (