Input Binding with Vue 3 #156
-
I'm using this repo with Vue 3 and TypeScript to create a range slider with two corresponding input fields (for minimum and maximum values). When I move the slider handles, the input fields update correctly. However, when I manually change the input values, the slider does not reflect this change. It seems like the slider does not detect external changes to the min and max values once it’s initialized. I couldn’t find any method in documentation to programmatically update the slider position based on input values. Is there a built-in way to synchronize the slider with input changes? <script setup lang="ts">
import { ref, reactive, onMounted } from 'vue'
const rangeDefinitions = {
power: { min: 0, max: 81, step: 10 }
}
const rangeStates = reactive({
power: { min: rangeDefinitions.power.min, max: rangeDefinitions.power.max }
})
const $powerRangeSliderNode = ref(null)
onMounted(async () => {
const { default: RangeSlider } = await import('svelte-range-slider-pips')
const powerRangeSlider = new RangeSlider({
target: $powerRangeSliderNode.value,
props: {
values: [rangeStates.power.min, rangeStates.power.max],
pips: true,
range: true,
float: true,
step: rangeDefinitions.power.step,
min: rangeDefinitions.power.min,
max: rangeDefinitions.power.max,
suffix: 'k'
}
})
powerRangeSlider.$on('change', (e) => {
rangeStates.power.min = e.detail.values[0]
rangeStates.power.max = e.detail.values[1]
})
})
</script>
<template>
<div>
<h4>Engine Power (kW)</h4>
<div class="flex items-center gap-4 mt-4">
<!-- Minimum Value Input -->
<UFormGroup label="Minimum" name="engine-power-minimum">
<UInput
v-model="rangeStates.power.min"
type="number"
:min="rangeDefinitions.power.min"
:max="rangeStates.power.max"
:step="rangeDefinitions.power.step"
:placeholder="rangeStates.power.min.toString()"
/>
</UFormGroup>
<!-- Maximum Value Input -->
<UFormGroup label="Maximum" name="engine-power-maximum">
<UInput
v-model="rangeStates.power.max"
type="number"
:min="rangeStates.power.min"
:max="rangeDefinitions.power.max"
:step="rangeDefinitions.power.step"
:placeholder="rangeStates.power.max.toString()"
/>
</UFormGroup>
</div>
<!-- Slider Element -->
<div id="power-slider" ref="$powerRangeSliderNode" class="mt-8 px-2" />
</div>
</template> Moving the slider updates Any guidance on how to sync the slider with manual input changes would be appreciated! |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 7 replies
-
Forgot to mention that if I import this package synchronously ( "Class extends value undefined is not a constructor or null" |
Beta Was this translation helpful? Give feedback.
-
Here's a working version based on the documentation website link to "see a vue example running at play.vuejs" If you have any improvements / suggestions for the documentation to help Vue users get started easier, please let me know and/or create some PRs on the |
Beta Was this translation helpful? Give feedback.
Ok, there's an even easier way to do it;
see here, im using v-model and an generic slider update function.