Skip to content

Commit

Permalink
fix(kit): InputTime fix selection of the nearest value from items (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
vladimirpotekhin authored Sep 10, 2024
1 parent ee0b4bf commit 96a7728
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 9 deletions.
13 changes: 6 additions & 7 deletions projects/kit/components/input-time/input-time.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down Expand Up @@ -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(),
);
});

Expand Down Expand Up @@ -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');
});
});
});

Expand Down

0 comments on commit 96a7728

Please sign in to comment.