Skip to content

Commit 6285d8a

Browse files
authored
Fix: Allow Keyboard navigation on instance selection list (#2307)
1 parent 70f284d commit 6285d8a

File tree

1 file changed

+26
-7
lines changed

1 file changed

+26
-7
lines changed

src/shell/components/global-sidebar/components/InstanceMenu/Flyouts/InstancesList.tsx

+26-7
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,21 @@ export const InstancesList = () => {
4040
const user: User = useSelector((state: AppState) => state.user);
4141
const { data: instances, isLoading } = useGetInstancesQuery();
4242

43+
const [menuFocused, setMenuFocused] = useState(false);
44+
45+
const handleArrowNavigation = (e: React.KeyboardEvent) => {
46+
e.stopPropagation();
47+
e.preventDefault();
48+
49+
if (e.key === "ArrowDown" && !menuFocused) {
50+
const menuItem = document.querySelector(
51+
".MuiListSubheader-root + div li:first-of-type"
52+
);
53+
(menuItem as HTMLElement)?.focus();
54+
setMenuFocused(true);
55+
}
56+
};
57+
4358
const favoriteInstances = useMemo(() => {
4459
if (user && instances?.length) {
4560
let data: Instance[] = [];
@@ -140,6 +155,7 @@ export const InstancesList = () => {
140155
<ArrowForwardIosRoundedIcon color="action" fontSize="small" />
141156
</>
142157
}
158+
onKeyDown={handleArrowNavigation}
143159
PopperProps={{
144160
popperOptions: {
145161
modifiers: [
@@ -176,13 +192,6 @@ export const InstancesList = () => {
176192
height: 72,
177193
borderRadius: "8px 8px 0px 0px",
178194
}}
179-
onKeyDown={(e: React.KeyboardEvent) => {
180-
const allowedKeys = ["ArrowUp", "ArrowDown", "Escape"];
181-
182-
if (!allowedKeys.includes(e.key)) {
183-
e.stopPropagation();
184-
}
185-
}}
186195
>
187196
<TextField
188197
autoFocus
@@ -191,6 +200,16 @@ export const InstancesList = () => {
191200
value={filter}
192201
onChange={(e) => setFilter(e.target.value)}
193202
inputRef={searchField}
203+
onFocus={() => {
204+
setMenuFocused(false);
205+
}}
206+
onKeyDown={(e: React.KeyboardEvent) => {
207+
const allowedKeys = ["ArrowUp", "ArrowDown", "Escape"];
208+
209+
if (!allowedKeys.includes(e.key)) {
210+
e.stopPropagation();
211+
}
212+
}}
194213
/>
195214
</ListSubheader>
196215
{filter && !filteredInstances?.length ? (

0 commit comments

Comments
 (0)