Skip to content

Commit

Permalink
refactor: convert options menu item to a function component
Browse files Browse the repository at this point in the history
  • Loading branch information
anastasialanz committed Oct 1, 2024
1 parent d147b85 commit aaa242c
Showing 1 changed file with 31 additions and 31 deletions.
62 changes: 31 additions & 31 deletions packages/react/src/components/OptionsMenu/OptionsMenuItem.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React from 'react';
import React, { forwardRef } from 'react';

export interface OptionsMenuItemProps
extends Pick<
Expand All @@ -11,39 +11,39 @@ export interface OptionsMenuItemProps
onSelect: (e: React.MouseEvent<HTMLElement>) => void;
}

class OptionsMenuItemComponent extends React.Component<OptionsMenuItemProps> {
static defaultProps = {
// eslint-disable-next-line @typescript-eslint/no-empty-function
onSelect: () => {}
};

handleClick = (event: React.MouseEvent<HTMLElement>) => {
const { disabled, onSelect } = this.props;
function OptionsMenuItemComponent({
disabled,
className,
menuItemRef,
onSelect = () => {}, // eslint-disable-line @typescript-eslint/no-empty-function
...other
}: OptionsMenuItemProps) {
function handleClick(event: React.MouseEvent<HTMLElement>) {
if (!disabled) {
onSelect(event);
}
};

render() {
const { handleClick, props } = this;
const { menuItemRef, disabled, onSelect, ...other } = props;
return (
// keydown happens in OptionsMenu which proxies to click
// eslint-disable-next-line jsx-a11y/click-events-have-key-events
<li
role="menuitem"
ref={menuItemRef}
aria-disabled={disabled}
onClick={handleClick}
{...other}
/>
);
}

return (
// keydown happens in OptionsMenu which proxies to click
// eslint-disable-next-line jsx-a11y/click-events-have-key-events
<li
role="menuitem"
ref={menuItemRef}
aria-disabled={disabled}
onClick={handleClick}
className={className}
{...other}
/>
);
}

export default React.forwardRef(function OptionsMenuItem(
props: OptionsMenuItemProps,
ref: React.Ref<HTMLLIElement>
) {
return <OptionsMenuItemComponent menuItemRef={ref} {...props} />;
});
const OptionsMenuItem = forwardRef<HTMLLIElement, OptionsMenuItemProps>(
({ ...props }, ref) => (
<OptionsMenuItemComponent menuItemRef={ref} {...props} />
)
);

OptionsMenuItem.displayName = 'OptionsMenuItem';

export default OptionsMenuItem;

0 comments on commit aaa242c

Please sign in to comment.