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

refactor: make MenuItem use our own props #1822

Merged
merged 10 commits into from
Jul 14, 2023
65 changes: 44 additions & 21 deletions packages/odyssey-react-mui/src/MenuItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,40 +10,63 @@
* See the License for the specific language governing permissions and limitations under the License.
*/

import { MenuItem as MuiMenuItem } from "@mui/material";
import {
MenuItem as MuiMenuItem,
MenuItemProps as MuiMenuItemProps,
} from "@mui/material";
import { menuItemClasses } from "@mui/material/MenuItem";
import type { MenuItemProps as MuiMenuItemProps } from "@mui/material";
import { memo, useContext } from "react";
import {
memo,
useCallback,
useContext,
type MouseEventHandler,
type ReactNode,
} from "react";

import { MenuContext } from "./MenuContext";

export interface MenuItemProps
extends Omit<
MuiMenuItemProps,
| "component"
| "dense"
| "disableGutters"
| "divider"
| "focusVisibleClassName"
> {
/**
* Toggles whether or not the MenuItem represents a destructive action.
*/
export type MenuItemProps = {
children: ReactNode;
hasInitialFocus?: boolean;
isSelected?: boolean;
isDestructive?: boolean;
}
onClick?: MuiMenuItemProps["onClick"];
value?: string;
variant?: "default" | "destructive";
};
jordankoschei-okta marked this conversation as resolved.
Show resolved Hide resolved

const MenuItem = ({ isDestructive, ...props }: MenuItemProps) => {
const MenuItem = ({
children,
hasInitialFocus,
isSelected,
onClick: onClickProp,
value,
variant = "default",
}: MenuItemProps) => {
const { closeMenu } = useContext(MenuContext);

const onClick = useCallback<MouseEventHandler<HTMLLIElement>>(
jordankoschei-okta marked this conversation as resolved.
Show resolved Hide resolved
(event) => {
onClickProp?.(event);
closeMenu();
},
[onClickProp, closeMenu]
);

return (
<MuiMenuItem
{...props}
onClick={closeMenu}
/* eslint-disable-next-line jsx-a11y/no-autofocus */
autoFocus={hasInitialFocus}
jordankoschei-okta marked this conversation as resolved.
Show resolved Hide resolved
selected={isSelected}
value={value}
onClick={onClick}
className={
isDestructive ? `${menuItemClasses.root}-destructive` : undefined
variant === "destructive"
? `${menuItemClasses.root}-destructive`
: undefined
}
>
{props.children}
{children}
</MuiMenuItem>
);
};
Expand Down