Skip to content

Commit 3ce5f23

Browse files
committed
Allow click on range option to slide pieces
1 parent 8ca5d43 commit 3ce5f23

File tree

1 file changed

+27
-4
lines changed

1 file changed

+27
-4
lines changed

src/range_slider.js

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ export default class extends Controller {
6161
container: 'relative h-10 w-full',
6262
background: 'absolute h-2 w-full bg-gray-200 rounded-lg',
6363
selected: 'absolute h-2 w-full bg-indigo-600 rounded-lg',
64-
scale: 'absolute left-0 w-full mt-2 text-xs text-gray-600',
64+
scale: 'absolute left-0 w-full mt-2 text-xs text-gray-600 cursor-pointer',
6565
pointer: `absolute cursor-pointer z-10 inline-block size-5 transform rounded-full border border-gray-200
6666
bg-white ring-0 shadow-sm transition-transform duration-200 ease-in-out translate-x-5`,
6767
tip: 'bg-gray-800 text-white text-xs px-1 rounded'
@@ -111,8 +111,6 @@ export default class extends Controller {
111111

112112
this.setRangeValues(this.valRange, this.step)
113113

114-
console.log(this.values.range, this.inputMinValue)
115-
116114
this.values.start = this.isRange ? this.values.range.indexOf(this.inputMinValue) : 0
117115
this.values.end = this.isRange ? this.values.range.indexOf(this.inputMaxValue) : this.values.range.indexOf(this.inputMinValue)
118116
}
@@ -239,7 +237,6 @@ export default class extends Controller {
239237
if (this.isRange && this.values.start > this.values.end) {
240238
this.values.start = this.values.end
241239
}
242-
console.log(this.values)
243240

244241
if (this.isRange) {
245242
this.pointerL.style.left = (this.values.start * this.stepWidth - (this.pointerWidth / 2)) + 'px'
@@ -287,6 +284,11 @@ export default class extends Controller {
287284
this.pointerL.addEventListener('mousedown', this.drag.bind(this))
288285
this.pointerR?.addEventListener('mousedown', this.drag.bind(this))
289286
window.addEventListener('resize', this.onResize.bind(this))
287+
288+
const pieces = this.slider.querySelectorAll('span');
289+
for (var i = 0, iLen = pieces.length; i < iLen; i++) {
290+
pieces[i].addEventListener('click', this.onClickPiece.bind(this));
291+
}
290292
}
291293

292294
drag(e) {
@@ -320,6 +322,27 @@ export default class extends Controller {
320322
this.activePointer = null
321323
}
322324

325+
onClickPiece(e) {
326+
if (this.isDisabled) return;
327+
328+
var idx = Math.round((e.clientX - this.sliderLeft) / this.stepWidth);
329+
330+
if (idx > this.values.range.length - 1) idx = this.values.range.length - 1;
331+
if (idx < 0) idx = 0;
332+
333+
if (this.isRange) {
334+
if (idx - this.values.start <= this.values.end - idx) {
335+
this.values.start = idx;
336+
}
337+
else this.values.end = idx;
338+
}
339+
else this.values.end = idx;
340+
341+
this.slider.classList.remove('sliding');
342+
343+
this.setSliders()
344+
}
345+
323346
onResize() {
324347
this.sliderLeft = this.slider.getBoundingClientRect().left
325348
this.sliderWidth = this.slider.clientWidth

0 commit comments

Comments
 (0)