diff --git a/packages/uni-components/src/helpers/useField.ts b/packages/uni-components/src/helpers/useField.ts index 47f2fe923a985dd88d96e5df0d0ba2fb19b12eab..796a5802e23a0c3cd322e0cf42cf80defa0a2578 100644 --- a/packages/uni-components/src/helpers/useField.ts +++ b/packages/uni-components/src/helpers/useField.ts @@ -52,7 +52,10 @@ const UniViewJSBridgeSubscribe = function () { const FOCUS_DELAY = 200 let startTime: number -function getValueString(value: any) { +function getValueString(value: any, type: string) { + if (type === 'number' && isNaN(Number(value))) { + value = '' + } return value === null ? '' : String(value) } @@ -185,7 +188,9 @@ function useBase( var maxlength = Number(props.maxlength) return isNaN(maxlength) ? 140 : maxlength }) - const value = getValueString(props.modelValue) || getValueString(props.value) + const value = + getValueString(props.modelValue, props.type) || + getValueString(props.value, props.type) const state: State = reactive({ value, valueOrigin: value, @@ -218,7 +223,7 @@ function useValueSync( trigger: CustomEventTrigger ) { const valueChangeFn = debounce((val: any) => { - state.value = getValueString(val) + state.value = getValueString(val, props.type) }, 100) watch(() => props.modelValue, valueChangeFn) watch(() => props.value, valueChangeFn) diff --git a/packages/uni-components/src/vue/input/index.tsx b/packages/uni-components/src/vue/input/index.tsx index f3b6a82dd2d5c70d6bc82e2a3279ca189e656133..d83e443ac454ee8c7573a2caeab2b526db482c48 100644 --- a/packages/uni-components/src/vue/input/index.tsx +++ b/packages/uni-components/src/vue/input/index.tsx @@ -1,5 +1,5 @@ import { extend, hyphenate } from '@vue/shared' -import { Ref, ref, computed } from 'vue' +import { Ref, ref, computed, watch } from 'vue' import { defineBuiltInComponent } from '../../helpers/component' import { props as fieldProps, @@ -107,6 +107,14 @@ export default /*#__PURE__*/ defineBuiltInComponent({ } } }) + watch( + () => state.value, + (value) => { + if (props.type === 'number' && !(cache.value === '-' && value === '')) { + cache.value = value + } + } + ) const NUMBER_TYPES = ['number', 'digit'] const step = computed(() => NUMBER_TYPES.includes(props.type) ? '0.000000000000000001' : ''