-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add range slider & use it for the price slider
- Loading branch information
Showing
5 changed files
with
134 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
<script> | ||
export default { | ||
props: { | ||
range: { | ||
type: Object, | ||
default: () => ({min: 0, max: 100}) | ||
}, | ||
current: { | ||
type: Object, | ||
default: () => ({min: undefined, max: undefined}) | ||
}, | ||
}, | ||
render() { | ||
return this.$scopedSlots.default(this) | ||
}, | ||
data() { | ||
return { | ||
minValue: null, | ||
maxValue: null, | ||
} | ||
}, | ||
watch: { | ||
minValue() { | ||
if(this.minValue >= this.maxValue) { | ||
this.minValue = this.maxValue - 1 | ||
} | ||
}, | ||
maxValue() { | ||
if(this.maxValue <= this.minValue) { | ||
this.maxValue = this.minValue + 1 | ||
} | ||
}, | ||
range() { | ||
if(this.range.min == this.range.max && this.range.min == 0) { | ||
return | ||
} | ||
if(this.minValue === null && this.maxValue === null) { | ||
this.updateFromProps() | ||
} | ||
}, | ||
current() { | ||
this.updateFromProps() | ||
}, | ||
}, | ||
mounted() { | ||
this.updateFromProps() | ||
}, | ||
methods: { | ||
updateFromProps() { | ||
this.minValue = this.current.min ?? this.range.min | ||
this.maxValue = this.current.max ?? this.range.max | ||
}, | ||
percentage(amount) { | ||
return (amount - this.range.min) / (this.range.max - this.range.min) * 100 | ||
}, | ||
updateRefinement() { | ||
this.$emit('change', { | ||
min: this.minValue, | ||
max: this.maxValue | ||
}) | ||
}, | ||
}, | ||
computed: { | ||
minThumb() { | ||
return Math.max(this.percentage(this.minValue), 0) | ||
}, | ||
maxThumb() { | ||
return Math.max(100 - this.percentage(this.maxValue), 0) | ||
}, | ||
}, | ||
} | ||
</script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<range-slider v-slot="rangeInputScope" {{ $attributes }}> | ||
<div class="flex relative h-16 mx-5"> | ||
<input | ||
type="range" | ||
v-bind:disabled="!canRefine" | ||
v-bind:min="rangeInputScope.range.min" | ||
v-bind:max="rangeInputScope.range.max" | ||
v-model="rangeInputScope.minValue" | ||
v-on:change="rangeInputScope.updateRefinement" | ||
class="absolute pointer-events-none appearance-none z-20 h-5 w-full opacity-0 cursor-pointer [&::-moz-range-thumb]:pointer-events-auto [&::-webkit-slider-thumb]:pointer-events-auto" | ||
> | ||
|
||
<input | ||
type="range" | ||
v-bind:disabled="!canRefine" | ||
v-bind:min="rangeInputScope.range.min" | ||
v-bind:max="rangeInputScope.range.max" | ||
v-model="rangeInputScope.maxValue" | ||
v-on:change="rangeInputScope.updateRefinement" | ||
class="absolute pointer-events-none appearance-none z-20 h-5 w-full opacity-0 cursor-pointer [&::-moz-range-thumb]:pointer-events-auto [&::-webkit-slider-thumb]:pointer-events-auto" | ||
> | ||
|
||
<div class="relative z-10 h-1 w-full mt-2 mx-2"> | ||
<div class="absolute z-10 inset-0 rounded-md bg-border"></div> | ||
<div class="absolute z-20 inset-y-0 rounded-md bg-primary" v-bind:style="'right:'+rangeInputScope.maxThumb+'%; left:'+rangeInputScope.minThumb+'%'"></div> | ||
<div class="absolute z-30 size-5 top-0 left-0 bg-white border rounded-full -mt-2 -translate-x-1/2" v-bind:style="'left: '+rangeInputScope.minThumb+'%'"></div> | ||
<div class="absolute z-30 size-5 top-0 right-0 bg-white border rounded-full -mt-2 translate-x-1/2" v-bind:style="'right: '+rangeInputScope.maxThumb+'%'"></div> | ||
<div class="absolute z-30 top-4 left-0 bg-white border px-2 py-0.5 rounded-full -translate-x-1/2" v-bind:style="'left: '+rangeInputScope.minThumb+'%'"> | ||
@{{ rangeInputScope.minValue }} | ||
</div> | ||
<div class="absolute z-30 top-4 right-0 bg-white border px-2 py-0.5 rounded-full translate-x-1/2" v-bind:style="'right: '+rangeInputScope.maxThumb+'%'"> | ||
@{{ rangeInputScope.maxValue }} | ||
</div> | ||
</div> | ||
</div> | ||
</range-input> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters