Update slider to composition API (#35)

* Update slider to composition API

* Version bump
This commit is contained in:
Adrian O.V
2023-04-06 20:05:14 -04:00
committed by GitHub
parent 6bdea219bf
commit 0736f372dc
3 changed files with 57 additions and 61 deletions

View File

@@ -1,9 +1,21 @@
# Slider # Slider
<script setup>
import { ref } from "vue";
const value = ref(0)
</script>
<DemoContainer> <DemoContainer>
<Slider :min="1000" :max="10000" :step="1000"/> <Slider v-model="value" :min="1000" :max="10000" :step="1000"/>
</DemoContainer> </DemoContainer>
```vue ```vue
<Slider :min="1000" :max="10000" :step="1000"/> <script setup>
import { ref } from "vue";
const value = ref(0)
</script>
<Slider v-model="value" :min="1000" :max="10000" :step="1000"/>
``` ```

View File

@@ -37,68 +37,52 @@
</div> </div>
</template> </template>
<script> <script setup>
export default { import { ref } from 'vue'
name: 'Slider',
props: { const emit = defineEmits(['update:modelValue'])
value: {
type: Number, const props = defineProps({
default: 0, modelValue: {
}, type: Number,
min: { default: 0,
type: Number,
default: 0,
},
max: {
type: Number,
default: 100,
},
step: {
type: Number,
default: 10,
},
forceStep: {
type: Boolean,
default: true,
},
}, },
emits: ['input'], min: {
data() { type: Number,
return { default: 0,
sliderWidth: 0,
objectPosition: 0,
startOffset: 0,
currentValue: Math.max(this.min, this.value).toString(),
}
}, },
computed: { max: {
inputValueValid: { type: Number,
get() { default: 100,
return this.$refs.value.value
},
set(newValue) {
const parsedValue = parseInt(newValue)
if (parsedValue < this.min) {
this.currentValue = this.min.toString()
} else if (parsedValue > this.max) {
this.currentValue = this.max.toString()
} else if (!parsedValue) {
this.currentValue = this.min.toString()
} else {
this.currentValue = (
parsedValue - (this.forceStep ? parsedValue % this.step : 0)
).toString()
}
this.$refs.value.value = this.currentValue
this.$emit('input', parseInt(this.currentValue))
},
},
}, },
methods: { step: {
onInput(value) { type: Number,
this.inputValueValid = parseInt(value) default: 10,
},
}, },
forceStep: {
type: Boolean,
default: true,
},
})
let currentValue = ref(Math.max(props.min, props.modelValue).toString())
const inputValueValid = (newValue) => {
const parsedValue = parseInt(newValue)
if (parsedValue < props.min) {
currentValue.value = props.min.toString()
} else if (parsedValue > props.max) {
currentValue.value = props.max.toString()
} else if (!parsedValue) {
currentValue.value = props.min.toString()
} else {
currentValue.value = (parsedValue - (props.forceStep ? parsedValue % props.step : 0)).toString()
}
emit('update:modelValue', parseInt(currentValue.value))
}
const onInput = (value) => {
inputValueValid(value)
} }
</script> </script>

View File

@@ -1,7 +1,7 @@
{ {
"name": "omorphia", "name": "omorphia",
"type": "module", "type": "module",
"version": "0.4.3", "version": "0.4.4",
"files": [ "files": [
"dist" "dist"
], ],