Skip to content

Commit

Permalink
Fix type warnings for onClick handlers
Browse files Browse the repository at this point in the history
  • Loading branch information
kraenhansen committed Dec 11, 2024
1 parent f75e398 commit 94f3f17
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,19 @@ export function DropdownMenuButton<Action extends string>({
const menuTriggerRef = useRef<HTMLButtonElement | null>(null);
const [isMenuOpen, setIsMenuOpen] = useState(false);

const onClick = useCallback(
const onClick: React.MouseEventHandler<HTMLElement> = useCallback(
(evt) => {
evt.stopPropagation();
if (evt.currentTarget.dataset.menuitem) {
setIsMenuOpen(false);
// Workaround for https://jira.mongodb.org/browse/PD-1674
menuTriggerRef.current?.focus();
}
onAction(evt.currentTarget.dataset.action);
const actionName = evt.currentTarget.dataset.action;
if (typeof actionName !== 'string') {
throw new Error('Expected element to have a "data-action" attribute');
}
onAction(actionName as Action);
},
[onAction]
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,14 @@ export function ItemActionGroup<Action extends string>({
isVisible = true,
'data-testid': dataTestId,
}: ItemActionGroupProps<Action>) {
const onClick = useCallback(
const onClick: React.MouseEventHandler<HTMLElement> = useCallback(
(evt) => {
evt.stopPropagation();
onAction(evt.currentTarget.dataset.action);
const actionName = evt.currentTarget.dataset.action;
if (typeof actionName !== 'string') {
throw new Error('Expected element to have a "data-action" attribute');
}
onAction(actionName as Action);
},
[onAction]
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,15 +61,19 @@ export function ItemActionMenu<Action extends string>({
const menuTriggerRef = useRef<HTMLButtonElement | null>(null);
const [isMenuOpen, setIsMenuOpen] = useState(false);

const onClick = useCallback(
const onClick: React.MouseEventHandler<HTMLElement> = useCallback(
(evt) => {
evt.stopPropagation();
if (evt.currentTarget.dataset.menuitem) {
setIsMenuOpen(false);
// Workaround for https://jira.mongodb.org/browse/PD-1674
menuTriggerRef.current?.focus();
}
onAction(evt.currentTarget.dataset.action);
const actionName = evt.currentTarget.dataset.action;
if (typeof actionName !== 'string') {
throw new Error('Expected element to have a "data-action" attribute');
}
onAction(actionName as Action);
},
[onAction]
);
Expand Down

0 comments on commit 94f3f17

Please sign in to comment.