From 029ca0b412d454cd2a649ac4d38eeb22e2ee82bf Mon Sep 17 00:00:00 2001 From: Mats Eriksson Date: Sat, 4 Jan 2025 18:12:59 +0200 Subject: [PATCH] fix: current step comparison and stepper handle click - also fixes reservation-unit-card aria-hidden --- .../application/ApplicationPage.tsx | 60 ++++++++++++------- .../application/reservation-unit-card.tsx | 6 +- 2 files changed, 43 insertions(+), 23 deletions(-) diff --git a/apps/ui/components/application/ApplicationPage.tsx b/apps/ui/components/application/ApplicationPage.tsx index 19e481f66..df9003e20 100644 --- a/apps/ui/components/application/ApplicationPage.tsx +++ b/apps/ui/components/application/ApplicationPage.tsx @@ -92,13 +92,38 @@ type ApplicationPageProps = { headContent?: React.ReactNode; }; +const getStep = (slug: string) => { + switch (slug) { + case "page1": + return 0; + case "page2": + return 1; + case "page3": + return 2; + case "preview": + return 3; + default: + return 0; + } +}; + +const getStepState = (completedStep: number, step: number) => { + if (completedStep === step) { + return StepState.completed; + } + if (completedStep > step) { + return StepState.completed; + } + return StepState.disabled; +}; + export function ApplicationPageWrapper({ application, translationKeyPrefix, headContent, overrideText, children, -}: ApplicationPageProps): JSX.Element { +}: Readonly): JSX.Element { const { t, i18n } = useTranslation(); const router = useRouter(); const { asPath, push } = router; @@ -107,37 +132,32 @@ export function ApplicationPageWrapper({ const hideStepper = pages.filter((x) => router.asPath.match(`/${x}`)).length === 0; - + const completedStep = calculateCompletedStep(application); const steps = pages.map((x, i) => ({ label: t(`application:navigation.${x}`), - state: - calculateCompletedStep(application) === i - ? StepState.available - : calculateCompletedStep(application) < i - ? StepState.disabled - : StepState.completed, + state: getStepState(completedStep, i), })); - const handleStepClick = (e: React.MouseEvent) => { - const target = e.currentTarget; - const s = - Number( - target.getAttribute("data-testid")?.replace("hds-stepper-step-", "") - ) + 1; - if (s === 4 ? asPath.endsWith("preview") : asPath.includes(`page${s}`)) { + const handleStepClick = (i: number) => { + if (Number.isNaN(i) || i > 3) return; // invalid step + const targetPageIndex = i + 1; + if ( + targetPageIndex === 4 + ? asPath.endsWith("preview") + : asPath.includes(`page${targetPageIndex}`) + ) { return; // already on the page, so do nothing } - if (s === 4) { + if (targetPageIndex === 4) { push(`${getApplicationPath(application?.pk)}preview`); } else { - push(`${getApplicationPath(application?.pk)}page${s}`); + push(`${getApplicationPath(application?.pk)}page${targetPageIndex}`); } }; const title = t(`${translationKeyPrefix}.heading`); const subTitle = headContent || overrideText || t(`${translationKeyPrefix}.text`); - const selectedStep = asPath.charAt(asPath.length - 1); return ( <> @@ -150,8 +170,8 @@ export function ApplicationPageWrapper({ handleStepClick(i)} /> )} diff --git a/apps/ui/components/application/reservation-unit-card.tsx b/apps/ui/components/application/reservation-unit-card.tsx index f3468e100..dfbae3740 100644 --- a/apps/ui/components/application/reservation-unit-card.tsx +++ b/apps/ui/components/application/reservation-unit-card.tsx @@ -240,25 +240,25 @@ export function ReservationUnitCard({ { onDelete(reservationUnit); }} + iconRight={undefined} > {t("reservationUnitList:buttonRemove")} } + iconLeft={ } + iconLeft={