Merge pull request #4699 from tchak/search-by-code

Search departements by code
This commit is contained in:
Paul Chavard 2020-01-23 20:43:19 +00:00 committed by GitHub
commit 35aceeb5e6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 17 additions and 1 deletions

View file

@ -1,5 +1,6 @@
import $ from 'jquery'; import $ from 'jquery';
import 'select2'; import 'select2';
import { isNumeric } from '@utils';
const { api_geo_url, api_adresse_url } = gon.autocomplete || {}; const { api_geo_url, api_adresse_url } = gon.autocomplete || {};
@ -80,10 +81,21 @@ const communesOptions = {
const etranger99 = { id: '99 - Étranger', text: '99 - Étranger' }; const etranger99 = { id: '99 - Étranger', text: '99 - Étranger' };
const departementsOptions = { const departementsOptions = {
...baseOptions, ...baseOptions,
minimumInputLength: 2, minimumInputLength: 1,
ajax: { ajax: {
...baseAjaxOptions, ...baseAjaxOptions,
url: `${api_geo_url}/departements`, url: `${api_geo_url}/departements`,
data({ term }) {
const data = { fields: 'nom,code' };
if (isNumeric(term)) {
data.code = term.trim().padStart(2, '0');
} else {
data.nom = term;
}
return data;
},
processResults(data) { processResults(data) {
return { return {
results: data results: data

View file

@ -78,6 +78,10 @@ export function to(promise) {
return promise.then(result => [result]).catch(error => [null, error]); return promise.then(result => [result]).catch(error => [null, error]);
} }
export function isNumeric(n) {
return !isNaN(parseFloat(n)) && isFinite(n);
}
function offset(element) { function offset(element) {
const rect = element.getBoundingClientRect(); const rect = element.getBoundingClientRect();
return { return {