Emit the input event when changing departure date

It is necessary to detect the change using AlpineJS, for instance.
This commit is contained in:
jordi fita mas 2024-02-03 03:22:25 +01:00
parent cabab93aa9
commit 2a5b6df8cf
1 changed files with 7 additions and 2 deletions

View File

@ -11,6 +11,11 @@
return; return;
} }
function updateDepartureDate(date) {
departureDateField.value = date;
departureDateField.dispatchEvent(new Event('input', {bubbles: true}));
}
const minNights = Math.max(1, parseInt(departureDateField.dataset.minNights, 10) || 0); const minNights = Math.max(1, parseInt(departureDateField.dataset.minNights, 10) || 0);
arrivalDate.setUTCDate(arrivalDate.getUTCDate() + minNights); arrivalDate.setUTCDate(arrivalDate.getUTCDate() + minNights);
const minDate = formatDate(arrivalDate); const minDate = formatDate(arrivalDate);
@ -19,7 +24,7 @@
const departureDate = new Date(departureDateField.value); const departureDate = new Date(departureDateField.value);
const validDepartureDate = !isNaN(departureDate.getTime()) const validDepartureDate = !isNaN(departureDate.getTime())
if (!validDepartureDate || departureDate < arrivalDate) { if (!validDepartureDate || departureDate < arrivalDate) {
departureDateField.value = minDate; updateDepartureDate(minDate);
} }
const maxNights = parseInt(departureDateField.dataset.maxNights, 10) || 0; const maxNights = parseInt(departureDateField.dataset.maxNights, 10) || 0;
@ -28,7 +33,7 @@
const maxDate = formatDate(arrivalDate); const maxDate = formatDate(arrivalDate);
departureDateField.setAttribute('max', maxDate); departureDateField.setAttribute('max', maxDate);
if (validDepartureDate && departureDate >= arrivalDate) { if (validDepartureDate && departureDate >= arrivalDate) {
departureDateField.value = maxDate; updateDepartureDate(maxDate);
} }
} }
} }