Skip to content

Commit c4ec8e4

Browse files
committed
fix(range): emit ionInput when value changes
1 parent 9e11fcb commit c4ec8e4

File tree

2 files changed

+37
-3
lines changed

2 files changed

+37
-3
lines changed

core/src/components/range/range.tsx

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,12 @@ export class Range implements ComponentInterface {
213213
*/
214214
@Prop({ mutable: true }) value: RangeValue = 0;
215215
@Watch('value')
216-
protected valueChanged() {
216+
protected valueChanged(newVlue: RangeValue, oldValue: RangeValue) {
217+
// The check is necessary because the value can be an object.
218+
if (JSON.stringify(newVlue) !== JSON.stringify(oldValue)) {
219+
this.ionInput.emit({ value: this.value });
220+
}
221+
217222
if (!this.noUpdate) {
218223
this.updateRatio();
219224
}
@@ -591,8 +596,6 @@ export class Range implements ComponentInterface {
591596
upper: Math.max(valA, valB),
592597
};
593598

594-
this.ionInput.emit({ value: this.value });
595-
596599
this.noUpdate = false;
597600
}
598601

core/src/components/range/test/range-events.e2e.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,37 @@ configs({ modes: ['ios'], directions: ['ltr'] }).forEach(({ title, config }) =>
217217
expect(ionInputSpy).toHaveReceivedEvent();
218218
});
219219

220+
test('should not emit when the value does not change', async ({ page }) => {
221+
/**
222+
* Requires padding to prevent the knob from being clipped.
223+
* If it's clipped, then the value might be one off.
224+
* For example, if the knob is clipped on the right, then the value
225+
* will be 99 instead of 100.
226+
*/
227+
await page.setContent(
228+
`
229+
<div style="padding: 0 20px">
230+
<ion-range aria-label="range"></ion-range>
231+
</div>
232+
`,
233+
config
234+
);
235+
236+
const rangeHandle = page.locator('ion-range .range-knob-handle');
237+
const ionInputSpy = await page.spyOnEvent('ionInput');
238+
239+
const rangeHandleBoundingBox = await rangeHandle.boundingBox();
240+
const x = rangeHandleBoundingBox!.width / 2;
241+
const y = rangeHandleBoundingBox!.height / 2;
242+
243+
// Click in the middle of the knob to prevent the knob from moving.
244+
await rangeHandle.click({
245+
position: { x, y },
246+
});
247+
248+
expect(ionInputSpy).not.toHaveReceivedEvent();
249+
});
250+
220251
test('should emit when the knob is moved with the keyboard', async ({ page }) => {
221252
await page.setContent(`<ion-range aria-label="range" value="50"></ion-range>`, config);
222253

0 commit comments

Comments
 (0)