fix(ui): focus add node popover on open

Need an extra ref to pass to the InvSelect component.
This commit is contained in:
psychedelicious 2023-12-31 19:34:05 +11:00 committed by Kent Keirsey
parent 5168415999
commit 6209fef63d
3 changed files with 14 additions and 2 deletions

View File

@ -4,7 +4,7 @@ import type {
StylesConfig, StylesConfig,
} from 'chakra-react-select'; } from 'chakra-react-select';
import { Select as ChakraReactSelect } from 'chakra-react-select'; import { Select as ChakraReactSelect } from 'chakra-react-select';
import { memo, useMemo } from 'react'; import { memo, useEffect, useMemo } from 'react';
import { CustomMenuList } from './CustomMenuList'; import { CustomMenuList } from './CustomMenuList';
import { CustomOption } from './CustomOption'; import { CustomOption } from './CustomOption';
@ -29,7 +29,7 @@ const components: SelectComponentsConfig<
}; };
export const InvSelect = memo((props: InvSelectProps) => { export const InvSelect = memo((props: InvSelectProps) => {
const { sx, selectRef, ...rest } = props; const { sx, selectRef, inputRef, ...rest } = props;
const chakraStyles = useMemo<CustomChakraStylesConfig>( const chakraStyles = useMemo<CustomChakraStylesConfig>(
() => ({ () => ({
container: (provided, _state) => ({ ...provided, w: 'full', ...sx }), container: (provided, _state) => ({ ...provided, w: 'full', ...sx }),
@ -55,6 +55,13 @@ export const InvSelect = memo((props: InvSelectProps) => {
[sx] [sx]
); );
useEffect(() => {
if (!inputRef) {
return;
}
inputRef.current = selectRef?.current?.inputRef ?? null;
}, [inputRef, selectRef]);
return ( return (
<ChakraReactSelect<InvSelectOption, false, GroupBase<InvSelectOption>> <ChakraReactSelect<InvSelectOption, false, GroupBase<InvSelectOption>>
ref={selectRef} ref={selectRef}

View File

@ -28,6 +28,7 @@ export type InvSelectProps = ChakraReactSelectProps<
selectRef?: React.RefObject< selectRef?: React.RefObject<
SelectInstance<InvSelectOption, false, GroupBase<InvSelectOption>> SelectInstance<InvSelectOption, false, GroupBase<InvSelectOption>>
>; >;
inputRef?: React.MutableRefObject<HTMLInputElement | null>;
}; };
export type CustomChakraStylesConfig = ChakraStylesConfig< export type CustomChakraStylesConfig = ChakraStylesConfig<
InvSelectOption, InvSelectOption,

View File

@ -65,6 +65,8 @@ const AddNodePopover = () => {
const toaster = useAppToaster(); const toaster = useAppToaster();
const { t } = useTranslation(); const { t } = useTranslation();
const selectRef = useRef<SelectInstance<InvSelectOption> | null>(null); const selectRef = useRef<SelectInstance<InvSelectOption> | null>(null);
const inputRef = useRef<HTMLInputElement>(null);
const fieldFilter = useAppSelector( const fieldFilter = useAppSelector(
(state) => state.nodes.connectionStartFieldType (state) => state.nodes.connectionStartFieldType
); );
@ -201,6 +203,7 @@ const AddNodePopover = () => {
closeDelay={0} closeDelay={0}
closeOnBlur={true} closeOnBlur={true}
returnFocusOnClose={true} returnFocusOnClose={true}
initialFocusRef={inputRef}
> >
<InvPopoverAnchor> <InvPopoverAnchor>
<Flex <Flex
@ -230,6 +233,7 @@ const AddNodePopover = () => {
onChange={onChange} onChange={onChange}
onMenuClose={onClose} onMenuClose={onClose}
onKeyDown={onKeyDown} onKeyDown={onKeyDown}
inputRef={inputRef}
/> />
</InvPopoverBody> </InvPopoverBody>
</InvPopoverContent> </InvPopoverContent>