Skip to content

Commit

Permalink
fix(slider): don't change value when slider is display:none; #INFR-10…
Browse files Browse the repository at this point in the history
…717 @wangkai (#2937)

* fix(slider): don't change value when slider is display:none; #INFR-10717

* test(slider): add test
  • Loading branch information
xujing0521 committed Dec 5, 2023
1 parent dd32e06 commit ff249f1
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
3 changes: 3 additions & 0 deletions src/slider/slider.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,9 @@ export class ThySliderComponent
private mousePositionToAdaptiveValue(position: number): number {
const sliderStartPosition = this.getSliderPagePosition();
const sliderLength = this.getRailLength();
if (!sliderLength) {
return this.value;
}
const ratio = this.convertPointerPositionToRatio(position, sliderStartPosition, sliderLength);
const value = this.ratioToValue(ratio);
return parseFloat(value.toFixed(this.getDecimals(this.thyStep)));
Expand Down
20 changes: 18 additions & 2 deletions src/slider/slider.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { FormsModule } from '@angular/forms';
import { By } from '@angular/platform-browser';
import { ThySliderModule } from './slider.module';
import { dispatchMouseEvent } from 'ngx-tethys/testing';
import { Component, DebugElement } from '@angular/core';
import { Component, DebugElement, ViewEncapsulation } from '@angular/core';
import { ComponentFixture, TestBed, fakeAsync } from '@angular/core/testing';

@Component({
Expand All @@ -21,9 +21,11 @@ import { ComponentFixture, TestBed, fakeAsync } from '@angular/core/testing';
(thyAfterChange)="afterChange($event)"></thy-slider>
</div>
`,
encapsulation: ViewEncapsulation.None,
styles: [
`
.test-slider-container {
.thy-slider-rail {
display: inline-block;
width: 500px;
height: 200px;
}
Expand Down Expand Up @@ -115,6 +117,20 @@ describe('ThyTestSliderComponent', () => {
}).toThrowError('(max - min) must be divisible by step.');
}));

it('should not change value when slider is hidden', fakeAsync(() => {
fixture.detectChanges();
const sliderElement = getSliderElement();
const pointerElement = getSliderPointerElement();
const pointerElementRect = pointerElement.getBoundingClientRect();
dispatchMouseEvent(pointerElement, 'mousedown', pointerElementRect.left + 10);
const value = fixtureInstance.value;

sliderElement.style.display = 'none';
fixture.detectChanges();
dispatchMouseEvent(pointerElement, 'mousemove', pointerElementRect.left + 50, pointerElementRect.height);
expect(fixtureInstance.value).toBe(value);
}));

it('min/max value will be right', fakeAsync(() => {
const minValue = 50;
const maxValue = 200;
Expand Down

0 comments on commit ff249f1

Please sign in to comment.