Skip to content

Commit 1b77acc

Browse files
committed
调整input组件
1 parent 1c5fb4a commit 1b77acc

File tree

1 file changed

+14
-8
lines changed

1 file changed

+14
-8
lines changed

packages/input/src/index.vue

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@
123123
onUpdated,
124124
getCurrentInstance,
125125
ComponentInternalInstance,
126-
toRefs
126+
toRefs, ref
127127
} from 'vue'
128128
import Migrating from '@/mixins/migrating'
129129
// @ts-ignore
@@ -212,6 +212,7 @@
212212
attrs,
213213
slots
214214
})
215+
const inputValue = ref(props.modelValue)
215216
const emitter: Emitter = mitt()
216217
const elForm = inject('elForm', {}) as elFormInject
217218
const elFormItem = inject('elFormItem', {}) as elFormItemInject
@@ -282,7 +283,11 @@
282283
return isWordLimitVisible &&
283284
(textLength > upperLimit)
284285
})
285-
286+
const finalValue = computed(() => {
287+
const isNumber = !!(attrs.modelModifiers as anyObject)?.number
288+
const value = parseFloat(String(inputValue.value))
289+
return isNumber ? isNaN(value) ? '' : value : inputValue.value
290+
})
286291
watch(() => props.modelValue, (val) => {
287292
nextTick(resizeTextarea)
288293
if (props.validateEvent) {
@@ -367,6 +372,10 @@
367372
handleInput(event)
368373
}
369374
}
375+
watch(() => finalValue.value, (value: String | Number) => {
376+
emit('update:modelValue', value)
377+
emit('input', value)
378+
})
370379
const handleInput = (event: Event) => {
371380
// should not emit input during composition
372381
// see: https://github.com/ElemeFE/element/issues/10516
@@ -376,16 +385,13 @@
376385
// hack for https://github.com/ElemeFE/element/issues/8548
377386
// should remove the following line when we don't support IE
378387
if (value === nativeInputValue.value) return
379-
const isNumber = !!(attrs.modelModifiers as anyObject)?.number
380-
const finalValue = isNumber ? isNaN(parseFloat(value)) ? '' : parseFloat(value) : value
381-
emit('update:modelValue', finalValue)
382-
emit('input', finalValue)
388+
inputValue.value = value
383389
// ensure native input value is controlled
384390
// see: https://github.com/ElemeFE/element/issues/12850
385391
nextTick(setNativeInputValue)
386392
}
387393
const handleChange = (event: Event) => {
388-
emit('change', (event.target as HTMLInputElement)?.value)
394+
emit('change', finalValue.value)
389395
}
390396
const calcIconOffset = (place: 'suffix' | 'prefix') => {
391397
const $el = instance.vnode.el
@@ -420,6 +426,7 @@
420426
setNativeInputValue()
421427
emit('input', '')
422428
emit('change', '')
429+
console.log('clear')
423430
emit('update:modelValue', '')
424431
emit('clear')
425432
})
@@ -492,7 +499,6 @@
492499
handlePasswordVisible,
493500
getInput,
494501
getSuffixVisible
495-
496502
}
497503
},
498504

0 commit comments

Comments
 (0)