Skip to content

Commit 33b5565

Browse files
fix: 🐛 修复 Input、Textarea、Search 组件设置清空后不聚焦时无法触发失焦事件的问题 (#1046)
1 parent 8177978 commit 33b5565

3 files changed

Lines changed: 38 additions & 52 deletions

File tree

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -236,10 +236,10 @@ function togglePwdVisible() {
236236
isPwdVisible.value = !isPwdVisible.value
237237
}
238238
async function handleClear() {
239-
clearing.value = true
240239
focusing.value = false
241240
inputValue.value = ''
242241
if (props.focusWhenClear) {
242+
clearing.value = true
243243
focused.value = false
244244
}
245245
await pause()

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

Lines changed: 36 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -7,24 +7,24 @@
77
<wd-icon name="search" custom-class="wd-search__search-icon"></wd-icon>
88
<text :class="`wd-search__placeholder-txt ${placeholderClass}`">{{ placeholder || translate('search') }}</text>
99
</view>
10-
<wd-icon v-if="showInput || str || placeholderLeft" name="search" custom-class="wd-search__search-left-icon"></wd-icon>
10+
<wd-icon v-if="showInput || inputValue || placeholderLeft" name="search" custom-class="wd-search__search-left-icon"></wd-icon>
1111
<input
12-
v-if="showInput || str || placeholderLeft"
12+
v-if="showInput || inputValue || placeholderLeft"
1313
:placeholder="placeholder || translate('search')"
1414
:placeholder-class="`wd-search__placeholder-txt ${placeholderClass}`"
1515
:placeholder-style="placeholderStyle"
1616
confirm-type="search"
17-
v-model="str"
17+
v-model="inputValue"
1818
:class="['wd-search__input', customInputClass]"
19-
@focus="searchFocus"
20-
@input="inputValue"
21-
@blur="searchBlur"
22-
@confirm="search"
19+
@focus="handleFocus"
20+
@input="handleInput"
21+
@blur="handleBlur"
22+
@confirm="handleConfirm"
2323
:disabled="disabled"
2424
:maxlength="maxlength"
2525
:focus="isFocused"
2626
/>
27-
<wd-icon v-if="str" custom-class="wd-search__clear wd-search__clear-icon" name="error-fill" @click="clearSearch" />
27+
<wd-icon v-if="inputValue" custom-class="wd-search__clear wd-search__clear-icon" name="error-fill" @click="handleClear" />
2828
</view>
2929
</view>
3030

@@ -61,14 +61,14 @@ const { translate } = useTranslate('search')
6161
6262
const isFocused = ref<boolean>(false) // 是否聚焦中
6363
const showInput = ref<boolean>(false) // 是否显示输入框 用于实现聚焦的hack
64-
const str = ref('')
64+
const inputValue = ref<string>('') // 输入框的值
6565
const showPlaceHolder = ref<boolean>(true)
6666
const clearing = ref<boolean>(false)
6767
6868
watch(
6969
() => props.modelValue,
7070
(newValue) => {
71-
str.value = newValue
71+
inputValue.value = newValue
7272
if (newValue) {
7373
showInput.value = true
7474
}
@@ -98,7 +98,7 @@ const rootClass = computed(() => {
9898
9999
const coverStyle = computed(() => {
100100
const coverStyle: CSSProperties = {
101-
display: str.value === '' && showPlaceHolder.value ? 'flex' : 'none'
101+
display: inputValue.value === '' && showPlaceHolder.value ? 'flex' : 'none'
102102
}
103103
104104
return objToStyle(coverStyle)
@@ -116,27 +116,22 @@ async function closeCover() {
116116
showPlaceHolder.value = false
117117
hackFocus(true)
118118
}
119-
/**
120-
* @description input的input事件handle
121-
* @param value
122-
*/
123-
function inputValue(event: any) {
124-
str.value = event.detail.value
119+
120+
function handleInput(event: any) {
121+
inputValue.value = event.detail.value
125122
emit('update:modelValue', event.detail.value)
126123
emit('change', {
127124
value: event.detail.value
128125
})
129126
}
130-
/**
131-
* @description 点击清空icon的handle
132-
*/
133-
async function clearSearch() {
134-
str.value = ''
135-
clearing.value = true
127+
128+
async function handleClear() {
129+
inputValue.value = ''
136130
if (props.focusWhenClear) {
131+
clearing.value = true
137132
isFocused.value = false
138133
}
139-
await pause(100)
134+
await pause()
140135
if (props.focusWhenClear) {
141136
showPlaceHolder.value = false
142137
hackFocus(true)
@@ -150,49 +145,40 @@ async function clearSearch() {
150145
emit('update:modelValue', '')
151146
emit('clear')
152147
}
153-
/**
154-
* @description 点击搜索按钮时的handle
155-
* @param value
156-
*/
157-
function search({ detail: { value } }: any) {
148+
149+
function handleConfirm({ detail: { value } }: any) {
158150
// 组件触发search事件
159151
emit('search', {
160152
value
161153
})
162154
}
163-
/**
164-
* @description 输入框聚焦时的handle
165-
*/
166-
function searchFocus() {
167-
if (clearing.value) {
168-
clearing.value = false
169-
return
170-
}
155+
156+
function handleFocus() {
171157
showPlaceHolder.value = false
172158
emit('focus', {
173-
value: str.value
159+
value: inputValue.value
174160
})
175161
}
176-
/**
177-
* @description 输入框失焦的handle
178-
*/
179-
function searchBlur() {
180-
if (clearing.value) return
162+
163+
async function handleBlur() {
164+
// 等待150毫秒,clear执行完毕
165+
await pause(150)
166+
if (clearing.value) {
167+
clearing.value = false
168+
return
169+
}
181170
// 组件触发blur事件
182-
showPlaceHolder.value = !str.value
171+
showPlaceHolder.value = !inputValue.value
183172
showInput.value = !showPlaceHolder.value
184173
isFocused.value = false
185174
emit('blur', {
186-
value: str.value
175+
value: inputValue.value
187176
})
188177
}
189-
/**
190-
* @description 点击取消搜索按钮的handle
191-
*/
178+
192179
function handleCancel() {
193-
// 组件触发cancel事件
194180
emit('cancel', {
195-
value: str.value
181+
value: inputValue.value
196182
})
197183
}
198184
</script>

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -233,10 +233,10 @@ function formatValue(value: string | number) {
233233
}
234234
235235
async function handleClear() {
236-
clearing.value = true
237236
focusing.value = false
238237
inputValue.value = ''
239238
if (props.focusWhenClear) {
239+
clearing.value = true
240240
focused.value = false
241241
}
242242
await pause()

0 commit comments

Comments
 (0)