diff --git a/components/form.js b/components/form.js index ee481a4f..4688f1e2 100644 --- a/components/form.js +++ b/components/form.js @@ -219,7 +219,7 @@ function FormGroup ({ className, label, children }) { function InputInner ({ prepend, append, hint, showValid, onChange, overrideValue, - innerRef, noForm, clear, onKeyDown, ...props + innerRef, noForm, clear, onKeyDown, debounce, ...props }) { const [field, meta, helpers] = noForm ? [{}, {}, {}] : useField(props) const formik = noForm ? null : useFormikContext() @@ -245,6 +245,18 @@ function InputInner ({ const invalid = (!formik || formik.submitCount > 0) && meta.touched && meta.error + const debounceRef = useRef(-1) + + useEffect(() => { + if (debounceRef.current !== -1) { + clearTimeout(debounceRef.current) + } + if (!noForm && !isNaN(debounce) && debounce > 0) { + debounceRef.current = setTimeout(() => formik.validateForm(), debounce) + } + return () => clearTimeout(debounceRef.current) + }, [noForm, formik, field.value]) + return ( <> @@ -446,13 +458,14 @@ export function Checkbox ({ children, label, groupClassName, hiddenLabel, extra, const StorageKeyPrefixContext = createContext() export function Form ({ - initial, schema, onSubmit, children, initialError, validateImmediately, storageKeyPrefix, ...props + initial, schema, onSubmit, children, initialError, validateImmediately, storageKeyPrefix, validateOnChange = true, ...props }) { const [error, setError] = useState(initialError) return ( { if (name === user.name) { setEditting(false) @@ -137,6 +138,7 @@ function NymEdit ({ user, setEditting }) { autoFocus groupClassName={styles.usernameForm} showValid + debounce={500} /> setEditting(true)}>save