From 96a772852207ab1540db9e28a0d900bc23e45d64 Mon Sep 17 00:00:00 2001 From: Vladimir Potekhin <46284632+vladimirpotekhin@users.noreply.github.com> Date: Tue, 10 Sep 2024 13:17:04 +0300 Subject: [PATCH] fix(kit): `InputTime` fix selection of the nearest value from items (#8903) --- .../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'); + }); }); });