-
Notifications
You must be signed in to change notification settings - Fork 86
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(action-popover): ensure that opening using the up arrow focuses l…
…ast element in the menu Currently we have a bug whereby when a user presses the up arrow key, the first focusable item in the menu is focused. This appears to be a bug as we have code that specifically handles this behaviour but is overridden elsewhere in the code. This fix ensures that the last focusable item is focused. fixes #6826
- Loading branch information
1 parent
e5d2ff5
commit 38aaed9
Showing
9 changed files
with
367 additions
and
70 deletions.
There are no files selected for viewing
30 changes: 30 additions & 0 deletions
30
src/components/action-popover/__internal__/action-popover-utils.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import React from "react"; | ||
import { ActionPopoverItem } from "../action-popover-item/action-popover-item.component"; | ||
|
||
// Reusable type alias for item types | ||
type ReactItem = React.ReactChild | React.ReactFragment | React.ReactPortal; | ||
|
||
export const getItems = ( | ||
children: React.ReactNode | React.ReactNode[] | ||
): ReactItem[] => | ||
React.Children.toArray(children).filter( | ||
(child) => React.isValidElement(child) && child.type === ActionPopoverItem | ||
); | ||
|
||
export const isItemDisabled = (item: ReactItem) => | ||
React.isValidElement(item) && !!item.props?.disabled; | ||
|
||
export const findFirstFocusableItem = (items: ReactItem[]): number => | ||
items.findIndex((_, index) => !isItemDisabled(items[index])); | ||
|
||
// FIX-ME: FE-6248 | ||
// Once we no longer support Node 16, this function can be removed and `findLastIndex()` can be used in its place. | ||
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/findLastIndex | ||
export const findLastFocusableItem = (items: ReactItem[]): number => { | ||
for (let i = items.length - 1; i >= 0; i--) { | ||
if (!isItemDisabled(items[i])) { | ||
return i; | ||
} | ||
} | ||
return -1; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.