Skip to content

Commit

Permalink
fix(query-builder): Fix focus management with multiple component inst…
Browse files Browse the repository at this point in the history
…ances (#74539)
  • Loading branch information
malwilley authored Jul 19, 2024
1 parent 1676ad0 commit 62d9d13
Showing 1 changed file with 5 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {useGridListItem} from '@react-aria/gridlist';
import type {ListState} from '@react-stately/list';
import type {FocusableElement, Node} from '@react-types/shared';

import {useSearchQueryBuilder} from 'sentry/components/searchQueryBuilder/context';
import type {ParseResultToken} from 'sentry/components/searchSyntax/parser';

function isInputElement(target: EventTarget): target is HTMLInputElement {
Expand All @@ -27,6 +28,7 @@ export function useQueryBuilderGridItem(
state: ListState<ParseResultToken>,
ref: RefObject<FocusableElement>
) {
const {wrapperRef} = useSearchQueryBuilder();
const {rowProps, gridCellProps} = useGridListItem({node: item}, state, ref);

// When focus is inside the input, we want to handle some things differently.
Expand Down Expand Up @@ -89,7 +91,7 @@ export function useQueryBuilderGridItem(
e.stopPropagation();

// Focus the next token
const el = document.querySelector(
const el = wrapperRef.current?.querySelector(
`[data-key="${state.collection.getKeyAfter(item.key)}"]`
);

Expand Down Expand Up @@ -118,7 +120,7 @@ export function useQueryBuilderGridItem(
e.stopPropagation();

// Focus the previous token
const el = document.querySelector(
const el = wrapperRef.current?.querySelector(
`[data-key="${state.collection.getKeyBefore(item.key)}"]`
);

Expand All @@ -133,7 +135,7 @@ export function useQueryBuilderGridItem(
}
}
},
[handleInputKeyDown, item.key, state.collection]
[handleInputKeyDown, item.key, state.collection, wrapperRef]
);

const onKeyDown = useCallback(
Expand Down

0 comments on commit 62d9d13

Please sign in to comment.