Skip to content

Commit 448b6c0

Browse files
committed
fix: prevent non-numeric input on firefox and Safari
1 parent 23ef956 commit 448b6c0

File tree

1 file changed

+11
-6
lines changed

1 file changed

+11
-6
lines changed

src/components/CvNumberInput/CvNumberInput.vue

+11-6
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@
9898
</template>
9999

100100
<script setup>
101-
import { computed, ref, useSlots } from 'vue';
101+
import { computed, ref, useSlots, nextTick } from 'vue';
102102
import Add16 from '@carbon/icons-vue/es/add/16';
103103
import Subtract16 from '@carbon/icons-vue/es/subtract/16';
104104
import WarningFilled16 from '@carbon/icons-vue/es/warning--filled/16';
@@ -114,7 +114,7 @@ const props = defineProps({
114114
disabled: Boolean,
115115
modelValue: {
116116
type: [String, Number],
117-
default: '',
117+
default: undefined,
118118
},
119119
formItem: {
120120
type: Boolean,
@@ -190,18 +190,23 @@ const modelValueType = typeof props.modelValue;
190190
const valuesToNaN = ['', null, undefined];
191191
192192
const formatInputValueType = value => {
193-
if (modelValueType === 'string') return value.toString();
193+
if (modelValueType === 'string') return Number(value).toString();
194194
// According to Vue2 component if there is no value NaN is emitted
195195
return valuesToNaN.includes(value) ? NaN : Number(value);
196196
};
197197
198+
const displayValue = ref(props.modelValue || props.min);
198199
const internalValue = computed({
199200
get() {
200-
return props.modelValue;
201+
return props.modelValue || displayValue.value;
201202
},
202203
set(value) {
203-
const valueFormatted = formatInputValueType(value);
204-
onInput(valueFormatted);
204+
displayValue.value = formatInputValueType(value);
205+
if (Number.isNaN(displayValue.value) || displayValue.value === 'NaN') {
206+
displayValue.value = 0;
207+
nextTick(() => (displayValue.value = NaN));
208+
}
209+
onInput(displayValue.value);
205210
},
206211
});
207212

0 commit comments

Comments
 (0)