-
Notifications
You must be signed in to change notification settings - Fork 79
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(site): Crée un composant indépendant pour les résultats de r…
…echerche d'entreprise
- Loading branch information
Showing
11 changed files
with
110 additions
and
104 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
84 changes: 84 additions & 0 deletions
84
site/source/components/entreprise/EntrepriseSearchField.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
import { useSearchFieldState } from '@react-stately/searchfield' | ||
import { ReactNode, useEffect, useRef } from 'react' | ||
import { useTranslation } from 'react-i18next' | ||
|
||
import { SearchableSelectField } from '@/design-system/field/SearchableSelectField/SearchableSelectField' | ||
import { Grid } from '@/design-system/layout' | ||
import { Entreprise } from '@/domaine/Entreprise' | ||
import useSearchCompany from '@/hooks/useSearchCompany' | ||
|
||
import { Appear } from '../ui/animate' | ||
import EntrepriseSearchResults from './EntrepriseSearchResults' | ||
|
||
export function EntrepriseSearchField(props: { | ||
label?: ReactNode | ||
selectedValue?: ReactNode | null | ||
onValue?: () => void | ||
onClear?: () => void | ||
onSubmit?: (search: Entreprise | null) => void | ||
}) { | ||
const { t } = useTranslation() | ||
const refResults = useRef<Entreprise[] | null>(null) | ||
|
||
const searchFieldProps = { | ||
...props, | ||
label: | ||
!props.selectedValue && | ||
t('EntrepriseSearchField.label', "Nom de l'entreprise, SIREN ou SIRET"), | ||
description: | ||
!props.selectedValue && | ||
t( | ||
'EntrepriseSearchField.description', | ||
'Le numéro Siret est un numéro de 14 chiffres unique pour chaque entreprise. Exemple : 40123778000127' | ||
), | ||
onSubmit() { | ||
const results = refResults.current | ||
props.onSubmit?.(results?.[0] ?? null) | ||
}, | ||
onClear() { | ||
props.onClear?.() | ||
}, | ||
placeholder: t( | ||
'EntrepriseSearchField.placeholder', | ||
'Exemple : Café de la gare ou 40123778000127' | ||
), | ||
} | ||
|
||
const state = useSearchFieldState(searchFieldProps) | ||
|
||
const { onSubmit } = props | ||
|
||
const [searchPending, results] = useSearchCompany(state.value) | ||
|
||
useEffect(() => { | ||
refResults.current = results ?? null | ||
}, [results]) | ||
|
||
return ( | ||
<Grid container> | ||
<Grid item xs={12}> | ||
<SearchableSelectField | ||
data-test-id="company-search-input" | ||
state={state} | ||
isSearchStalled={searchPending} | ||
aria-label={ | ||
searchFieldProps.label + | ||
', ' + | ||
t( | ||
"recherche lancée automatiquement après l'entrée de caractères, les résultats s'afficheront à la suite de cet élément." | ||
) | ||
} | ||
{...searchFieldProps} | ||
/> | ||
</Grid> | ||
|
||
<Grid item xs={12}> | ||
<Appear unless={searchPending || !state.value}> | ||
{state.value && !searchPending && !props.selectedValue && ( | ||
<EntrepriseSearchResults results={results} onSubmit={onSubmit} /> | ||
)} | ||
</Appear> | ||
</Grid> | ||
</Grid> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters