11import without from 'lodash/without' ;
2- import { FC } from 'react' ;
3- import { useController } from 'react-hook-form' ;
2+ import { FC , ReactElement } from 'react' ;
3+ import { FieldValues , Path , useController } from 'react-hook-form' ;
44import { useTranslation } from 'react-i18next' ;
55import { twJoin , twMerge } from 'tailwind-merge' ;
6- import { mapStopRegistryTransportModeTypeToUiName } from '../../../../../../i18n/uiNameMappings' ;
7- import { Row } from '../../../../../../layoutComponents' ;
8- import { JoreStopRegistryTransportModeType } from '../../../../../../types/stop-registry' ;
9- import { AllOptionEnum } from '../../../../../../utils' ;
10- import { StopSearchFilters } from '../../../types' ;
11- import { stopSearchBarTestIds } from '../stopSearchBarTestIds' ;
12- import { DisableableFilterProps } from '../Types/DisableableFilterProps' ;
6+ import { TranslationKey } from '../../../../i18n' ;
7+ import { mapStopRegistryTransportModeTypeToUiName } from '../../../../i18n/uiNameMappings' ;
8+ import { Row } from '../../../../layoutComponents' ;
9+ import { JoreStopRegistryTransportModeType } from '../../../../types/stop-registry' ;
10+ import { AllOptionEnum } from '../../../../utils' ;
1311import s from './TransportationModeFilter.module.css' ;
1412
13+ const testIds = {
14+ transportationModeButton : (
15+ prefix : string ,
16+ mode : JoreStopRegistryTransportModeType ,
17+ ) => `${ prefix } ::transportationMode::${ mode } ` ,
18+ } ;
19+
1520const modeIconMap : Readonly < Record < JoreStopRegistryTransportModeType , string > > =
1621 {
1722 [ JoreStopRegistryTransportModeType . Bus ] : 'icon-bus' ,
@@ -25,12 +30,14 @@ type TransportationModeButtonProps = {
2530 readonly isSelected : ( mode : JoreStopRegistryTransportModeType ) => boolean ;
2631 readonly mode : JoreStopRegistryTransportModeType ;
2732 readonly onToggle : ( mode : JoreStopRegistryTransportModeType ) => void ;
33+ readonly testIdPrefix : string ;
2834} ;
2935
3036const TransportationModeButton : FC < TransportationModeButtonProps > = ( {
3137 isSelected,
3238 mode,
3339 onToggle,
40+ testIdPrefix,
3441} ) => {
3542 const { t } = useTranslation ( ) ;
3643
@@ -44,7 +51,7 @@ const TransportationModeButton: FC<TransportationModeButtonProps> = ({
4451 'aria-checked:border-tweaked-brand aria-checked:bg-tweaked-brand aria-checked:text-white' ,
4552 modeIconMap [ mode ] ,
4653 ) }
47- data-testid = { stopSearchBarTestIds . transportationModeButton ( mode ) }
54+ data-testid = { testIds . transportationModeButton ( testIdPrefix , mode ) }
4855 onClick = { ( ) => onToggle ( mode ) }
4956 role = "checkbox"
5057 type = "button"
@@ -60,20 +67,32 @@ const options: ReadonlyArray<JoreStopRegistryTransportModeType> = [
6067 JoreStopRegistryTransportModeType . Metro ,
6168] ;
6269
63- export const TransportationModeFilter : FC < DisableableFilterProps > = ( {
70+ type TransportationModeFilterProps < FormState extends FieldValues > = {
71+ readonly fieldPath : Path < FormState > ;
72+ readonly translationPrefix : TranslationKey ;
73+ readonly testIdPrefix : string ;
74+ readonly className ?: string ;
75+ readonly disabled ?: boolean ;
76+ } ;
77+
78+ export const TransportationModeFilter = < FormState extends FieldValues > ( {
79+ fieldPath,
80+ translationPrefix,
81+ testIdPrefix,
6482 className,
6583 disabled,
66- } ) => {
84+ } : TransportationModeFilterProps < FormState > ) : ReactElement => {
6785 const { t } = useTranslation ( ) ;
6886
6987 const {
7088 field : { value, onBlur, onChange } ,
71- } = useController < StopSearchFilters , 'transportationMode' > ( {
72- name : 'transportationMode' ,
89+ } = useController < FormState , typeof fieldPath > ( {
90+ name : fieldPath ,
7391 disabled,
7492 } ) ;
7593
7694 const isSelected = ( mode : JoreStopRegistryTransportModeType ) =>
95+ // eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing
7796 value . includes ( AllOptionEnum . All ) || value . includes ( mode ) ;
7897
7998 const onToggle = ( mode : JoreStopRegistryTransportModeType ) => {
@@ -101,14 +120,15 @@ export const TransportationModeFilter: FC<DisableableFilterProps> = ({
101120
102121 return (
103122 < fieldset className = { twMerge ( 'flex flex-col' , className ) } onBlur = { onBlur } >
104- < label > { t ( 'stopRegistrySearch.fieldLabels. transportMode' ) } </ label >
123+ < label > { t ( ` ${ translationPrefix } . transportMode` ) } </ label >
105124 < Row className = { twJoin ( 'gap-1' , s . noIconMargins ) } >
106125 { options . map ( ( mode ) => (
107126 < TransportationModeButton
108127 key = { mode }
109128 mode = { mode }
110129 onToggle = { onToggle }
111130 isSelected = { isSelected }
131+ testIdPrefix = { testIdPrefix }
112132 />
113133 ) ) }
114134 </ Row >
0 commit comments