From d0cd2e094daa8797b4c3d79e4875cda404d0825b Mon Sep 17 00:00:00 2001 From: mdlufy Date: Fri, 8 Nov 2024 15:34:33 +0300 Subject: [PATCH 1/5] fix(cdk): `MIN_YEAR` incorrect work with `new Date()` constructor --- projects/cdk/date-time/date-time.ts | 2 +- .../demo/src/modules/components/input-date/examples/1/index.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/projects/cdk/date-time/date-time.ts b/projects/cdk/date-time/date-time.ts index c9a5e85cc5ee..cb9ee143f11d 100644 --- a/projects/cdk/date-time/date-time.ts +++ b/projects/cdk/date-time/date-time.ts @@ -14,7 +14,7 @@ export const MIN_MONTH = 0; export const MAX_MONTH = 11; -export const MIN_YEAR = 0; +export const MIN_YEAR = 100; export const MAX_YEAR = 9999; diff --git a/projects/demo/src/modules/components/input-date/examples/1/index.ts b/projects/demo/src/modules/components/input-date/examples/1/index.ts index 1ede71a43589..f20a7ac61d6c 100644 --- a/projects/demo/src/modules/components/input-date/examples/1/index.ts +++ b/projects/demo/src/modules/components/input-date/examples/1/index.ts @@ -12,6 +12,6 @@ import {TuiDay} from '@taiga-ui/cdk'; }) export class TuiInputDateExample1 { readonly testForm = new FormGroup({ - testValue: new FormControl(new TuiDay(2017, 0, 15)), + testValue: new FormControl(new TuiDay(1870, 0, 1)), }); } From ee1be0ab135873748675642b1cf15a2d4bd392c7 Mon Sep 17 00:00:00 2001 From: mdlufy Date: Tue, 12 Nov 2024 11:16:33 +0300 Subject: [PATCH 2/5] fix(cdk): `toLocalNativeDate` correct work with years less 1900 --- projects/cdk/date-time/date-time.ts | 2 +- projects/cdk/date-time/day.ts | 7 ++++++- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/projects/cdk/date-time/date-time.ts b/projects/cdk/date-time/date-time.ts index cb9ee143f11d..c9a5e85cc5ee 100644 --- a/projects/cdk/date-time/date-time.ts +++ b/projects/cdk/date-time/date-time.ts @@ -14,7 +14,7 @@ export const MIN_MONTH = 0; export const MAX_MONTH = 11; -export const MIN_YEAR = 100; +export const MIN_YEAR = 0; export const MAX_YEAR = 9999; diff --git a/projects/cdk/date-time/day.ts b/projects/cdk/date-time/day.ts index 833c1ea5d86c..8f8271e98062 100644 --- a/projects/cdk/date-time/day.ts +++ b/projects/cdk/date-time/day.ts @@ -377,7 +377,12 @@ export class TuiDay extends TuiMonth { * Returns native {@link Date} based on local time zone */ override toLocalNativeDate(): Date { - return new Date(this.year, this.month, this.day); + const date = new Date(this.year, this.month, this.day); + + // needed for years less than 1900 + date.setFullYear(Number(this.year ?? '0')); + + return date; } /** From 4b6cfd59c4086c08bea9c78b29c5f8c0bf90fc67 Mon Sep 17 00:00:00 2001 From: mdlufy Date: Mon, 18 Nov 2024 18:03:28 +0300 Subject: [PATCH 3/5] chore(kit): `InputDate` update tests --- .../test/input-date.component.spec.ts | 31 ++++++++++++------- 1 file changed, 20 insertions(+), 11 deletions(-) diff --git a/projects/kit/components/input-date/test/input-date.component.spec.ts b/projects/kit/components/input-date/test/input-date.component.spec.ts index bd88d6e63621..6e43e17db7ac 100644 --- a/projects/kit/components/input-date/test/input-date.component.spec.ts +++ b/projects/kit/components/input-date/test/input-date.component.spec.ts @@ -47,7 +47,7 @@ describe('InputDate', () => { readOnly = false; - min = new TuiDay(1900, 0, 1); + min: TuiDay | null = new TuiDay(1500, 0, 1); labelOutside = false; @@ -125,6 +125,15 @@ describe('InputDate', () => { expect(inputPO.value).toBe('14.03.2017'); }); + it('correct shows value less than 01.01.1900', async () => { + testComponent.control.patchValue(new TuiDay(1000, 0, 1)); + fixture.detectChanges(); + + await fixture.whenStable(); + + expect(inputPO.value).toBe('01.01.1000'); + }); + describe('Keyboard input', () => { it('The passed date is inserted into the field', () => { inputPO.sendText('01.03.2017'); @@ -211,10 +220,10 @@ describe('InputDate', () => { const typedDay = testComponent.control.value; - expect(inputPO.value).toBe('1900.01.01'); - expect(typedDay.day).toBe(1); - expect(typedDay.month).toBe(0); - expect(typedDay.year).toBe(1900); + expect(inputPO.value).toBe('1223.08.21'); + expect(typedDay.day).toBe(21); + expect(typedDay.month).toBe(7); + expect(typedDay.year).toBe(1223); }); it('sets valid day if date selected via calendar', async () => { @@ -258,10 +267,10 @@ describe('InputDate', () => { const typedDay = testComponent.control.value; - expect(inputPO.value).toBe('01.01.1900'); - expect(typedDay.day).toBe(1); - expect(typedDay.month).toBe(0); - expect(typedDay.year).toBe(1900); + expect(inputPO.value).toBe('08.21.1223'); + expect(typedDay.day).toBe(21); + expect(typedDay.month).toBe(7); + expect(typedDay.year).toBe(1223); }); it('sets valid day if date selected via calendar', async () => { @@ -363,8 +372,8 @@ describe('InputDate', () => { it('transforms min day as output (if typed day is less than min day)', () => { inputPO.sendText('19.02.1861'); - expect(inputPO.value).toBe('01.01.1900'); - expect(testComponent.control.value).toEqual(new Date(1900, 0, 1)); + expect(inputPO.value).toBe('19.02.1861'); + expect(testComponent.control.value).toEqual(new Date(1861, 1, 19)); }); it('transforms value which was selected via calendar', async () => { From ed14441f5f4177d9df3c239f3690ddf6eafdd94c Mon Sep 17 00:00:00 2001 From: mdlufy Date: Tue, 19 Nov 2024 13:58:47 +0300 Subject: [PATCH 4/5] chore(demo): `InputDate` revert date --- .../demo/src/modules/components/input-date/examples/1/index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/projects/demo/src/modules/components/input-date/examples/1/index.ts b/projects/demo/src/modules/components/input-date/examples/1/index.ts index f20a7ac61d6c..1ede71a43589 100644 --- a/projects/demo/src/modules/components/input-date/examples/1/index.ts +++ b/projects/demo/src/modules/components/input-date/examples/1/index.ts @@ -12,6 +12,6 @@ import {TuiDay} from '@taiga-ui/cdk'; }) export class TuiInputDateExample1 { readonly testForm = new FormGroup({ - testValue: new FormControl(new TuiDay(1870, 0, 1)), + testValue: new FormControl(new TuiDay(2017, 0, 15)), }); } From 3154d570d784b79c0a881f4dfd049f1648f8e842 Mon Sep 17 00:00:00 2001 From: mdlufy Date: Wed, 20 Nov 2024 16:08:18 +0300 Subject: [PATCH 5/5] chore(kit): `InputDate` test update min checking --- .../test/input-date.component.spec.ts | 34 +++++++++++-------- 1 file changed, 20 insertions(+), 14 deletions(-) diff --git a/projects/kit/components/input-date/test/input-date.component.spec.ts b/projects/kit/components/input-date/test/input-date.component.spec.ts index 6e43e17db7ac..bd9f0579a143 100644 --- a/projects/kit/components/input-date/test/input-date.component.spec.ts +++ b/projects/kit/components/input-date/test/input-date.component.spec.ts @@ -215,15 +215,17 @@ describe('InputDate', () => { expect(typedDay.year).toBe(2021); }); - it('does not accept mm.dd.yyyy (and set min day if it is less min day)', () => { + it('does not accept mm.dd.yyyy (and set min day if it is less min day)', async () => { inputPO.sendText('12.23.2021'); + await fixture.whenStable(); + const typedDay = testComponent.control.value; - expect(inputPO.value).toBe('1223.08.21'); - expect(typedDay.day).toBe(21); - expect(typedDay.month).toBe(7); - expect(typedDay.year).toBe(1223); + expect(inputPO.value).toBe('1500.01.01'); + expect(typedDay.day).toBe(1); + expect(typedDay.month).toBe(0); + expect(typedDay.year).toBe(1500); }); it('sets valid day if date selected via calendar', async () => { @@ -262,15 +264,17 @@ describe('InputDate', () => { expect(typedDay.year).toBe(2021); }); - it('does not accept yyyy.mm.dd (and set min day if it is less min day)', () => { + it('does not accept yyyy.mm.dd (and set min day if it is less min day)', async () => { inputPO.sendText('2021.12.23'); + await fixture.whenStable(); + const typedDay = testComponent.control.value; - expect(inputPO.value).toBe('08.21.1223'); - expect(typedDay.day).toBe(21); - expect(typedDay.month).toBe(7); - expect(typedDay.year).toBe(1223); + expect(inputPO.value).toBe('01.01.1500'); + expect(typedDay.day).toBe(1); + expect(typedDay.month).toBe(0); + expect(typedDay.year).toBe(1500); }); it('sets valid day if date selected via calendar', async () => { @@ -369,11 +373,13 @@ describe('InputDate', () => { expect(testComponent.control.value).toEqual(new Date(1905, 0, 9)); }); - it('transforms min day as output (if typed day is less than min day)', () => { - inputPO.sendText('19.02.1861'); + it('transforms min day as output (if typed day is less than min day)', async () => { + inputPO.sendText('19.02.1300'); + + await fixture.whenStable(); - expect(inputPO.value).toBe('19.02.1861'); - expect(testComponent.control.value).toEqual(new Date(1861, 1, 19)); + expect(inputPO.value).toBe('01.01.1500'); + expect(testComponent.control.value).toEqual(new Date(1500, 0, 1)); }); it('transforms value which was selected via calendar', async () => {