demarches-normaliennes/app/javascript/components/ComboDepartementsSearch.jsx

51 lines
1.2 KiB
React
Raw Normal View History

import React from 'react';
import PropTypes from 'prop-types';
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
import ComboSearch from './ComboSearch';
2021-02-16 13:54:16 +01:00
import { queryClient } from './shared/queryClient';
2020-10-07 17:42:21 +02:00
const extraTerms = [{ code: '99', nom: 'Etranger' }];
function expandResultsWithForeignDepartement(term, results) {
return [
2020-10-07 17:42:21 +02:00
...results,
...matchSorter(extraTerms, term, {
keys: ['nom', 'code']
})
];
}
2020-10-07 17:42:21 +02:00
export function ComboDepartementsSearch({
addForeignDepartement = true,
...props
}) {
return (
<ComboSearch
{...props}
scope="departements"
minimumInputLength={1}
transformResult={({ code, nom }) => [code, `${code} - ${nom}`]}
transformResults={
addForeignDepartement ? expandResultsWithForeignDepartement : undefined
}
/>
);
}
function ComboDepartementsSearchDefault(params) {
2020-10-07 17:42:21 +02:00
return (
2021-02-16 13:54:16 +01:00
<QueryClientProvider client={queryClient}>
<ComboDepartementsSearch {...params} />
2021-02-16 13:54:16 +01:00
</QueryClientProvider>
2020-10-07 17:42:21 +02:00
);
}
ComboDepartementsSearch.propTypes = {
...ComboSearch.propTypes,
addForeignDepartement: PropTypes.bool
};
export default ComboDepartementsSearchDefault;