Skip to content

Commit dec228c

Browse files
authored
refactor: preserve fractional step digits when updating value (#10987)
1 parent 260f9aa commit dec228c

5 files changed

Lines changed: 79 additions & 3 deletions

File tree

‎packages/slider/src/vaadin-range-slider.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ class RangeSlider extends SliderMixin(
166166
updated(props) {
167167
super.updated(props);
168168

169-
if (props.has('value') || props.has('min') || props.has('max')) {
169+
if (props.has('value') || props.has('min') || props.has('max') || props.has('step')) {
170170
const value = [...this.value];
171171
value.forEach((v, idx) => {
172172
this.__updateValue(v, idx, value);

‎packages/slider/src/vaadin-slider-mixin.js‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,9 @@ export const SliderMixin = (superClass) =>
102102
const nearestOffset = Math.round(offset / step) * step;
103103
const nearestValue = minValue + nearestOffset;
104104

105-
const newValue = Math.round(nearestValue);
105+
// Ensure the last value matching step is used below the max limit.
106+
// Example: max = 100, step = 1.5 - force maximum allowed value to 99.
107+
const newValue = nearestValue <= maxValue ? nearestValue : nearestValue - step;
106108

107109
this.__value = fullValue.with(index, newValue);
108110
}

‎packages/slider/src/vaadin-slider.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ class Slider extends SliderMixin(
146146
updated(props) {
147147
super.updated(props);
148148

149-
if (props.has('value') || props.has('min') || props.has('max')) {
149+
if (props.has('value') || props.has('min') || props.has('max') || props.has('step')) {
150150
this.__updateValue(this.value);
151151
}
152152
}

‎packages/slider/test/range-slider.test.ts‎

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,29 @@ describe('vaadin-range-slider', () => {
2929
});
3030
});
3131

32+
describe('value property', () => {
33+
beforeEach(async () => {
34+
slider = fixtureSync('<vaadin-range-slider></vaadin-range-slider>');
35+
slider.value = [1, 3];
36+
await nextRender();
37+
});
38+
39+
it('should not update the value when changing min', () => {
40+
slider.min = 2;
41+
expect(slider.value).to.deep.equal([1, 3]);
42+
});
43+
44+
it('should not update the value when changing max', () => {
45+
slider.max = 2;
46+
expect(slider.value).to.deep.equal([1, 3]);
47+
});
48+
49+
it('should not update the value when changing step', () => {
50+
slider.step = 1.5;
51+
expect(slider.value).to.deep.equal([1, 3]);
52+
});
53+
});
54+
3255
describe('focus', () => {
3356
let firstGlobalFocusable: HTMLElement;
3457
let lastGlobalFocusable: HTMLElement;
@@ -255,6 +278,20 @@ describe('vaadin-range-slider', () => {
255278
expect(spy).to.be.calledOnce;
256279
});
257280

281+
it('should preserve decimals when changing value using fractional step', async () => {
282+
slider.value = [0, 3];
283+
slider.step = 1.5;
284+
await sendKeys({ press: 'ArrowLeft' });
285+
expect(slider.value).to.deep.equal([0, 1.5]);
286+
});
287+
288+
it('should not increase value past max allowed value with fractional step', async () => {
289+
slider.value = [0, 99];
290+
slider.step = 1.5;
291+
await sendKeys({ press: 'ArrowRight' });
292+
expect(slider.value).to.deep.equal([0, 99]);
293+
});
294+
258295
it('should not change value on arrow key when readonly', async () => {
259296
slider.readonly = true;
260297
await sendKeys({ press: 'ArrowRight' });

‎packages/slider/test/slider.test.ts‎

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,29 @@ describe('vaadin-slider', () => {
2929
});
3030
});
3131

32+
describe('value property', () => {
33+
beforeEach(async () => {
34+
slider = fixtureSync('<vaadin-slider></vaadin-slider>');
35+
slider.value = 2;
36+
await nextRender();
37+
});
38+
39+
it('should not update the value when changing min', () => {
40+
slider.min = 3;
41+
expect(slider.value).to.equal(2);
42+
});
43+
44+
it('should not update the value when changing max', () => {
45+
slider.max = 1;
46+
expect(slider.value).to.equal(2);
47+
});
48+
49+
it('should not update the value when changing step', () => {
50+
slider.step = 1.5;
51+
expect(slider.value).to.equal(2);
52+
});
53+
});
54+
3255
describe('focus', () => {
3356
let firstGlobalFocusable: HTMLElement;
3457
let lastGlobalFocusable: HTMLElement;
@@ -125,6 +148,20 @@ describe('vaadin-slider', () => {
125148
expect(spy).to.be.calledOnce;
126149
});
127150

151+
it('should preserve decimals when changing value using fractional step', async () => {
152+
slider.value = 3;
153+
slider.step = 1.5;
154+
await sendKeys({ press: 'ArrowLeft' });
155+
expect(slider.value).to.equal(1.5);
156+
});
157+
158+
it('should not increase value past max allowed value with fractional step', async () => {
159+
slider.step = 1.5;
160+
slider.value = 99;
161+
await sendKeys({ press: 'ArrowRight' });
162+
expect(slider.value).to.equal(99);
163+
});
164+
128165
it('should not change value on arrow key when readonly', async () => {
129166
slider.readonly = true;
130167
await sendKeys({ press: 'ArrowRight' });

0 commit comments

Comments
 (0)