Merge pull request #9760 from tchak/date-field-format

ETQ utilisateur, je voudrais que le format affiché pour les champs de date corresponde au format du champ de saisie
This commit is contained in:
Paul Chavard 2023-12-01 21:11:56 +00:00 committed by GitHub
commit 55cdcc8eb9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 51 additions and 2 deletions

View file

@ -18,7 +18,7 @@
= t('.check_content_rebased') = t('.check_content_rebased')
- if hint? - if hint?
%span.fr-hint-text= hint %span.fr-hint-text{ data: { controller: 'date-input-hint' } }= hint
- if @champ.description.present? - if @champ.description.present?
%span.fr-hint-text{ id: @champ.describedby_id }= render SimpleFormatComponent.new(@champ.description, allow_a: true) %span.fr-hint-text{ id: @champ.describedby_id }= render SimpleFormatComponent.new(@champ.description, allow_a: true)

View file

@ -3,4 +3,4 @@
aria: { describedby: @champ.describedby_id }, aria: { describedby: @champ.describedby_id },
value: @champ.value, value: @champ.value,
required: @champ.required?, required: @champ.required?,
placeholder: 'aaaa-mm-jj', class: "width-33-desktop") class: "width-33-desktop")

View file

@ -0,0 +1,49 @@
import { ApplicationController } from './application_controller';
const DATE_PLACEHOLDER_REGEXP = /[jJmMdD]{2}\/[jJmMdD]{2}\/[aAyY]{4}/;
const DATE_EXAMPLE_REGEXP = /\d{1,2}\/\d{1,2}\/\d{4}/;
const PARTS = {
fr: {
'15': 'JJ',
'10': 'MM',
'2022': 'AAAA'
},
en: {
'15': 'dd',
'10': 'mm',
'2022': 'yyyy'
}
};
export class DateInputHintController extends ApplicationController {
connect() {
this.fixDateFormat(this.element);
}
private fixDateFormat(input: Element) {
const text = input.textContent ?? '';
const match = text.match(DATE_PLACEHOLDER_REGEXP);
if (match) {
const [placeholder, example] = this.translatePlaceholder();
// This component behaviour can be had to test and debug. We keep a (debug) log here to help.
console.debug(`Replace ${match[0]} with ${placeholder} and ${example}`);
input.textContent = text
.replace(DATE_PLACEHOLDER_REGEXP, placeholder)
.replace(DATE_EXAMPLE_REGEXP, example);
}
}
private translatePlaceholder() {
const locale = document.documentElement.lang as 'fr' | 'en';
const parts = PARTS[locale];
const example = new Date(2022, 9, 15).toLocaleDateString();
return [
Object.entries(parts).reduce(
(text, [part, str]) => text.replace(part, str),
example
),
example
];
}
}