Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhance the to **MenuItem and fix a crash issue of reading properties of null (reading 'children') in isSubmenuFocused method. #41

Merged
merged 2 commits into from
Jan 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 3 additions & 2 deletions packages/mui-nested-menu/src/components/IconMenuItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ type IconMenuItemProps = {
className?: string;
disabled?: boolean;
label?: string;
renderLabel?: () => React.ReactNode;
leftIcon?: React.ReactNode;
onClick?: (event: React.MouseEvent<HTMLElement>) => void;
ref?: RefObject<HTMLLIElement>;
Expand All @@ -35,14 +36,14 @@ type IconMenuItemProps = {
};

export const IconMenuItem = forwardRef<HTMLLIElement, IconMenuItemProps>(function IconMenuItem(
{ MenuItemProps, className, label, leftIcon, rightIcon, ...props },
{ MenuItemProps, className, label, leftIcon, renderLabel, rightIcon, ...props },
ref
) {
return (
<StyledMenuItem {...MenuItemProps} ref={ref} className={className} {...props}>
<FlexBox>
{leftIcon}
<StyledTypography>{label}</StyledTypography>
{renderLabel ? renderLabel() : <StyledTypography>{label}</StyledTypography>}
</FlexBox>
{rightIcon}
</StyledMenuItem>
Expand Down
9 changes: 7 additions & 2 deletions packages/mui-nested-menu/src/components/NestedMenuItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export type NestedMenuItemProps = Omit<MenuItemProps, 'button'> & {
parentMenuOpen: boolean;
component?: ElementType;
label?: string;
renderLabel?: () => ReactNode;
rightIcon?: ReactNode;
leftIcon?: ReactNode;
children?: ReactNode;
Expand All @@ -39,6 +40,7 @@ const NestedMenuItem = forwardRef<HTMLLIElement | null, NestedMenuItemProps>(fun
const {
parentMenuOpen,
label,
renderLabel,
rightIcon = <ChevronRight />,
leftIcon = null,
children,
Expand Down Expand Up @@ -79,8 +81,10 @@ const NestedMenuItem = forwardRef<HTMLLIElement | null, NestedMenuItemProps>(fun
// Check if any immediate children are active
const isSubmenuFocused = () => {
const active = containerRef.current?.ownerDocument.activeElement ?? null;
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
for (const child of menuContainerRef.current!.children) {
if(menuContainerRef.current == null) {
return false;
}
for (const child of menuContainerRef.current.children) {
if (child === active) {
return true;
}
Expand Down Expand Up @@ -145,6 +149,7 @@ const NestedMenuItem = forwardRef<HTMLLIElement | null, NestedMenuItemProps>(fun
leftIcon={leftIcon}
rightIcon={rightIcon}
label={label}
renderLabel={renderLabel}
/>

<Menu
Expand Down