Skip to content

Commit 9895b86

Browse files
committed
fix(range): fixes when step size is bigger than range
fixes #8830 fixes #8802
1 parent 90f9b5c commit 9895b86

File tree

6 files changed

+95
-7
lines changed

6 files changed

+95
-7
lines changed

src/components/range/range.scss

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,14 @@
44
// --------------------------------------------------
55

66
.item-range .item-inner {
7+
width: 100%;
8+
79
overflow: visible;
810
}
911

1012
.item-range .input-wrapper {
13+
width: 100%;
14+
1115
overflow: visible;
1216

1317
flex-direction: column;

src/components/range/range.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -602,16 +602,18 @@ export class Range extends Ion implements AfterViewInit, ControlValueAccessor, O
602602
* @private
603603
*/
604604
ratioToValue(ratio: number) {
605-
ratio = Math.round(((this._max - this._min) * ratio) + this._min);
606-
return Math.round(ratio / this._step) * this._step;
605+
ratio = Math.round(((this._max - this._min) * ratio));
606+
ratio = Math.round(ratio / this._step) * this._step + this._min;
607+
return clamp(this._min, ratio, this._max);
607608
}
608609

609610
/**
610611
* @private
611612
*/
612613
valueToRatio(value: number) {
613-
value = Math.round(clamp(this._min, value, this._max) / this._step) * this._step;
614-
return (value - this._min) / (this._max - this._min);
614+
value = Math.round((value - this._min) / this._step) * this._step;
615+
value = value / (this._max - this._min);
616+
return clamp(0, value, 1);
615617
}
616618

617619
/**

src/components/range/test/basic/page1.html

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,12 @@
1818
<ion-content>
1919

2020
<ion-list>
21+
22+
<ion-item>
23+
<ion-label>step="5199" max="5199" min="2499" snaps="true"</ion-label>
24+
<ion-range step="5199" max="5199" min="2499" snaps="true"></ion-range>
25+
</ion-item>
26+
2127
<ion-item>
2228
<ion-range [(ngModel)]="singleValue" color="danger" pin="true" (ionChange)="rangeChange($event)" [debounce]="100"></ion-range>
2329
</ion-item>
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
import { Range } from '../range';
2+
import { mockConfig, mockRenderer, mockElementRef, mockHaptic } from '../../../util/mock-providers';
3+
import { Form } from '../../../util/form';
4+
5+
6+
describe('Range', () => {
7+
describe('valueToRatio', () => {
8+
it('step=1', () => {
9+
let range = createRange();
10+
range.max = 5000;
11+
range.min = 2490;
12+
range.step = 1;
13+
range.snaps = true;
14+
expect(range.valueToRatio(5000)).toEqual(1);
15+
expect(range.valueToRatio(5100)).toEqual(1);
16+
expect(range.valueToRatio(2490)).toEqual(0);
17+
expect(range.valueToRatio(2000)).toEqual(0);
18+
19+
let middle = (range.max - range.min) / 2 + range.min;
20+
expect(range.valueToRatio(middle)).toEqual(0.5);
21+
});
22+
23+
it('step>range', () => {
24+
let range = createRange();
25+
range.max = 5000;
26+
range.min = 2490;
27+
range.step = 5900;
28+
range.snaps = true;
29+
expect(range.valueToRatio(7000)).toEqual(1);
30+
expect(range.valueToRatio(5000)).toEqual(0);
31+
expect(range.valueToRatio(2490)).toEqual(0);
32+
expect(range.valueToRatio(2000)).toEqual(0);
33+
});
34+
});
35+
36+
describe('ratioToValue', () => {
37+
it('step=1', () => {
38+
let range = createRange();
39+
range.max = 5000;
40+
range.min = 2490;
41+
range.step = 1;
42+
range.snaps = true;
43+
expect(range.ratioToValue(0)).toEqual(2490);
44+
expect(range.ratioToValue(1)).toEqual(5000);
45+
46+
let middle = (range.max - range.min) / 2 + range.min;
47+
expect(range.ratioToValue(0.5)).toEqual(middle);
48+
});
49+
50+
it('step>range', () => {
51+
let range = createRange();
52+
range.max = 5000;
53+
range.min = 2490;
54+
range.step = 1;
55+
expect(range.ratioToValue(0)).toEqual(2490);
56+
expect(range.ratioToValue(1)).toEqual(5000);
57+
58+
let middle = (range.max - range.min) / 2 + range.min;
59+
expect(range.ratioToValue(0.5)).toEqual(middle);
60+
});
61+
});
62+
63+
});
64+
65+
66+
function createRange(): Range {
67+
let form = new Form();
68+
return new Range(form, mockHaptic(), null, mockConfig(), mockElementRef(), mockRenderer());
69+
}

src/util/haptic.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,11 @@ export class Haptic {
3333
plugin: any;
3434

3535
constructor(platform: Platform) {
36-
platform.ready().then(() => {
37-
this.plugin = window.TapticEngine;
38-
});
36+
if (platform) {
37+
platform.ready().then(() => {
38+
this.plugin = window.TapticEngine;
39+
});
40+
}
3941
}
4042

4143
/**

src/util/mock-providers.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import { UrlSerializer } from '../navigation/url-serializer';
2222
import { ViewController } from '../navigation/view-controller';
2323

2424
import { NavControllerBase } from '../navigation/nav-controller-base';
25+
import { Haptic } from './haptic';
2526

2627
export const mockConfig = function(config?: any, url: string = '/', platform?: Platform) {
2728
const c = new Config();
@@ -397,6 +398,10 @@ export const mockDeepLinkConfig = function(links?: any[]): DeepLinkConfig {
397398
};
398399
};
399400

401+
export const mockHaptic = function (): Haptic {
402+
return new Haptic(null);
403+
}
404+
400405
export class MockView {}
401406
export class MockView1 {}
402407
export class MockView2 {}

0 commit comments

Comments
 (0)