I'm primarily a React developer but I'm dipping my toes back into Vue again.
I'm making an app which allows you to convert your running pace between minutes per km and minutes per mile. The minute and second values for each unit are set in a parent component and passed down into child input components. I would like to prevent users from entering values higher than 59 into the number inputs but the v-model functionality continues to allow users to type beyond that.
I'm probably approaching this from a React mindset, so any help would be greatly appreciated!
App.vue
<template>
<div>
<NumberInput
:id="'kmMins'"
:unit="'km'"
v-model="kmMins"
/>
</div>
</template>
<script>
import NumberInput from "./NumberInput.vue";
export default {
name: "App",
data() {
return {
kmMins: 0,
}
},
components: {
NumberInput
}
}
</script>
NumberInput.vue
<template>
<input
type="number"
placeholder="00"
:value="modelValue"
@input="$emit('update:modelValue', sanitize($event))"
/>
</template>
<script>
export default {
name: "NumberInput",
props: ['id', 'unit', 'modelValue'],
methods: {
sanitize(e) {
const { value } = e.target;
if (!Number.isNaN(value) && value < 60) {
return value;
}
return this.modelValue;
}
}
}
</script>