Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(kit): InputDate incorrect works with date less than 1900 #9719

Merged
merged 5 commits into from
Nov 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion projects/cdk/date-time/day.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ describe('InputDate', () => {

readOnly = false;

min = new TuiDay(1900, 0, 1);
min: TuiDay | null = new TuiDay(1500, 0, 1);

labelOutside = false;

Expand Down Expand Up @@ -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');
Expand Down Expand Up @@ -206,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('1900.01.01');
expect(inputPO.value).toBe('1500.01.01');
expect(typedDay.day).toBe(1);
expect(typedDay.month).toBe(0);
expect(typedDay.year).toBe(1900);
expect(typedDay.year).toBe(1500);
});

it('sets valid day if date selected via calendar', async () => {
Expand Down Expand Up @@ -253,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('01.01.1900');
expect(inputPO.value).toBe('01.01.1500');
expect(typedDay.day).toBe(1);
expect(typedDay.month).toBe(0);
expect(typedDay.year).toBe(1900);
expect(typedDay.year).toBe(1500);
});

it('sets valid day if date selected via calendar', async () => {
Expand Down Expand Up @@ -360,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('01.01.1900');
expect(testComponent.control.value).toEqual(new Date(1900, 0, 1));
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 () => {
Expand Down
Loading