Skip to content

Commit

Permalink
fix(kit): InputRange should not validate quantum if textfield is …
Browse files Browse the repository at this point in the history
…focused
  • Loading branch information
nsbarsukov committed Sep 15, 2023
1 parent 8cc1e76 commit 11be4da
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 28 deletions.
38 changes: 14 additions & 24 deletions projects/kit/components/input-range/input-range.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,8 @@ export class TuiInputRangeComponent
@Input()
pluralize: Record<string, string> | null = null;

leftTextfieldValue = this.safeCurrentValue[0];
rightTextfieldValue = this.safeCurrentValue[1];
lastActiveSide: 'left' | 'right' = 'left';

constructor(
Expand Down Expand Up @@ -178,18 +180,12 @@ export class TuiInputRangeComponent
this.updateFocused(active);
}

onTextInputFocused(focused: boolean, right: boolean): void {
onTextInputFocused(focused: boolean): void {
if (focused) {
return;
}

const [leftTextInputRef, rightTextInputRef] = this.inputNumberRefs;
const inputRef = right ? rightTextInputRef : leftTextInputRef;
const valueIndex = right ? 1 : 0;

if (!inputRef.nativeValue || inputRef.value !== this.value[valueIndex]) {
this.updateTextInputValue(this.safeCurrentValue[valueIndex], right);
}
this.updateTextfieldValues(this.value);
}

changeByStep(
Expand All @@ -211,10 +207,7 @@ export class TuiInputRangeComponent

if (leftValueChanged || rightValueChanged) {
this.safelyUpdateValue(newValue);
this.updateTextInputValue(
newValue[rightValueChanged ? 1 : 0],
rightValueChanged,
);
this.updateTextfieldValues(this.value);
}
}

Expand All @@ -228,13 +221,7 @@ export class TuiInputRangeComponent

onRangeValue(value: [number, number]): void {
this.safelyUpdateValue(value);

const rightValueChanged = this.lastActiveSide === 'right';

this.updateTextInputValue(
this.value[rightValueChanged ? 1 : 0],
rightValueChanged,
);
this.updateTextfieldValues(this.value);
}

focusToTextInput(): void {
Expand All @@ -252,6 +239,11 @@ export class TuiInputRangeComponent
this.lastActiveSide = activeThumb;
}

override writeValue(value: [number, number]): void {
super.writeValue(value);
this.updateTextfieldValues(this.value);
}

protected getFallbackValue(): [number, number] {
return [0, 0];
}
Expand Down Expand Up @@ -279,10 +271,8 @@ export class TuiInputRangeComponent
return tuiClamp(roundedValue, this.min, this.max);
}

private updateTextInputValue(value: number, right: boolean): void {
const [leftInputRef, rightInputRef] = this.inputNumberRefs;
const textInputRef = right ? rightInputRef : leftInputRef;

textInputRef?.writeValue(value);
private updateTextfieldValues([leftValue, rightValue]: [number, number]): void {
this.leftTextfieldValue = leftValue;
this.rightTextfieldValue = rightValue;
}
}
8 changes: 4 additions & 4 deletions projects/kit/components/input-range/input-range.template.html
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@
[disabled]="computedDisabled"
[max]="value[1]"
[min]="min"
[ngModel]="value[0]"
[precision]="precision"
[readOnly]="readOnly"
[tuiTextfieldPostfix]="pluralize && !showLeftValueContent ? (value[0] | i18nPlural: pluralize) : ''"
(focusedChange)="onTextInputFocused($event, false)"
[(ngModel)]="leftTextfieldValue"
(focusedChange)="onTextInputFocused($event)"
(keydown.arrowDown)="changeByStep($event, [-1, 0])"
(keydown.arrowUp)="changeByStep($event, [1, 0])"
(ngModelChange)="onInputLeft($event)"
Expand All @@ -45,11 +45,11 @@
[disabled]="computedDisabled"
[max]="max"
[min]="value[0]"
[ngModel]="value[1]"
[precision]="precision"
[readOnly]="readOnly"
[tuiTextfieldPostfix]="pluralize && !showRightValueContent ? (value[1] | i18nPlural: pluralize) : ''"
(focusedChange)="onTextInputFocused($event, true)"
[(ngModel)]="rightTextfieldValue"
(focusedChange)="onTextInputFocused($event)"
(keydown.arrowDown)="changeByStep($event, [0, -1])"
(keydown.arrowUp)="changeByStep($event, [0, 1])"
(ngModelChange)="onInputRight($event)"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,18 @@ describe(`InputRange`, () => {

expect(inputPORight.value).toBe(`5 лет`);
});

it(`does not rounds to nearest multiple of [quantum] until text field losses focus`, () => {
inputPORight.sendText(`8`);

expect(inputPORight.value).toBe(`8 лет`);
expect(testComponent.control.value[1]).toBe(10);

inputPORight.blur();

expect(inputPORight.value).toBe(`10 лет`);
expect(testComponent.control.value[1]).toBe(10);
});
});

describe(`Deleting Values`, () => {
Expand Down Expand Up @@ -197,6 +209,14 @@ describe(`InputRange`, () => {
.replace(CHAR_HYPHEN, CHAR_MINUS)} лет`,
);
});

it(`programmatic FormControl updates should also update textfield value`, async () => {
testComponent.control.patchValue([5, 10]);
await fixture.whenStable();

expect(inputPOLeft.value).toBe(`5 лет`);
expect(inputPORight.value).toBe(`10 лет`);
});
});

describe(`Format`, () => {
Expand Down

0 comments on commit 11be4da

Please sign in to comment.