Skip to content

fix(range): emit ionInput when value changes #30293

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Mar 24, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 21 additions & 3 deletions core/src/components/range/range.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -213,12 +213,32 @@ export class Range implements ComponentInterface {
*/
@Prop({ mutable: true }) value: RangeValue = 0;
@Watch('value')
protected valueChanged() {
protected valueChanged(newValue: RangeValue, oldValue: RangeValue) {
const valuesChanged = this.compareValues(newValue, oldValue);
if (valuesChanged) {
this.ionInput.emit({ value: this.value });
}

if (!this.noUpdate) {
this.updateRatio();
}
}

/**
* Compares two RangeValue inputs to determine if they are different.
*
* @param newVal - The new value.
* @param oldVal - The old value.
* @returns `true` if the values are different, `false` otherwise.
*/
private compareValues = (newVal: RangeValue, oldVal: RangeValue) => {
if (typeof newVal === 'object' && typeof oldVal === 'object') {
return newVal.lower !== oldVal.lower || newVal.upper !== oldVal.upper;
}

return newVal !== oldVal;
};

private clampBounds = (value: any): number => {
return clamp(this.min, value, this.max);
};
Expand Down Expand Up @@ -591,8 +611,6 @@ export class Range implements ComponentInterface {
upper: Math.max(valA, valB),
};

this.ionInput.emit({ value: this.value });

this.noUpdate = false;
}

Expand Down
36 changes: 36 additions & 0 deletions core/src/components/range/test/range-events.e2e.ts
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,42 @@ configs({ modes: ['ios'], directions: ['ltr'] }).forEach(({ title, config }) =>
expect(ionInputSpy).toHaveReceivedEvent();
});

test('should not emit when the value does not change', async ({ page }, testInfo) => {
testInfo.annotations.push({
type: 'issue',
description: 'https://github.com/ionic-team/ionic-framework/issues/29619',
});

/**
* Requires padding to prevent the knob from being clipped.
* If it's clipped, then the value might be one off.
* For example, if the knob is clipped on the right, then the value
* will be 99 instead of 100.
*/
await page.setContent(
`
<div style="padding: 0 20px">
<ion-range aria-label="range"></ion-range>
</div>
`,
config
);

const rangeHandle = page.locator('ion-range .range-knob-handle');
const ionInputSpy = await page.spyOnEvent('ionInput');

const rangeHandleBoundingBox = await rangeHandle.boundingBox();
const x = rangeHandleBoundingBox!.width / 2;
const y = rangeHandleBoundingBox!.height / 2;

// Click in the middle of the knob to prevent the knob from moving.
await rangeHandle.click({
position: { x, y },
});

expect(ionInputSpy).not.toHaveReceivedEvent();
});

test('should emit when the knob is moved with the keyboard', async ({ page }) => {
await page.setContent(`<ion-range aria-label="range" value="50"></ion-range>`, config);

Expand Down
Loading