From 0e5129a4fd1e8931157222bc3e950acba7849539 Mon Sep 17 00:00:00 2001 From: Vladimir Potekhin Date: Mon, 9 Sep 2024 10:40:50 +0300 Subject: [PATCH] fix(kit): `InputTime` fix selection of the nearest value from items --- .../components/input-time/input-time.component.ts | 13 ++++++------- .../input-time/test/input-time.component.spec.ts | 12 ++++++++++-- 2 files changed, 16 insertions(+), 9 deletions(-) diff --git a/projects/kit/components/input-time/input-time.component.ts b/projects/kit/components/input-time/input-time.component.ts index 0b25561be901..e0767ea53b04 100644 --- a/projects/kit/components/input-time/input-time.component.ts +++ b/projects/kit/components/input-time/input-time.component.ts @@ -316,13 +316,12 @@ export class TuiInputTimeComponent } private findNearestTimeFromItems(value: TuiTime): TuiTime | null { - return this.items.reduce( - (previous, current) => - Math.abs(current.valueOf() - value.valueOf()) < - Math.abs(previous.valueOf() - value.valueOf()) - ? current - : previous, - new TuiTime(0, 0), + // eslint-disable-next-line no-restricted-syntax + return this.items.reduce((previous, current) => + Math.abs(current.valueOf() - value.valueOf()) < + Math.abs(previous.valueOf() - value.valueOf()) + ? current + : previous, ); } diff --git a/projects/kit/components/input-time/test/input-time.component.spec.ts b/projects/kit/components/input-time/test/input-time.component.spec.ts index 62a861d80532..74798145b9fa 100644 --- a/projects/kit/components/input-time/test/input-time.component.spec.ts +++ b/projects/kit/components/input-time/test/input-time.component.spec.ts @@ -16,7 +16,6 @@ import {TUI_TIME_VALUE_TRANSFORMER} from '@taiga-ui/kit/tokens'; import {tuiCreateKeyboardEvent, TuiNativeInputPO, TuiPageObject} from '@taiga-ui/testing'; const TIMES = [ - new TuiTime(0, 0), new TuiTime(0, 30), new TuiTime(1, 0), new TuiTime(1, 30), @@ -266,7 +265,7 @@ describe('InputTime', () => { pageObject.getByAutomationId('tui-input-time__item')!.nativeElement.click(); expect(testComponent.control.value.toString().trim()).toBe( - TIMES[6].toString(), + TIMES[5].toString(), ); }); @@ -303,6 +302,15 @@ describe('InputTime', () => { expect(testComponent.control.value.toString().trim()).toBe('01:30'); }); + + it('with strict = true, the entered value is rounded to the nearest in items (00:00)', () => { + testComponent.strict = true; + fixture.detectChanges(); + inputPO.sendText('00:00'); + fixture.detectChanges(); + + expect(testComponent.control.value.toString().trim()).toBe('00:30'); + }); }); });