Autofocus search when opening; closes #2522
This commit is contained in:
parent
5eda27933f
commit
9726a0de46
1 changed files with 8 additions and 6 deletions
|
@ -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>
|
||||||
|
|
Loading…
Reference in a new issue