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): InputTime fix selection of the nearest value from items #8903

Merged
merged 1 commit into from
Sep 10, 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
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
Loading