Skip to content

Commit

Permalink
chore: better typing
Browse files Browse the repository at this point in the history
  • Loading branch information
gustaveWPM committed Apr 15, 2024
1 parent 0717644 commit f2e0b53
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ import Link from 'next/link';

interface NavbarSearchButtonDialogDefaultViewProps {
quickMenuLeftRightCustomHandler: (e: ReactKeyboardEvent<HTMLAnchorElement>) => void;
updateMemorizedTabValueAndSetTabValue: (v: string) => void;
updateMemorizedTabValueAndSetTabValue: (v: WeakTabValue) => void;
quickAccessBtns: [string, QuickAccessBtnMetadatas][];
banners: [string, BannersMetadatas][];
focusInputField: () => void;
tabValue: string;
tabValue: WeakTabValue;
}

const NavbarSearchButtonDialogDefaultView: FunctionComponent<NavbarSearchButtonDialogDefaultViewProps> = ({
Expand Down Expand Up @@ -58,7 +58,7 @@ const NavbarSearchButtonDialogDefaultView: FunctionComponent<NavbarSearchButtonD
}
)}
onClick={() => {
updateMemorizedTabValueAndSetTabValue(category);
updateMemorizedTabValueAndSetTabValue(category as WeakTabValue);
focusInputField();
}}
aria-label={title}
Expand Down Expand Up @@ -110,3 +110,5 @@ enum EBannerPosition {
MIDDLE,
LAST
}

type WeakTabValue = string;
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use client';

import type { QuickAccessBtnMetadatas, navbarSearchBtnProps, BannersMetadatas } from '@/config/searchMenu';
import type { KeyboardEvent as ReactKeyboardEvent, ChangeEventHandler, ReactElement } from 'react';
import type { QuickAccessBtnMetadatas, BannersMetadatas } from '@/config/searchMenu';
import type { MaybeNull } from '@rtm/shared-types/CustomUtilityTypes';
import type { I18nVocabTarget } from '@rtm/shared-types/I18n';
import type { Index } from '@rtm/shared-types/Numbers';
Expand Down Expand Up @@ -42,7 +42,7 @@ interface NavbarSearchButtonProps<AllTabValues extends readonly string[]> {
const SEARCH_TEXT_INITIAL_STATE = '';
const RESULTS_INITIAL_STATE: MaybeNull<ReactElement[]> = null;

const NavbarSearchButtonInner = <AllTabValues extends readonly string[]>({
const NavbarSearchButtonInner = <AllTabValues extends typeof navbarSearchBtnProps.allTabValues>({
quickAccessBtns: orgQuickAccessBtns,
allTabValues: orgAllTabValues,
tabTriggers: orgTabTriggers,
Expand All @@ -67,7 +67,7 @@ const NavbarSearchButtonInner = <AllTabValues extends readonly string[]>({
const [isOpened, setIsOpened] = useState<boolean>(false);
const [searchText, setSearchText] = useState<string>(SEARCH_TEXT_INITIAL_STATE);
const [results, setResults] = useState<MaybeNull<ReactElement[]>>(RESULTS_INITIAL_STATE);
const [tabValue, setTabValue] = useState<TabValue>(tabValueInitialState);
const [tabValue, setTabValue] = useState(tabValueInitialState);
const pathnameAtOpen = useRef<MaybeNull<AppPath>>(null);
const inputFieldRef = useRef<HTMLInputElement>(null);
const prevScreenBtnRef = useRef<HTMLButtonElement>(null);
Expand Down Expand Up @@ -188,7 +188,7 @@ const NavbarSearchButtonInner = <AllTabValues extends readonly string[]>({

const defaultView = (
<NavbarSearchButtonDialogDefaultView
updateMemorizedTabValueAndSetTabValue={updateMemorizedTabValueAndSetTabValue}
updateMemorizedTabValueAndSetTabValue={updateMemorizedTabValueAndSetTabValue as (v: string) => void}
quickMenuLeftRightCustomHandler={quickMenuLeftRightCustomHandler}
focusInputField={focusInputField}
quickAccessBtns={quickAccessBtns}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,16 +52,20 @@ const __fakeResults = [
<Result metaTitle={'metaTitle'} excerpt={'excerpt'} key={'stupid38'} href={'#osef'} />
];

export function doUpdateMemorizedTabValueAndSetTabValue(v: string, memorizedTabValue: MutableRefObject<string>, setTabValue: (v: string) => unknown) {
export function doUpdateMemorizedTabValueAndSetTabValue<TabValue extends string>(
v: TabValue,
memorizedTabValue: MutableRefObject<string>,
setTabValue: (v: TabValue) => unknown
) {
memorizedTabValue.current = v;
setTabValue(v);
}

export const doBuildTabTrigger = (
tabValue: string,
export const doBuildTabTrigger = <TabValue extends string>(
tabValue: TabValue,
title: string,
memorizedTabValue: MutableRefObject<string>,
setTabValue: (v: string) => unknown
memorizedTabValue: MutableRefObject<TabValue>,
setTabValue: (v: TabValue) => unknown
) => (
<TabsTrigger
onFocusCapture={(e) => {
Expand Down

0 comments on commit f2e0b53

Please sign in to comment.