Skip to content

Commit 0c19d83

Browse files
authored
refactor(AutoComplete): redesign blur logic (#5499)
* refactor: 重构销毁方法位置 * refactor: 增加 triggerBlur 方法 * refactor: 增加 menu 点击事件
1 parent 98115a9 commit 0c19d83

File tree

1 file changed

+19
-11
lines changed

1 file changed

+19
-11
lines changed

src/BootstrapBlazor/Components/AutoComplete/AutoComplete.razor.js

+19-11
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,16 @@ export function init(id, invoke) {
3333
})
3434
}
3535

36-
EventHandler.on(input, 'blur', e => {
36+
ac.triggerBlur = () => {
3737
el.classList.remove('show');
3838
const triggerBlur = input.getAttribute('data-bb-blur') === 'true';
3939
if (triggerBlur) {
4040
invoke.invokeMethodAsync('TriggerBlur');
4141
}
42+
}
43+
44+
EventHandler.on(menu, 'click', '.dropdown-item', e => {
45+
ac.triggerBlur();
4246
});
4347

4448
EventHandler.on(input, 'focus', e => {
@@ -81,7 +85,11 @@ export function init(id, invoke) {
8185

8286
const el = a.querySelector('[data-bs-toggle="bb.dropdown"]');
8387
if (el === null) {
84-
a.classList.remove('show');
88+
const id = a.getAttribute('id');
89+
const d = Data.get(id);
90+
if (d) {
91+
d.triggerBlur();
92+
}
8593
}
8694
});
8795
}
@@ -99,7 +107,7 @@ const handlerKeyup = (ac, e) => {
99107
const current = menu.querySelector('.active');
100108
if (current !== null) {
101109
current.click();
102-
input.blur();
110+
ac.triggerBlur();
103111
}
104112
invoke.invokeMethodAsync('EnterCallback', input.value);
105113
}
@@ -108,7 +116,7 @@ const handlerKeyup = (ac, e) => {
108116
const skipEsc = el.getAttribute('data-bb-skip-esc') === 'true';
109117
if (skipEsc === false) {
110118
invoke.invokeMethodAsync('EscCallback');
111-
input.blur();
119+
ac.triggerBlur();
112120
}
113121
}
114122
else if (key === 'ArrowUp' || key === 'ArrowDown') {
@@ -149,7 +157,7 @@ export function dispose(id) {
149157
Data.remove(id)
150158

151159
if (ac) {
152-
const { popover, input } = ac;
160+
const { popover, input, menu } = ac;
153161
if (popover) {
154162
Popover.dispose(popover)
155163
if (input) {
@@ -158,14 +166,14 @@ export function dispose(id) {
158166
}
159167
EventHandler.off(input, 'change');
160168
EventHandler.off(input, 'keyup');
161-
EventHandler.off(input, 'blur');
169+
EventHandler.off(menu, 'click');
162170
Input.dispose(input);
163-
}
164171

165-
const { AutoComplete } = window.BootstrapBlazor;
166-
AutoComplete.dispose(id, () => {
167-
EventHandler.off(document, 'click', ac.closePopover);
168-
});
172+
const { AutoComplete } = window.BootstrapBlazor;
173+
AutoComplete.dispose(id, () => {
174+
EventHandler.off(document, 'click', ac.closePopover);
175+
});
176+
}
169177
}
170178

171179
const scrollIntoView = (el, item) => {

0 commit comments

Comments
 (0)