Skip to content

Commit

Permalink
JS: Improve date picker separator completion
Browse files Browse the repository at this point in the history
The previous version was relying on `UIEvent.which` to identify the key
being pressed, which is deprecated.
https://developer.mozilla.org/en-US/docs/Web/API/UIEvent/which

Also, typing out `01/` would result in `01//`, the first slash being
inserted automatically and the second `/` being added when the user
hits the `/` key.
  • Loading branch information
francoisfreitag committed Jan 31, 2025
1 parent a853758 commit e6a0d9e
Showing 1 changed file with 11 additions and 7 deletions.
18 changes: 11 additions & 7 deletions itou/static/js/duet_date_picker_widget.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,19 @@ const initDuetDatePicker = function () {
}

// Automatically insert slashes '/' in date fields.
pickerInstance.addEventListener("keyup", event => {
// Do nothing when backspace was pressed.
if (event.which !== 8) {
const numChars = event.target.value.length
if (numChars === 2 || numChars === 5) {
event.target.value = `${event.target.value}/`
pickerInstance.addEventListener("input", event => {
if (event.inputType === "insertText") {
// Remove typed '/' after the automatically inserted '/'.
// When user inputs "01/", we don’t want the input value to be "01//".
if (event.target.value.slice(-2) === "//") {
event.target.value = event.target.value.slice(0, -1);
}

if ([2, 5].includes(event.target.value.length)) {
event.target.value = `${event.target.value}/`;
}
}
})
});

pickerInstance.localization = {
buttonLabel: "Choisir une date",
Expand Down

0 comments on commit e6a0d9e

Please sign in to comment.