-
-
Notifications
You must be signed in to change notification settings - Fork 16
Description
We developed our custom numeric input based on this library.
Sadly we're facing a limitation, @input:model-value
doesn’t allow to start with -
because it’s technically not a number until you put something behind the minus (thus it thinks it's null
).
Here is the code of the custom component:
<template>
<number
class="number-input"
:model-value="value"
@input:model-value="onValueUpdated"
decimal=","
:separator="props.separator"
:precision="props.precision"
:min="props.min"
:max="props.max"
:suffix="props.suffix"
:disabled="props.disabled"
:placeholder="props.placeholder"
:null-value="null"
/>
</template>
<script setup lang="ts">
import { number } from '@coders-tm/vue-number-format';
import { isNil } from 'lodash-es';
import { watch, ref } from 'vue';
interface Props {
modelValue?: number | string | null;
suffix?: string;
precision?: number;
separator?: string;
min?: number;
max?: number;
placeholder?: string;
disabled?: boolean;
}
interface Emits {
(e: 'update:modelValue', value?: number): void;
}
const props = withDefaults(defineProps<Props>(), { precision: 2, separator: '.' });
const emit = defineEmits<Emits>();
const value = ref(props.modelValue);
watch(
() => props.modelValue,
val => {
value.value = val;
},
);
const onValueUpdated = val => {
if (!isNil(props.max) && val > props.max) {
emit('update:modelValue', props.max);
} else if (!isNil(props.min) && val < props.min) {
emit('update:modelValue', props.min);
} else {
const adjustedVal = typeof val === 'string' ? (val.length > 0 ? parseFloat(val) : null) : val;
emit('update:modelValue', adjustedVal);
}
};
</script>
I found a trick to workaround for our users for the time being but it's less than ideal.
The trick is to input the number as a positive number and hit -
at the end to make it negative => http://recordit.co/6kNvHKAnBs
Would you have any idea/proposal to fix this usecase?
I already tried listening to keydown event for minus key and tryed to work with that but without success.
Perhaps you have an idea how this could be solved with or without change to the library itself 🧑🎓