Skip to content

Instantly share code, notes, and snippets.

@Anoesj
Created February 15, 2025 12:55
Show Gist options
  • Save Anoesj/0cc2fc946968bed2f46b29a3a3132bfe to your computer and use it in GitHub Desktop.
Save Anoesj/0cc2fc946968bed2f46b29a3a3132bfe to your computer and use it in GitHub Desktop.
Vue better <input type="number"> behavior
<!--
Vue sets an empty number input to '' by default, because that's
what browsers do. However, me no likey.
Read on here: https://github.com/vuejs/vue/issues/4742
-->
<template>
<input
v-model="model"
type="number"
>
</template>
<script setup lang="ts">
const modelValue = defineModel<number | null>({ required: true });
const model = computed({
get: () => modelValue.value,
set: (value: number | '') => {
modelValue.value = value === '' ? null : value;
},
});
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment