Skip to content

Commit 4f676d4

Browse files
committedJan 14, 2025·
fix(material/slider): active vs inactive tick mark count (#30319)
(cherry picked from commit 5f238ab)
1 parent 9490f1a commit 4f676d4

File tree

2 files changed

+85
-4
lines changed

2 files changed

+85
-4
lines changed
 

‎src/material/slider/slider.spec.ts

+83-2
Original file line numberDiff line numberDiff line change
@@ -1549,6 +1549,7 @@ describe('MatSlider', () => {
15491549
let fixture: ComponentFixture<SliderWithTickMarks>;
15501550
let slider: MatSlider;
15511551
let sliderEl: HTMLElement;
1552+
let input: MatSliderThumb;
15521553

15531554
function getTickMarkEls() {
15541555
const activeClass = '.mdc-slider__tick-mark--active';
@@ -1565,6 +1566,7 @@ describe('MatSlider', () => {
15651566
const sliderDebugElement = fixture.debugElement.query(By.directive(MatSlider));
15661567
slider = sliderDebugElement.componentInstance;
15671568
sliderEl = sliderDebugElement.nativeElement;
1569+
input = slider._getInput(_MatThumb.END) as MatSliderThumb;
15681570
}));
15691571

15701572
it('should have tick marks', () => {
@@ -1604,15 +1606,80 @@ describe('MatSlider', () => {
16041606
}
16051607
});
16061608

1607-
// TODO(wagnermaciel): Add this test once this is fixed.
1608-
// it('should render the correct number of active & inactive ticks', () => {});
1609+
it('should render the correct number of active & inactive ticks', () => {
1610+
let tickEls = getTickMarkEls();
1611+
expect(tickEls.active.length).toBe(1);
1612+
expect(tickEls.inactive.length).toBe(100);
1613+
1614+
input.value = 50;
1615+
tickEls = getTickMarkEls();
1616+
expect(tickEls.active.length).toBe(51);
1617+
expect(tickEls.inactive.length).toBe(50);
1618+
1619+
input.value = 100;
1620+
tickEls = getTickMarkEls();
1621+
expect(tickEls.active.length).toBe(101);
1622+
expect(tickEls.inactive.length).toBe(0);
1623+
});
16091624

16101625
// TODO(wagnermaciel): Add this test once this is fixed.
16111626
// it('should position the tick marks correctly with a misaligned step', () => {});
16121627

16131628
// TODO(wagnermaciel): Add this test once this is fixed.
16141629
// it('should position the tick marks correctly with a misaligned step (rtl)', () => {});
16151630
});
1631+
1632+
describe('range slider with tick marks', () => {
1633+
let fixture: ComponentFixture<RangeSliderWithTickMarks>;
1634+
let slider: MatSlider;
1635+
let sliderEl: HTMLElement;
1636+
let endInput: MatSliderRangeThumb;
1637+
let startInput: MatSliderRangeThumb;
1638+
1639+
function getTickMarkEls() {
1640+
const activeClass = '.mdc-slider__tick-mark--active';
1641+
const inactiveClass = '.mdc-slider__tick-mark--inactive';
1642+
const active = sliderEl.querySelectorAll(activeClass);
1643+
const inactive = sliderEl.querySelectorAll(inactiveClass);
1644+
const ticks = sliderEl.querySelectorAll(`${activeClass},${inactiveClass}`);
1645+
return {ticks, active, inactive};
1646+
}
1647+
1648+
beforeEach(waitForAsync(() => {
1649+
fixture = createComponent(RangeSliderWithTickMarks);
1650+
fixture.detectChanges();
1651+
const sliderDebugElement = fixture.debugElement.query(By.directive(MatSlider));
1652+
slider = sliderDebugElement.componentInstance;
1653+
sliderEl = sliderDebugElement.nativeElement;
1654+
endInput = slider._getInput(_MatThumb.END) as MatSliderRangeThumb;
1655+
startInput = slider._getInput(_MatThumb.START) as MatSliderRangeThumb;
1656+
}));
1657+
1658+
it('should render the correct number of active & inactive ticks', () => {
1659+
startInput.value = 0;
1660+
endInput.value = 100;
1661+
1662+
let tickEls = getTickMarkEls();
1663+
expect(tickEls.active.length).toBe(101);
1664+
expect(tickEls.inactive.length).toBe(0);
1665+
1666+
startInput.value = 25;
1667+
tickEls = getTickMarkEls();
1668+
expect(tickEls.active.length).toBe(76);
1669+
expect(tickEls.inactive.length).toBe(25);
1670+
1671+
endInput.value = 75;
1672+
tickEls = getTickMarkEls();
1673+
expect(tickEls.active.length).toBe(51);
1674+
expect(tickEls.inactive.length).toBe(50);
1675+
1676+
startInput.value = 50;
1677+
endInput.value = 50;
1678+
tickEls = getTickMarkEls();
1679+
expect(tickEls.active.length).toBe(1);
1680+
expect(tickEls.inactive.length).toBe(100);
1681+
});
1682+
});
16161683
});
16171684

16181685
const SLIDER_STYLES = ['.mat-mdc-slider { width: 300px; }'];
@@ -1917,6 +1984,20 @@ class SliderWithTickMarks {
19171984
@ViewChild(MatSlider) slider: MatSlider;
19181985
}
19191986

1987+
@Component({
1988+
template: `
1989+
<mat-slider [showTickMarks]="true">
1990+
<input matSliderStartThumb>
1991+
<input matSliderEndThumb>
1992+
</mat-slider>
1993+
`,
1994+
styles: SLIDER_STYLES,
1995+
standalone: false,
1996+
})
1997+
class RangeSliderWithTickMarks {
1998+
@ViewChild(MatSlider) slider: MatSlider;
1999+
}
2000+
19202001
/** Clicks on the MatSlider at the coordinates corresponding to the given value. */
19212002
function setValueByClick(
19222003
slider: MatSlider,

‎src/material/slider/slider.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -885,8 +885,8 @@ export class MatSlider implements AfterViewInit, OnDestroy, _MatSlider {
885885

886886
private _updateTickMarkUINonRange(step: number): void {
887887
const value = this._getValue();
888-
let numActive = Math.max(Math.round((value - this.min) / step), 0);
889-
let numInactive = Math.max(Math.round((this.max - value) / step), 0);
888+
let numActive = Math.max(Math.round((value - this.min) / step), 0) + 1;
889+
let numInactive = Math.max(Math.round((this.max - value) / step), 0) - 1;
890890
this._isRtl ? numActive++ : numInactive++;
891891

892892
this._tickMarks = Array(numActive)

0 commit comments

Comments
 (0)
Please sign in to comment.