Skip to content

Commit

Permalink
fix(range): fixes when step size is bigger than range
Browse files Browse the repository at this point in the history
fixes #8830
fixes #8802
  • Loading branch information
manucorporat committed Oct 21, 2016
1 parent 90f9b5c commit 9895b86
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 7 deletions.
4 changes: 4 additions & 0 deletions src/components/range/range.scss
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,14 @@
// --------------------------------------------------

.item-range .item-inner {
width: 100%;

overflow: visible;
}

.item-range .input-wrapper {
width: 100%;

overflow: visible;

flex-direction: column;
Expand Down
10 changes: 6 additions & 4 deletions src/components/range/range.ts
Original file line number Diff line number Diff line change
Expand Up @@ -602,16 +602,18 @@ export class Range extends Ion implements AfterViewInit, ControlValueAccessor, O
* @private
*/
ratioToValue(ratio: number) {
ratio = Math.round(((this._max - this._min) * ratio) + this._min);
return Math.round(ratio / this._step) * this._step;
ratio = Math.round(((this._max - this._min) * ratio));
ratio = Math.round(ratio / this._step) * this._step + this._min;
return clamp(this._min, ratio, this._max);
}

/**
* @private
*/
valueToRatio(value: number) {
value = Math.round(clamp(this._min, value, this._max) / this._step) * this._step;
return (value - this._min) / (this._max - this._min);
value = Math.round((value - this._min) / this._step) * this._step;
value = value / (this._max - this._min);
return clamp(0, value, 1);
}

/**
Expand Down
6 changes: 6 additions & 0 deletions src/components/range/test/basic/page1.html
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@
<ion-content>

<ion-list>

<ion-item>
<ion-label>step="5199" max="5199" min="2499" snaps="true"</ion-label>
<ion-range step="5199" max="5199" min="2499" snaps="true"></ion-range>
</ion-item>

<ion-item>
<ion-range [(ngModel)]="singleValue" color="danger" pin="true" (ionChange)="rangeChange($event)" [debounce]="100"></ion-range>
</ion-item>
Expand Down
69 changes: 69 additions & 0 deletions src/components/range/test/range.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import { Range } from '../range';
import { mockConfig, mockRenderer, mockElementRef, mockHaptic } from '../../../util/mock-providers';
import { Form } from '../../../util/form';


describe('Range', () => {
describe('valueToRatio', () => {
it('step=1', () => {
let range = createRange();
range.max = 5000;
range.min = 2490;
range.step = 1;
range.snaps = true;
expect(range.valueToRatio(5000)).toEqual(1);
expect(range.valueToRatio(5100)).toEqual(1);
expect(range.valueToRatio(2490)).toEqual(0);
expect(range.valueToRatio(2000)).toEqual(0);

let middle = (range.max - range.min) / 2 + range.min;
expect(range.valueToRatio(middle)).toEqual(0.5);
});

it('step>range', () => {
let range = createRange();
range.max = 5000;
range.min = 2490;
range.step = 5900;
range.snaps = true;
expect(range.valueToRatio(7000)).toEqual(1);
expect(range.valueToRatio(5000)).toEqual(0);
expect(range.valueToRatio(2490)).toEqual(0);
expect(range.valueToRatio(2000)).toEqual(0);
});
});

describe('ratioToValue', () => {
it('step=1', () => {
let range = createRange();
range.max = 5000;
range.min = 2490;
range.step = 1;
range.snaps = true;
expect(range.ratioToValue(0)).toEqual(2490);
expect(range.ratioToValue(1)).toEqual(5000);

let middle = (range.max - range.min) / 2 + range.min;
expect(range.ratioToValue(0.5)).toEqual(middle);
});

it('step>range', () => {
let range = createRange();
range.max = 5000;
range.min = 2490;
range.step = 1;
expect(range.ratioToValue(0)).toEqual(2490);
expect(range.ratioToValue(1)).toEqual(5000);

let middle = (range.max - range.min) / 2 + range.min;
expect(range.ratioToValue(0.5)).toEqual(middle);
});
});

});


function createRange(): Range {
let form = new Form();
return new Range(form, mockHaptic(), null, mockConfig(), mockElementRef(), mockRenderer());
}
8 changes: 5 additions & 3 deletions src/util/haptic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,11 @@ export class Haptic {
plugin: any;

constructor(platform: Platform) {
platform.ready().then(() => {
this.plugin = window.TapticEngine;
});
if (platform) {
platform.ready().then(() => {
this.plugin = window.TapticEngine;
});
}
}

/**
Expand Down
5 changes: 5 additions & 0 deletions src/util/mock-providers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import { UrlSerializer } from '../navigation/url-serializer';
import { ViewController } from '../navigation/view-controller';

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

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

export const mockHaptic = function (): Haptic {
return new Haptic(null);
}

export class MockView {}
export class MockView1 {}
export class MockView2 {}
Expand Down

0 comments on commit 9895b86

Please sign in to comment.