Skip to content

Commit 62194cc

Browse files
committed
refactor the springPositions to fix #20
- move the spring positions in to a reactive statement - consolidate the percentOf() calls - remove the error with NaN when min/max is 0 (division error) related to #15 fixes #20
1 parent 9e9e657 commit 62194cc

File tree

3 files changed

+55
-45
lines changed

3 files changed

+55
-45
lines changed

src/RangeSlider.svelte

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -50,21 +50,26 @@
5050
5151
// copy the initial values in to a spring function which
5252
// will update every time the values array is modified
53-
let springPositions = spring(
54-
values.map((v) =>
55-
parseFloat((((v - min) / (max - min)) * 100).toFixed(precision))
56-
),
57-
springValues
58-
);
59-
60-
// check the values array, and trim it if needed (range)
61-
// and clamp the values to the steps and boundaries set up in the slider
62-
$: values = trimRange(values).map((v) => alignValueToStep(v));
63-
64-
// update the spring function so that movement can happen in the UI
53+
let springPositions;
54+
6555
$: {
66-
springPositions.set(values.map((v) => percentOf(v)));
67-
}
56+
// check that "values" is an array, or set it as array
57+
// to prevent any errors in springs, or range trimming
58+
if ( !Array.isArray( values ) ) {
59+
values = [(max + min) / 2];
60+
console.error( "'values' prop should be an Array (https://github.com/simeydotme/svelte-range-slider-pips#slider-props)" );
61+
}
62+
// trim the range as needed
63+
values = trimRange(values);
64+
// clamp the values to the steps and boundaries set up in the slider
65+
values = values.map((v) => alignValueToStep(v));
66+
// update the spring function so that movement can happen in the UI
67+
if ( springPositions ) {
68+
springPositions.set(values.map((v) => percentOf(v)));
69+
} else {
70+
springPositions = spring( values.map((v) => percentOf(v)), springValues );
71+
}
72+
};
6873
6974
/**
7075
* take in a value, and then calculate that value's percentage
@@ -167,7 +172,9 @@
167172
168173
/**
169174
* trim the values array based on whether the property
170-
* for 'range' is 'min', 'max', or truthy.
175+
* for 'range' is 'min', 'max', or truthy. This is because we
176+
* do not want more than one handle for a min/max range, and we do
177+
* not want more than two handles for a true range.
171178
* @param {array} values the input values for the rangeSlider
172179
* @return {array} the range array for creating a rangeSlider
173180
**/

test/src/App.svelte

Lines changed: 29 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,31 @@
11
<script>
2+
23
import RangeSlider from "../../src/RangeSlider.svelte";
3-
const num = new Intl.NumberFormat("en-US");
4-
const numzh = new Intl.NumberFormat("zh-Hans-CN-u-nu-hanidec");
4+
55
let values = [21.3, 40, 60, 80];
6-
let day = [3];
7-
let hue = [244];
86
let dynamic = [0,50];
97
let pushy = [30,60]
10-
const formatter = v => {
11-
return num.format(v);
12-
};
13-
const days = [
14-
"Monday",
15-
"Tuesday",
16-
"Wednesday",
17-
"Thursday",
18-
"Friday",
19-
"Saturday",
20-
"Happy Days"
21-
];
22-
const dayFormat = v => days[v];
23-
const dayFormatCn = v => {
24-
if (v === 6) {
25-
return "星期日";
26-
}
27-
return "星期" + numzh.format(v + 1);
28-
};
29-
$: lightColor = `hsl(${Math.round(hue[0]) - 10}, 65%, 70%)`;
30-
$: color = `hsl(${Math.round(hue[0])}, 63%, 54%)`;
318
9+
const num = new Intl.NumberFormat("en-US");
10+
const numzh = new Intl.NumberFormat("zh-Hans-CN-u-nu-hanidec");
11+
const formatter = v => { return num.format(v); };
12+
13+
let day = [3];
14+
const days = [ "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Happy Days" ];
15+
const dayFormat = v => days[v];
16+
const dayFormatCn = v => { if (v === 6) { return "星期日"; }; return "星期" + numzh.format(v + 1); };
17+
3218
let perc1 = [5];
3319
let perc2 = [100 - perc1];
3420
$: perc2max = 100 - perc1[0];
21+
22+
let hue = [244];
23+
$: lightColor = `hsl(${Math.round(hue[0]) - 10}, 65%, 70%)`;
24+
$: color = `hsl(${Math.round(hue[0])}, 63%, 54%)`;
25+
26+
let zero = [2,10];
27+
let zeromin = 0;
28+
let zeromax = 0;
3529
3630
</script>
3731

@@ -64,8 +58,11 @@
6458
<RangeSlider vertical range values={[10,30]} pips all="label" />
6559
<RangeSlider vertical range="min" values={[10]} pips all />
6660
<RangeSlider vertical range="max" values={[30]} pips />
61+
62+
<br>
63+
<RangeSlider id="test-id" springValues={{ stiffness: 0.03, damping: 0.08 }} />
6764
<br>
68-
<RangeSlider id="test-id" />
65+
6966
<RangeSlider bind:values
7067
on:start={(e) => { console.log("start",e.detail)}}
7168
on:stop={(e) => { console.log("end",e.detail)}}
@@ -118,6 +115,12 @@
118115
<hr>
119116
percent1: {perc1}<br>percent2: {perc2}
120117

118+
<br><br>
119+
120+
<RangeSlider bind:values={zero} bind:min={zeromin} bind:max={zeromax} range float pips all="label" step={1} pipstep={5} />
121+
<br><button on:click={()=>{ zeromin = 1; zeromax = 30; zero = 10; }}>increase min/max</button> - {zero}
122+
123+
121124
</div>
122125

123126

test/yarn.lock

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -554,10 +554,10 @@ supports-color@^6.1.0:
554554
dependencies:
555555
has-flag "^3.0.0"
556556

557-
svelte@^3.31.2:
558-
version "3.31.2"
559-
resolved "https://registry.yarnpkg.com/svelte/-/svelte-3.31.2.tgz#d2ddf6cacbb95e4cc3796207510b660a25586324"
560-
integrity sha512-TxZGrXzX2ggFH3BIKY5fmbeMdJuZrMIMDYPMX6R9255bueuYIuVaBQSLUeY2oD7W4IdeqRZiAVGCjDw2POKBRA==
557+
svelte@^3.0.0:
558+
version "3.32.1"
559+
resolved "https://registry.yarnpkg.com/svelte/-/svelte-3.32.1.tgz#c4b6e35517d0ed77e652cc8964ef660afa2f70f3"
560+
integrity sha512-j1KmD2ZOU0RGq1/STDXjwfh0/eJ/Deh2NXyuz1bpR9eOcz9yImn4CGxXdbSAN7cMTm9a7IyPUIbuBCzu/pXK0g==
561561

562562
terser@^4.6.2:
563563
version "4.8.0"

0 commit comments

Comments
 (0)