import React from 'react'; import cn from 'classnames'; import { useField } from 'react-final-form'; import Flex from './Flex'; import { ErrorText } from './Typography'; export default function Input({ // visuals type, name, label, className, accessory, labelAccessory, disabled = false, mono = false, obscure, // callbacks onEnter, // state config, // extra ...rest }) { const { input, meta: { active, error, submitError, dirtySinceLastSubmit, submitting, submitSucceeded, touched, valid, }, } = useField(name, config); disabled = disabled || submitting || submitSucceeded; // choose the base dom component const BaseComponent = type === 'textarea' ? 'textarea' : 'input'; // notify parent of enter keypress iff not disabled and passing // TODO: integrate this into react-final-form submission // const onKeyPress = useCallback( // e => !disabled && valid && e.key === 'Enter' && onEnter && onEnter(), // [disabled, valid] // eslint-disable-line react-hooks/exhaustive-deps // ); const showError = !!error; const showSubmitError = !!submitError && !dirtySinceLastSubmit; const indicateError = touched && !active && (showError || showSubmitError); return ( {label} {labelAccessory && {labelAccessory}} {accessory && (
{accessory}
)}
{indicateError && ( {error || submitError} )}
); }