2021-06-15 14:36:37 +02:00
|
|
|
import React from 'react';
|
2021-02-16 13:54:16 +01:00
|
|
|
import { QueryClientProvider } from 'react-query';
|
2021-02-16 13:17:36 +01:00
|
|
|
import { matchSorter } from 'match-sorter';
|
2020-10-07 17:42:21 +02:00
|
|
|
|
2022-04-15 13:05:46 +02:00
|
|
|
import ComboSearch, { ComboSearchProps } from './ComboSearch';
|
2021-02-16 13:54:16 +01:00
|
|
|
import { queryClient } from './shared/queryClient';
|
2020-10-07 17:42:21 +02:00
|
|
|
|
2022-04-15 13:05:46 +02:00
|
|
|
type DepartementResult = { code: string; nom: string };
|
|
|
|
|
2020-10-07 17:42:21 +02:00
|
|
|
const extraTerms = [{ code: '99', nom: 'Etranger' }];
|
|
|
|
|
2022-04-15 13:05:46 +02:00
|
|
|
function expandResultsWithForeignDepartement(term: string, result: unknown) {
|
|
|
|
const results = result as DepartementResult[];
|
2021-06-15 14:36:37 +02:00
|
|
|
return [
|
2020-10-07 17:42:21 +02:00
|
|
|
...results,
|
|
|
|
...matchSorter(extraTerms, term, {
|
|
|
|
keys: ['nom', 'code']
|
|
|
|
})
|
2021-06-15 14:36:37 +02:00
|
|
|
];
|
|
|
|
}
|
2020-10-07 17:42:21 +02:00
|
|
|
|
2022-04-15 13:05:46 +02:00
|
|
|
type ComboDepartementsSearchProps = Omit<
|
|
|
|
ComboSearchProps<DepartementResult> & {
|
|
|
|
addForeignDepartement: boolean;
|
|
|
|
},
|
|
|
|
'transformResult' | 'transformResults'
|
|
|
|
>;
|
|
|
|
|
2021-11-25 11:28:40 +01:00
|
|
|
export function ComboDepartementsSearch({
|
|
|
|
addForeignDepartement = true,
|
2022-01-05 11:34:43 +01:00
|
|
|
...props
|
2022-04-15 13:05:46 +02:00
|
|
|
}: ComboDepartementsSearchProps) {
|
2021-10-26 16:21:47 +02:00
|
|
|
return (
|
|
|
|
<ComboSearch
|
2022-01-05 11:34:43 +01:00
|
|
|
{...props}
|
2021-10-26 16:21:47 +02:00
|
|
|
scope="departements"
|
|
|
|
minimumInputLength={1}
|
|
|
|
transformResult={({ code, nom }) => [code, `${code} - ${nom}`]}
|
2021-11-25 11:28:40 +01:00
|
|
|
transformResults={
|
|
|
|
addForeignDepartement ? expandResultsWithForeignDepartement : undefined
|
|
|
|
}
|
2021-10-26 16:21:47 +02:00
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-04-15 13:05:46 +02:00
|
|
|
export default function ComboDepartementsSearchDefault(
|
|
|
|
params: ComboDepartementsSearchProps
|
|
|
|
) {
|
2020-10-07 17:42:21 +02:00
|
|
|
return (
|
2021-02-16 13:54:16 +01:00
|
|
|
<QueryClientProvider client={queryClient}>
|
2022-01-05 11:34:43 +01:00
|
|
|
<ComboDepartementsSearch {...params} />
|
2021-02-16 13:54:16 +01:00
|
|
|
</QueryClientProvider>
|
2020-10-07 17:42:21 +02:00
|
|
|
);
|
|
|
|
}
|