Skip to content

Commit

Permalink
refactor: remove "as any" assignments
Browse files Browse the repository at this point in the history
  • Loading branch information
Xiphe committed Feb 5, 2021
1 parent 7c632b6 commit 4e67325
Showing 1 changed file with 27 additions and 4 deletions.
31 changes: 27 additions & 4 deletions src/views/Budget/Header.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,34 @@ type Props = {
scrollRef: MutableRefObject<((target: HTMLDivElement) => void) | null>;
};

function isHTMLElement(target: EventTarget): target is HTMLElement {
return typeof (target as any).nodeName === 'string';
}

function isHTMLButtonElement(elm: HTMLElement): elm is HTMLButtonElement {
return elm.nodeName === 'BUTTON';
}

function findButton(
target: EventTarget | HTMLElement | null,
): null | HTMLButtonElement {
if (!target || !isHTMLElement(target)) {
return null;
}
if (isHTMLButtonElement(target)) {
return target;
}
return findButton(target.parentElement);
}

export default function BudgetHeader({ scrollRef, onClick }: Props) {
const months: { date: Date; key: string }[] = useMonths();
const handleClick = useCallback(
({ target }: { target: HTMLButtonElement }) => {
onClick(target.name);
({ target }: React.MouseEvent<HTMLButtonElement, MouseEvent>) => {
const button = findButton(target);
if (button) {
onClick(button.name);
}
},
[onClick],
);
Expand Down Expand Up @@ -111,7 +134,7 @@ export default function BudgetHeader({ scrollRef, onClick }: Props) {
visibleMonthKeys.includes(key) &&
styles.currentMonthListEntry,
)}
onClick={handleClick as any}
onClick={handleClick}
>
{format(date, 'MMM')}
</button>
Expand All @@ -122,7 +145,7 @@ export default function BudgetHeader({ scrollRef, onClick }: Props) {
<button
name={formatDateKey(new Date())}
className={styles.todayButton}
onClick={handleClick as any}
onClick={handleClick}
>
Today
</button>
Expand Down

0 comments on commit 4e67325

Please sign in to comment.