Skip to content

Commit

Permalink
fix(kit): InputDate hold initial value with min/max defined (#9623)
Browse files Browse the repository at this point in the history
  • Loading branch information
mdlufy authored Nov 5, 2024
1 parent f47554a commit 18a40c2
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 5 deletions.
2 changes: 1 addition & 1 deletion projects/demo-cypress/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@
"typeRoots": ["../../node_modules/@types", "../../node_modules/cypress/types", "../../scripts/types"],
"types": ["cypress", "node", "cypress-real-events", "@testing-library/cypress", "ng-dev-mode"]
},
"include": ["./cypress/**/*.ts"],
"include": ["**/*.ts"],
"exclude": []
}
34 changes: 33 additions & 1 deletion projects/demo-playwright/tests/kit/input-date/input-date.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,17 +43,49 @@ test.describe('InputDate', () => {
await expect(inputDate.textfield).toHaveScreenshot('01-input-date.png');
});

test('Click `Until today`', async ({page}) => {
test('Click `Until today`, calendar not switched to large date', async ({
page,
}) => {
await tuiGoto(page, 'components/input-date/API?items$=1');

await inputDate.textfield.click();
await calendar.itemButton.click();

await inputDate.textfield.click();

await expect(inputDate.textfield).toHaveValue('Until today');
await expect(inputDate.calendar).toHaveScreenshot(
'02-input-date-calendar.png',
);
});

test('Press backspace to remove `Until today`, textfield is empty', async ({
page,
}) => {
await tuiGoto(page, 'components/input-date/API?items$=1');

await inputDate.textfield.click();
await calendar.itemButton.click();

await inputDate.textfield.focus();
await inputDate.textfield.press('Backspace');

await expect(inputDate.textfield).toHaveValue('');
await expect(inputDate.textfield).toHaveScreenshot(
'03-input-date-textfield-empty.png',
);
});

test('Enter item date, it converts to item date name', async ({page}) => {
await tuiGoto(page, 'components/input-date/API?items$=1');

await inputDate.textfield.focus();
await inputDate.textfield.fill('31.12.9998');

await expect(inputDate.textfield).toHaveValue('Until today');
await expect(inputDate.textfield).toHaveScreenshot(
'04-input-date-item-name.png',
);
});
});
});
29 changes: 26 additions & 3 deletions projects/kit/components/input-date/input-date.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ import {
DATE_FILLER_LENGTH,
TUI_DATE_FORMAT,
TUI_DATE_SEPARATOR,
TUI_FIRST_DAY,
TUI_IS_MOBILE,
TUI_LAST_DAY,
TUI_LAST_DISPLAYED_DAY,
TuiActiveZoneDirective,
tuiAsControl,
Expand Down Expand Up @@ -148,10 +150,28 @@ export class TuiInputDateComponent
}

get computedMin(): TuiDay {
/**
* TODO: we can delete this workaround in v4.0
* after solving this issue:
* https://github.com/taiga-family/maskito/issues/604
*/
if (this.value && this.control?.pristine) {
return TUI_FIRST_DAY;
}

return this.min ?? this.options.min;
}

get computedMax(): TuiDay {
/**
* TODO: we can delete this workaround in v4.0
* after solving this issue:
* https://github.com/taiga-family/maskito/issues/604
*/
if (this.value && this.control?.pristine) {
return TUI_LAST_DAY;
}

return this.max ?? this.options.max;
}

Expand Down Expand Up @@ -222,8 +242,7 @@ export class TuiInputDateComponent
* after solving this issue:
* https://github.com/taiga-family/maskito/issues/604
*/
const nativeValueIsNotSynced =
this.textfield?.nativeFocusableElement?.value !== this.computedValue;
const nativeValueIsNotSynced = this.nativeValue !== this.computedValue;

return this.activeItem || nativeValueIsNotSynced
? MASKITO_DEFAULT_OPTIONS
Expand Down Expand Up @@ -276,8 +295,12 @@ export class TuiInputDateComponent
this.onOpenChange(true);
}

if (this.activeItem) {
this.nativeValue = '';
}

this.value =
value.length !== DATE_FILLER_LENGTH
value.length !== DATE_FILLER_LENGTH || this.activeItem
? null
: TuiDay.normalizeParse(value, this.dateFormat);
}
Expand Down

0 comments on commit 18a40c2

Please sign in to comment.