Skip to content

Commit cb1887d

Browse files
committed
fix(range): handling the case where range would return NaN if max or min were set to undefined by setting max and min to their default values if you try to set them directly to undefined
1 parent c3e822a commit cb1887d

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

core/src/components/range/range.tsx

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import type { ComponentInterface, EventEmitter } from '@stencil/core';
22
import { Component, Element, Event, Host, Prop, State, Watch, h } from '@stencil/core';
33
import { findClosestIonContent, disableContentScrollY, resetContentScrollY } from '@utils/content';
44
import type { Attributes } from '@utils/helpers';
5-
import { inheritAriaAttributes, clamp, debounceEvent, renderHiddenInput } from '@utils/helpers';
5+
import { inheritAriaAttributes, clamp, debounceEvent, renderHiddenInput, isSafeNumber } from '@utils/helpers';
66
import { printIonWarning } from '@utils/logging';
77
import { isRTL } from '@utils/rtl';
88
import { createColorClasses, hostContext } from '@utils/theme';
@@ -110,6 +110,10 @@ export class Range implements ComponentInterface {
110110
@Prop() min = 0;
111111
@Watch('min')
112112
protected minChanged() {
113+
if (!isSafeNumber(this.min)) {
114+
this.min = 0;
115+
}
116+
113117
if (!this.noUpdate) {
114118
this.updateRatio();
115119
}
@@ -121,6 +125,10 @@ export class Range implements ComponentInterface {
121125
@Prop() max = 100;
122126
@Watch('max')
123127
protected maxChanged() {
128+
if (!isSafeNumber(this.max)) {
129+
this.max = 100;
130+
}
131+
124132
if (!this.noUpdate) {
125133
this.updateRatio();
126134
}

core/src/components/range/test/range.spec.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,23 @@ describe('Range', () => {
2828
});
2929
});
3030

31+
it('should handle undefined min and max values by falling back to defaults', async () => {
32+
const page = await newSpecPage({
33+
components: [Range],
34+
html: `<ion-range id="my-custom-range">
35+
<div slot="label">Range</div>
36+
</ion-range>`,
37+
});
38+
39+
const range = page.body.querySelector('ion-range')!;
40+
// Here we have to cast this to any, but in its react wrapper it accepts undefined as a valid value
41+
range.min = undefined as any;
42+
range.max = undefined as any;
43+
await page.waitForChanges();
44+
expect(range.min).toBe(0);
45+
expect(range.max).toBe(100);
46+
});
47+
3148
it('should return the clamped value for a range dual knob component', () => {
3249
sharedRange.min = 0;
3350
sharedRange.max = 100;

0 commit comments

Comments
 (0)