import React, { useCallback, useEffect } from 'react'; import cn from 'classnames'; import Flex from './Flex'; import { ErrorText } from './Typography'; export default function Input({ // visuals name, label, className, accessory, mono = false, // callbacks onValue, onPass, onError, onFocus, onEnter, // state from hook focused, pass, visiblyPassed, error, data, bind, autoFocus, disabled, // ignored initialValue, validators, transformers, // extra ...rest }) { // NB(shrugs): we disable exhaustive deps because we don't want the callbacks // (whose identity might change between renders) to trigger a re-notify of // state that we already know // this also happens to prevent render loops when rendering a map of inputs // notify parent of enter keypress iff not disabled and passing const onKeyPress = useCallback( e => !disabled && pass && e.key === 'Enter' && onEnter && onEnter(), [disabled, pass] // eslint-disable-line react-hooks/exhaustive-deps ); // notify parent of value only when passing useEffect(() => { pass && onValue && onValue(data); }, [pass, data]); // eslint-disable-line react-hooks/exhaustive-deps // notify parent of pass useEffect(() => { onPass && onPass(pass); }, [pass]); // eslint-disable-line react-hooks/exhaustive-deps // notify parent of error whenever error changes useEffect(() => { onError && onError(error); }, [error]); // eslint-disable-line react-hooks/exhaustive-deps // notify parent of focus useEffect(() => { onFocus && onFocus(focused); }, [focused]); // eslint-disable-line react-hooks/exhaustive-deps return ( {label} {accessory && (
{accessory}
)}
{error && ( {error} )}
); }