Skip to content

Commit 5e8935a

Browse files
committed
Fix type warnings for onClick handlers
1 parent 75cf1ff commit 5e8935a

File tree

3 files changed

+18
-6
lines changed

3 files changed

+18
-6
lines changed

packages/compass-components/src/components/actions/dropdown-menu-button.tsx

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,15 +48,19 @@ export function DropdownMenuButton<Action extends string>({
4848
const menuTriggerRef = useRef<HTMLButtonElement | null>(null);
4949
const [isMenuOpen, setIsMenuOpen] = useState(false);
5050

51-
const onClick = useCallback(
51+
const onClick: React.MouseEventHandler<HTMLElement> = useCallback(
5252
(evt) => {
5353
evt.stopPropagation();
5454
if (evt.currentTarget.dataset.menuitem) {
5555
setIsMenuOpen(false);
5656
// Workaround for https://jira.mongodb.org/browse/PD-1674
5757
menuTriggerRef.current?.focus();
5858
}
59-
onAction(evt.currentTarget.dataset.action);
59+
const actionName = evt.currentTarget.dataset.action;
60+
if (typeof actionName !== 'string') {
61+
throw new Error('Expected element to have a "data-action" attribute');
62+
}
63+
onAction(actionName as Action);
6064
},
6165
[onAction]
6266
);

packages/compass-components/src/components/actions/item-action-group.tsx

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,14 @@ export function ItemActionGroup<Action extends string>({
4343
isVisible = true,
4444
'data-testid': dataTestId,
4545
}: ItemActionGroupProps<Action>) {
46-
const onClick = useCallback(
46+
const onClick: React.MouseEventHandler<HTMLElement> = useCallback(
4747
(evt) => {
4848
evt.stopPropagation();
49-
onAction(evt.currentTarget.dataset.action);
49+
const actionName = evt.currentTarget.dataset.action;
50+
if (typeof actionName !== 'string') {
51+
throw new Error('Expected element to have a "data-action" attribute');
52+
}
53+
onAction(actionName as Action);
5054
},
5155
[onAction]
5256
);

packages/compass-components/src/components/actions/item-action-menu.tsx

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,15 +61,19 @@ export function ItemActionMenu<Action extends string>({
6161
const menuTriggerRef = useRef<HTMLButtonElement | null>(null);
6262
const [isMenuOpen, setIsMenuOpen] = useState(false);
6363

64-
const onClick = useCallback(
64+
const onClick: React.MouseEventHandler<HTMLElement> = useCallback(
6565
(evt) => {
6666
evt.stopPropagation();
6767
if (evt.currentTarget.dataset.menuitem) {
6868
setIsMenuOpen(false);
6969
// Workaround for https://jira.mongodb.org/browse/PD-1674
7070
menuTriggerRef.current?.focus();
7171
}
72-
onAction(evt.currentTarget.dataset.action);
72+
const actionName = evt.currentTarget.dataset.action;
73+
if (typeof actionName !== 'string') {
74+
throw new Error('Expected element to have a "data-action" attribute');
75+
}
76+
onAction(actionName as Action);
7377
},
7478
[onAction]
7579
);

0 commit comments

Comments
 (0)