Skip to content

Commit

Permalink
refactor: use v-model and defineModel to update range input
Browse files Browse the repository at this point in the history
  • Loading branch information
caro3801 committed Dec 16, 2024
1 parent 7071434 commit bab312d
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,14 @@
import FormControlRangeSlider from './FormControlRangeSlider'
import FormControlRangeTicks from './FormControlRangeTicks'
const modelValue = defineModel({
type: Number,
default: 0
})
defineProps({
modelValue: {
type: Number,
default: 0
name: {
type: String
},
min: {
type: Number,
Expand All @@ -25,21 +29,8 @@ defineProps({
<template>
<div class="form-control-range d-inline-block">
<div class="form-control-range__wrapper">
<form-control-range-slider
class="mb-3"
:model-value="modelValue"
:min="min"
:max="max"
:step="step"
@update:modelValue="$emit('update:modelValue', $event)"
/>
<form-control-range-ticks
:model-value="modelValue"
:min="min"
:max="max"
:step="step"
@update:modelValue="$emit('update:modelValue', $event)"
/>
<form-control-range-slider v-model="modelValue" class="mb-3" :min="min" :max="max" :step="step" />
<form-control-range-ticks v-model="modelValue" :min="min" :max="max" :step="step" />
</div>
</div>
</template>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
<script setup>
import FormControlRangeSliderBullet from './FormControlRangeSliderBullet'
const modelValue = defineModel({
type: Number
})
defineProps({
modelValue: {
type: Number
},
step: {
type: Number
},
Expand All @@ -18,14 +19,8 @@ defineProps({
</script>

<template>
<div class="form-control-range-slider" aria-hidden>
<form-control-range-slider-bullet
:model-value="modelValue"
:min="min"
:max="max"
:step="step"
@update:modelValue="$emit('update:modelValue', $event)"
/>
<div class="form-control-range-slider" aria-hidden="true">
<form-control-range-slider-bullet v-model="modelValue" :min="min" :max="max" :step="step" />
</div>
</template>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@ import { range } from 'd3'
import { computed, ref } from 'vue'
import { draggable as vDraggable } from '@/directives/draggable'
const modelValue = defineModel({
type: Number
})
const props = defineProps({
modelValue: {
type: Number
},
step: {
type: Number
},
Expand All @@ -27,7 +26,7 @@ const steps = computed(() => {
})
const modelValueStep = computed(() => {
return steps.value.indexOf(props.modelValue)
return steps.value.indexOf(modelValue.value)
})
const style = computed(() => {
Expand All @@ -40,12 +39,10 @@ const style = computed(() => {
}
})
const emit = defineEmits(['update:modelValue'])
const drag = ({ detail: x }) => {
active.value = true
const index = Math.round(steps.value.length * (x / 100))
emit('update:modelValue', steps.value[index])
modelValue.value = steps.value[index]
}
const dragend = () => {
Expand All @@ -66,7 +63,6 @@ const classList = computed(() => {
class="form-control-range-slider-bullet"
:class="classList"
:style="style"
aria-live
@drag="drag"
@dragend="dragend"
></a>
Expand Down

0 comments on commit bab312d

Please sign in to comment.