From 765b0aa88cf0178391415c37ecce6108d5903af1 Mon Sep 17 00:00:00 2001 From: Yuchao Wu Date: Wed, 24 Jul 2024 21:35:47 +1000 Subject: [PATCH] refactor: improve logic --- .../src/labs/VNumberInput/VNumberInput.tsx | 24 ++++++++----------- 1 file changed, 10 insertions(+), 14 deletions(-) diff --git a/packages/vuetify/src/labs/VNumberInput/VNumberInput.tsx b/packages/vuetify/src/labs/VNumberInput/VNumberInput.tsx index 15d7ec5e168..ac623b641cd 100644 --- a/packages/vuetify/src/labs/VNumberInput/VNumberInput.tsx +++ b/packages/vuetify/src/labs/VNumberInput/VNumberInput.tsx @@ -64,17 +64,16 @@ export const VNumberInput = genericComponent()({ 'update:modelValue': (val: number) => true, }, - setup (props, { attrs, emit, slots }) { + setup (props, { slots }) { const _model = useProxiedModel(props, 'modelValue') const model = computed({ get () { return _model.value }, - set (val) { - _model.value = clamp(val, props.min, props.max) - }, + set (val) {}, }) + const vTextFieldRef = ref() const stepDecimals = computed(() => getDecimals(props.step)) @@ -111,7 +110,7 @@ export const VNumberInput = genericComponent()({ onMounted(() => { if (!props.readonly && !props.disabled) { - model.value = clamp(model.value, props.min, props.max) + convertInputStrToNum() } }) @@ -124,9 +123,9 @@ export const VNumberInput = genericComponent()({ const decimals = Math.max(modelDecimals.value, stepDecimals.value) if (increment) { - if (canIncrease.value) model.value = +(((model.value + props.step).toFixed(decimals))) + if (canIncrease.value) _model.value = +(((_model.value + props.step).toFixed(decimals))) } else { - if (canDecrease.value) model.value = +(((model.value - props.step).toFixed(decimals))) + if (canDecrease.value) _model.value = +(((_model.value - props.step).toFixed(decimals))) } } @@ -175,16 +174,13 @@ export const VNumberInput = genericComponent()({ } function onChange () { - convertToNum() + convertInputStrToNum() } - function convertToNum () { + function convertInputStrToNum () { const inputVal = vTextFieldRef.value.value - if (isNaN(+(inputVal))) { - model.value = inputVal - } else { - if (controlsDisabled.value) return - model.value = +(inputVal) + if (!isNaN(+(inputVal))) { + _model.value = clamp(+(inputVal), props.min, props.max) } }