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
50 changes: 31 additions & 19 deletions packages/odyssey-react-mui/src/MenuItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,36 +10,48 @@
* 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 { ReactNode, memo, useCallback, useContext } 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;
content?: string;
"data-testid"?: string;
jordankoschei-okta marked this conversation as resolved.
Show resolved Hide resolved
hasInitialFocus?: boolean;
href?: string;
isSelected?: boolean;
isDestructive?: boolean;
}
onClick?: MuiMenuItemProps["onClick"];
rel?: string;
rev?: string;
value?: string;
};

const MenuItem = ({ isDestructive, ...props }: MenuItemProps) => {
const MenuItem = ({
isDestructive,
onClick: onClickProp,
...props
}: MenuItemProps) => {
jordankoschei-okta marked this conversation as resolved.
Show resolved Hide resolved
const { closeMenu } = useContext(MenuContext);
console.log(closeMenu);

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

return (
<MuiMenuItem
{...props}
onClick={closeMenu}
onClick={onClick}
className={
isDestructive ? `${menuItemClasses.root}-destructive` : undefined
}
Expand Down