Skip to content

Commit 4e67325

Browse files
committed
refactor: remove "as any" assignments
1 parent 7c632b6 commit 4e67325

File tree

1 file changed

+27
-4
lines changed

1 file changed

+27
-4
lines changed

src/views/Budget/Header.tsx

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,34 @@ type Props = {
1818
scrollRef: MutableRefObject<((target: HTMLDivElement) => void) | null>;
1919
};
2020

21+
function isHTMLElement(target: EventTarget): target is HTMLElement {
22+
return typeof (target as any).nodeName === 'string';
23+
}
24+
25+
function isHTMLButtonElement(elm: HTMLElement): elm is HTMLButtonElement {
26+
return elm.nodeName === 'BUTTON';
27+
}
28+
29+
function findButton(
30+
target: EventTarget | HTMLElement | null,
31+
): null | HTMLButtonElement {
32+
if (!target || !isHTMLElement(target)) {
33+
return null;
34+
}
35+
if (isHTMLButtonElement(target)) {
36+
return target;
37+
}
38+
return findButton(target.parentElement);
39+
}
40+
2141
export default function BudgetHeader({ scrollRef, onClick }: Props) {
2242
const months: { date: Date; key: string }[] = useMonths();
2343
const handleClick = useCallback(
24-
({ target }: { target: HTMLButtonElement }) => {
25-
onClick(target.name);
44+
({ target }: React.MouseEvent<HTMLButtonElement, MouseEvent>) => {
45+
const button = findButton(target);
46+
if (button) {
47+
onClick(button.name);
48+
}
2649
},
2750
[onClick],
2851
);
@@ -111,7 +134,7 @@ export default function BudgetHeader({ scrollRef, onClick }: Props) {
111134
visibleMonthKeys.includes(key) &&
112135
styles.currentMonthListEntry,
113136
)}
114-
onClick={handleClick as any}
137+
onClick={handleClick}
115138
>
116139
{format(date, 'MMM')}
117140
</button>
@@ -122,7 +145,7 @@ export default function BudgetHeader({ scrollRef, onClick }: Props) {
122145
<button
123146
name={formatDateKey(new Date())}
124147
className={styles.todayButton}
125-
onClick={handleClick as any}
148+
onClick={handleClick}
126149
>
127150
Today
128151
</button>

0 commit comments

Comments
 (0)