Skip to content

Commit 921d3ad

Browse files
make most changes (todo: update date validator so it can tell if its ISO format)
1 parent c2fde50 commit 921d3ad

4 files changed

Lines changed: 64 additions & 26 deletions

File tree

packages/web/src/components/gcds-date-input/gcds-date-input.tsx

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -557,6 +557,18 @@ export class GcdsDateInput {
557557
dayValue = dayValue?.replace(/[eE-]/g, '');
558558

559559
if (format === 'iso') {
560+
// Logic to make sure the month input is registered correctly
561+
if (monthValue && monthValue.length === 1 && monthValue != '0') {
562+
monthValue = '0' + monthValue;
563+
this.monthValue = monthValue;
564+
} else if (
565+
monthValue &&
566+
monthValue.length == 3 &&
567+
monthValue[0] === '0'
568+
) {
569+
monthValue = monthValue.substring(1);
570+
this.monthValue = monthValue;
571+
}
560572
monthValue = monthValue?.replace(/[eE-]/g, '');
561573
}
562574

packages/web/src/components/gcds-date-input/stories/gcds-date-input.stories.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ export default {
2222
},
2323
format: {
2424
control: { type: 'select' },
25-
options: ['full', 'compact', 'yyyy-MM-dd'],
25+
options: ['full', 'compact', 'iso'],
2626
table: {
2727
type: { summary: 'string' },
2828
defaultValue: { summary: '-' },

packages/web/src/components/gcds-date-input/test/gcds-date-input.e2e.ts

Lines changed: 45 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -62,30 +62,37 @@ test.describe('gcds-date-input', () => {
6262
expect(value).toEqual('1234-03');
6363
});
6464

65-
test('Format: iso - value', async ({ page }) => {
66-
const element = page.locator('gcds-date-input');
67-
68-
// Wait for element to attach and become visible, allowing up to 10s
69-
await element.waitFor({ state: 'attached' });
70-
await element.waitFor({ state: 'visible' });
71-
await element.waitFor({ timeout: 10000 });
72-
73-
await element.evaluate(
74-
(el: HTMLGcdsDateInputElement) => (el.format = 'iso'),
75-
);
76-
77-
await page.waitForChanges();
78-
79-
await element.locator('input[name="year"]').fill('1234');
80-
await element.locator('input[name="month"]').fill('03');
81-
await element.locator('input[name="day"]').fill('11');
82-
83-
const value = await element.evaluate(
84-
(el: HTMLGcdsDateInputElement) => el.value,
85-
);
86-
87-
expect(value).toEqual('1234-03-11');
88-
});
65+
for (const { year, month, day, expected } of [
66+
{ year: '2023', month: '3', day: '1', expected: '2023-03-01' },
67+
{ year: '2021', month: '05', day: '2', expected: '2021-05-02' },
68+
{ year: '1600', month: '2', day: '11', expected: '1600-02-11' },
69+
{ year: '1234', month: '03', day: '11', expected: '1234-03-11' },
70+
]) {
71+
test(`Format: iso - value - ${year}-${month}-${day}`, async ({ page }) => {
72+
const element = page.locator('gcds-date-input');
73+
74+
// Wait for element to attach and become visible, allowing up to 10s
75+
await element.waitFor({ state: 'attached' });
76+
await element.waitFor({ state: 'visible' });
77+
await element.waitFor({ timeout: 10000 });
78+
79+
await element.evaluate(
80+
(el: HTMLGcdsDateInputElement) => (el.format = 'iso'),
81+
);
82+
83+
await page.waitForChanges();
84+
85+
await element.locator('input[name="year"]').fill(year);
86+
await element.locator('input[name="month"]').fill(month);
87+
await element.locator('input[name="day"]').fill(day);
88+
89+
const value = await element.evaluate(
90+
(el: HTMLGcdsDateInputElement) => el.value,
91+
);
92+
93+
expect(value).toEqual(expected);
94+
});
95+
}
8996
/**
9097
* Invalid value
9198
*/
@@ -471,7 +478,7 @@ test.describe('gcds-date-input', () => {
471478
el => (el as HTMLGcdsDateInputElement).errorMessage,
472479
);
473480

474-
expect(errorMessage).toEqual(dateInputErrorMessage.en.invalidday);
481+
expect(errorMessage).toEqual(dateInputErrorMessage.en.missingmonthinput);
475482
});
476483
test('Validation - Custom validation', async ({ page }) => {
477484
const element = page.locator('gcds-date-input');
@@ -770,6 +777,19 @@ test.describe('gcds-date-input a11y tests', () => {
770777
* ISO format
771778
*/
772779
test('ISO format', async ({ page }) => {
780+
const element = page.locator('gcds-date-input');
781+
782+
// Wait for element to attach and become visible, allowing up to 10s
783+
await element.waitFor({ state: 'attached' });
784+
await element.waitFor({ state: 'visible' });
785+
await element.waitFor({ timeout: 10000 });
786+
787+
await element.evaluate(
788+
(el: HTMLGcdsDateInputElement) => (el.format = 'iso'),
789+
);
790+
791+
await page.waitForChanges();
792+
773793
const results = await new AxeBuilder({ page })
774794
.include('gcds-date-input')
775795
.analyze();

packages/web/src/validators/input-validators/input-validators.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,23 +61,29 @@ export const requiredSelectField: Validator<string> = {
6161
export const dateInputErrorMessage = {
6262
en: {
6363
all: 'Enter the date.',
64+
missingmonthinput: 'Enter the month.',
6465
missingmonth: 'Select the month.',
6566
missingyear: 'Enter the year.',
6667
missingday: 'Enter the day.',
6768
missingmonthday: 'Select the month and enter the day.',
6869
missingmonthyear: 'Select the month and enter the year.',
70+
missingmonthinputday: 'Enter the month and day.',
71+
missingmonthinputyear: 'Enter the year and month.',
6972
missingdayyear: 'Enter the day and year.',
7073
invalidyearlength: 'Year must be 4 digits.',
7174
invalidyear: 'Enter a valid year.',
7275
invalidday: 'Enter a valid day.',
7376
},
7477
fr: {
7578
all: 'Saisissez la date.',
79+
missingmonthinput: 'Saisissez le mois.',
7680
missingmonth: 'Sélectionnez un mois.',
7781
missingyear: "Saisissez l'année.",
7882
missingday: 'Saisissez le jour.',
7983
missingmonthday: 'Saisissez le jour et sélectionnez un mois.',
8084
missingmonthyear: "Sélectionnez un mois et saisissez l'année.",
85+
missingmonthinputday: 'Saisissez le mois et le jour.',
86+
missingmonthinputyear: "Saisissez l'année et le mois.",
8187
missingdayyear: "Saisissez le jour et l'année.",
8288
invalidyearlength: "L'année doit inclure 4 chiffres.",
8389
invalidyear: 'Entrez une année valide.',

0 commit comments

Comments
 (0)