I'm trying to create a numbered input with a max length.
However, when I try to type 1.2345, anything after the decimal doesn't get stored. Upon further inspection, target.value never includes the decimal point despite being a string. Why is that?
<script>
let props = defineProps({
hint: String,
min: Number,
max: Number,
maxLen: Number,
modelValue: Number,
})
let emits = defineEmits({
'update:modelValue': (it: number) => true
})
function onInput(event: Event) {
let target = event.target as HTMLInputElement
if (!target) return
console.log(typeof target.value, target.value) // <<< prints string, 1
target.value = target.value.slice(0, props.maxLen)
// let value = +target.value
// let oldValue = value
// value = Math.min(value, props.max ?? value)
// value = Math.max(value, props.min ?? value)
// u.log(oldValue, value)
// if (oldValue !== value) {
// u.log('update', value)
// target.value = '' + value
// emits('update:modelValue', value)
// }
}
</script>
<template>
<input class="form-control
block
w-full
px-3
py-1.5
text-base
font-normal
text-gray-700
bg-white disabled:bg-gray-100
bg-clip-padding
border border-solid border-gray-300
rounded
transition
ease-in-out
m-0
focus:text-gray-700 focus:bg-white focus:border-blue-600 focus:outline-none
"
type="number"
@input="onInput"
:placeholder="hint" />
</template>