Skip to content

Commit 04e9a50

Browse files
authored
fix: 🐛 修复Input初始化修改失败的问题 (#814)
1 parent 9b50d6c commit 04e9a50

1 file changed

Lines changed: 15 additions & 14 deletions

File tree

  • src/uni_modules/wot-design-uni/components/wd-input

src/uni_modules/wot-design-uni/components/wd-input/wd-input.vue

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,9 @@
5757
<view v-if="showWordCount" class="wd-input__count">
5858
<text
5959
:class="[
60-
inputValue && String(inputValue).length > 0 ? 'wd-input__count-current' : '',
61-
String(inputValue).length > maxlength! ? 'is-error' : ''
62-
]"
60+
inputValue && String(inputValue).length > 0 ? 'wd-input__count-current' : '',
61+
String(inputValue).length > maxlength! ? 'is-error' : ''
62+
]"
6363
>
6464
{{ String(inputValue).length }}
6565
</text>
@@ -88,7 +88,7 @@ export default {
8888
<script lang="ts" setup>
8989
import wdIcon from '../wd-icon/wd-icon.vue'
9090
import { computed, onBeforeMount, ref, watch } from 'vue'
91-
import { isDef, objToStyle, pause } from '../common/util'
91+
import { isDef, objToStyle, pause, isEqual } from '../common/util'
9292
import { useCell } from '../composables/useCell'
9393
import { FORM_KEY, type FormItemRule } from '../wd-form/types'
9494
import { useParent } from '../composables/useParent'
@@ -115,7 +115,7 @@ const isPwdVisible = ref<boolean>(false)
115115
const clearing = ref<boolean>(false) // 是否正在清空操作,避免重复触发失焦
116116
const focused = ref<boolean>(false) // 控制聚焦
117117
const focusing = ref<boolean>(false) // 当前是否激活状态
118-
const inputValue = ref<string | number>('') // 输入框的值
118+
const inputValue = ref<string | number>(getInitValue()) // 输入框的值
119119
const cell = useCell()
120120
121121
watch(
@@ -130,8 +130,7 @@ watch(
130130
() => props.modelValue,
131131
(newValue) => {
132132
inputValue.value = isDef(newValue) ? String(newValue) : ''
133-
},
134-
{ immediate: true, deep: true }
133+
}
135134
)
136135
137136
const { parent: form } = useParent(FORM_KEY)
@@ -210,14 +209,13 @@ const labelStyle = computed(() => {
210209
: ''
211210
})
212211
213-
onBeforeMount(() => {
214-
initState()
215-
})
216-
217212
// 状态初始化
218-
function initState() {
219-
inputValue.value = formatValue(inputValue.value)
220-
emit('update:modelValue', inputValue.value)
213+
function getInitValue() {
214+
const formatted = formatValue(props.modelValue)
215+
if (!isValueEqual(formatted, props.modelValue)) {
216+
emit('update:modelValue', formatted)
217+
}
218+
return formatted
221219
}
222220
223221
function formatValue(value: string | number) {
@@ -284,6 +282,9 @@ function onClickPrefixIcon() {
284282
function handleClick(event: MouseEvent) {
285283
emit('click', event)
286284
}
285+
function isValueEqual(value1: number | string, value2: number | string) {
286+
return isEqual(String(value1), String(value2))
287+
}
287288
</script>
288289

289290
<style lang="scss" scoped>

0 commit comments

Comments
 (0)