Skip to content

Commit e01c7d7

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 e01c7d7

File tree

2 files changed

+32
-3
lines changed

2 files changed

+32
-3
lines changed

core/src/components/range/range.tsx

Lines changed: 15 additions & 3 deletions
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';
@@ -109,7 +109,11 @@ export class Range implements ComponentInterface {
109109
*/
110110
@Prop() min = 0;
111111
@Watch('min')
112-
protected minChanged() {
112+
protected minChanged(newValue: number) {
113+
if (!isSafeNumber(newValue)) {
114+
this.min = 0;
115+
}
116+
113117
if (!this.noUpdate) {
114118
this.updateRatio();
115119
}
@@ -120,7 +124,11 @@ export class Range implements ComponentInterface {
120124
*/
121125
@Prop() max = 100;
122126
@Watch('max')
123-
protected maxChanged() {
127+
protected maxChanged(newValue: number) {
128+
if (!isSafeNumber(newValue)) {
129+
this.max = 100;
130+
}
131+
124132
if (!this.noUpdate) {
125133
this.updateRatio();
126134
}
@@ -300,6 +308,10 @@ export class Range implements ComponentInterface {
300308
}
301309

302310
this.inheritedAttributes = inheritAriaAttributes(this.el);
311+
// If the min or max is not safe, set it to 0 or 100 respectively.
312+
// Our watch does this, but not before the initial load.
313+
this.min = isSafeNumber(this.min) ? this.min : 0;
314+
this.max = isSafeNumber(this.max) ? this.max : 100;
303315
}
304316

305317
componentDidLoad() {

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)