Autofocus search when opening; closes #2522

This commit is contained in:
Dane Everitt 2020-10-15 20:09:13 -07:00
parent 5eda27933f
commit 9726a0de46
No known key found for this signature in database
GPG key ID: EEA66103B3D71F53

View file

@ -63,22 +63,24 @@ export default ({ ...props }: Props) => {
console.error(error); console.error(error);
addError({ key: 'search', message: httpErrorToHuman(error) }); addError({ key: 'search', message: httpErrorToHuman(error) });
}) })
.then(() => setLoading(false)); .then(() => setLoading(false))
.then(() => ref.current?.focus());
}, 500); }, 500);
useEffect(() => { useEffect(() => {
if (props.visible) { if (props.visible) {
setTimeout(() => ref.current?.focus(), 250); if (ref.current) ref.current.focus();
} }
}, [ props.visible ]); }, [ props.visible ]);
// Formik does not support an innerRef on custom components.
const InputWithRef = (props: any) => <Input {...props} ref={ref}/>;
return ( return (
<Formik <Formik
onSubmit={search} onSubmit={search}
validationSchema={object().shape({ validationSchema={object().shape({
term: string() term: string().min(3, 'Please enter at least three characters to begin searching.'),
.min(3, 'Please enter at least three characters to begin searching.')
.required('A search term must be provided.'),
})} })}
initialValues={{ term: '' } as Values} initialValues={{ term: '' } as Values}
> >
@ -95,7 +97,7 @@ export default ({ ...props }: Props) => {
> >
<SearchWatcher/> <SearchWatcher/>
<InputSpinner visible={loading}> <InputSpinner visible={loading}>
<Field as={Input} innerRef={ref} name={'term'}/> <Field as={InputWithRef} name={'term'}/>
</InputSpinner> </InputSpinner>
</FormikFieldWrapper> </FormikFieldWrapper>
</Form> </Form>