Skip to content

Commit 71a3a96

Browse files
committed
Fix broken range pointers when values are empty
1 parent 283dfd4 commit 71a3a96

File tree

6 files changed

+42
-24
lines changed

6 files changed

+42
-24
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
# 1.0.3
2+
3+
* Fix broken range pointers if initial values are empty
4+
15
# 1.0.2
26

37
* Fix range to slide on click

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,8 @@ Now we can add `range-slider` component to our view;
6767
<input type="text" data-range-slider-target="inputMax" value="5" />
6868

6969
<!-- OR erb -->
70-
<%= form.text_field :range_min, data: { rangeSliderTarget: "inputMin" } %>
71-
<%= form.text_field :range_max, data: { rangeSliderTarget: "inputMax" } %>
70+
<%= form.text_field :range_min, data: { range_slider_target: "inputMin" } %>
71+
<%= form.text_field :range_max, data: { range_slider_target: "inputMax" } %>
7272

7373
<!-- To show value on different element => <span data-range-slider-target="value"></span> -->
7474
</div>
@@ -85,7 +85,7 @@ Now we can add `range-slider` component to our view;
8585
<input type="text" data-range-slider-target="inputMin" value="5" />
8686

8787
<!-- OR erb -->
88-
<%= form.text_field :range_min, data: { rangeSliderTarget: "inputMin" } %>
88+
<%= form.text_field :range_min, data: { range_slider_target: "inputMin" } %>
8989

9090
<!-- To show value on different element => <span data-range-slider-target="value"></span> -->
9191
</div>

index.html

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ <h2 class="mb-6 text-2xl font-semibold text-gray-800">
5656
<label class="block mb-2 text-sm font-medium text-gray-700 ">Example of single range
5757
slider</label>
5858

59-
<input type="text" data-range-slider-target="inputMin" value="5">
59+
<input type="text" data-range-slider-target="inputMin">
6060
</div>
6161

6262
<!-- Single Range Slider with markers -->
@@ -135,8 +135,8 @@ <h2 class="mb-6 text-2xl font-semibold text-gray-800">
135135
<label class="block mb-2 text-sm font-medium text-gray-700 ">Example of multi point range
136136
slider</label>
137137

138-
<input type="text" data-range-slider-target="inputMin" value="2">
139-
<input type="text" data-range-slider-target="inputMax" value="5">
138+
<input type="text" data-range-slider-target="inputMin">
139+
<input type="text" data-range-slider-target="inputMax">
140140
</div>
141141

142142
<!-- Multi Range Slider with Labels -->

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "stimulus-range-slider",
3-
"version": "1.0.2",
3+
"version": "1.0.3",
44
"description": "A range slider controller for Stimulus",
55
"source": "./src/range_slider.js",
66
"main": "./dist/stimulus-range-slider.cjs",

src/range_slider.js

Lines changed: 29 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,8 @@ export default class extends Controller {
3636
this.step = 0;
3737
this.sliderMinValue = 0;
3838
this.sliderMaxValue = 0;
39-
this.inputMinValue = 0;
40-
this.inputMaxValue = 0;
41-
this.valRange = null;
39+
this.inputMinValue = null;
40+
this.inputMaxValue = null;
4241
this.activePointer = null;
4342
this.isRange = false;
4443
this.isDisabled = false;
@@ -87,24 +86,28 @@ export default class extends Controller {
8786

8887
setInitialValues() {
8988
this.step = Number(this.stepValue || 1);
90-
this.valRange = this.valuesValue;
9189

9290
if (this.step <= 0) return;
9391

9492
if (this.hasInputMinTarget) {
9593
this.inputMin = this.inputMinTarget;
96-
this.inputMinValue = Number(this.inputMin.value);
94+
95+
if (this.inputMin.value === "") {
96+
this.inputMinValue = null;
97+
} else {
98+
this.inputMinValue = Number(this.inputMin.value);
99+
}
97100
if (isNaN(this.inputMinValue)) {
98101
this.inputMinValue = this.inputMin.value;
99102
}
100-
101-
if (this.inputMinValue === "") return;
102103
}
103104

104105
if (this.hasInputMaxTarget) {
105106
this.inputMax = this.inputMaxTarget;
106107

107-
if (this.inputMax.value instanceof String) {
108+
if (this.inputMax.value === "") {
109+
this.inputMaxValue = null;
110+
} else if (this.inputMax.value instanceof String) {
108111
this.inputMaxValue = this.inputMax.value;
109112
} else {
110113
this.inputMaxValue = Number(this.inputMax.value);
@@ -116,14 +119,25 @@ export default class extends Controller {
116119
this.isDisabled = this.disabledValue;
117120
this.isRange = this.hasInputMaxTarget && this.hasInputMinTarget;
118121

119-
this.setRangeValues(this.valRange, this.step);
122+
this.setRangeValues(this.valuesValue, this.step);
120123

121-
this.values.start = this.isRange
122-
? this.values.range.indexOf(this.inputMinValue)
123-
: 0;
124-
this.values.end = this.isRange
125-
? this.values.range.indexOf(this.inputMaxValue)
126-
: this.values.range.indexOf(this.inputMinValue);
124+
if (this.inputMin.value === "") {
125+
this.values.start = 0;
126+
} else {
127+
this.values.start = this.isRange
128+
? this.values.range.indexOf(this.inputMinValue)
129+
: 0;
130+
}
131+
132+
if (this.hasInputMaxTarget && this.inputMax.value === "") {
133+
this.values.end = this.values.range.length - 1;
134+
} else {
135+
this.values.end = this.isRange
136+
? this.values.range.indexOf(this.inputMaxValue)
137+
: this.inputMin.value === ""
138+
? this.values.range.length - 1
139+
: this.values.range.indexOf(this.inputMinValue);
140+
}
127141
}
128142

129143
setRangeValues(valueStr, step) {

0 commit comments

Comments
 (0)