Skip to content

Commit bf955e5

Browse files
committed
refactor: 重构代码支持搜索功能切换
1 parent 9008d69 commit bf955e5

1 file changed

Lines changed: 36 additions & 10 deletions

File tree

src/components/BootstrapBlazor.Region/Components/SelectCity.razor.js

Lines changed: 36 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
1-
import Data from "../../BootstrapBlazor/modules/data.js"
1+
import Data from "../../BootstrapBlazor/modules/data.js"
22
import EventHandler from "../../BootstrapBlazor/modules/event-handler.js"
33
import Input from "../../BootstrapBlazor/modules/input.js"
44
import Popover from "../../BootstrapBlazor/modules/base-popover.js"
55

66
export function init(id, invoke, options) {
7-
const el = document.getElementById(id)
7+
const el = document.getElementById(id);
88
if (el === null) {
99
return
1010
}
11+
1112
const popover = Popover.init(el, {
1213
shownCallback: () => {
1314
if (searchInput != null) {
@@ -16,6 +17,24 @@ export function init(id, invoke, options) {
1617
}
1718
});
1819

20+
const region = { el, invoke, options, popover };
21+
initSearch(region);
22+
Data.set(id, region);
23+
}
24+
25+
export function resetSearch(id, search) {
26+
const region = Data.get(id);
27+
28+
if (search) {
29+
initSearch(region)
30+
}
31+
else {
32+
disposeSearch(region);
33+
}
34+
}
35+
36+
const initSearch = region => {
37+
const { el, invoke, options } = region;
1938
const searchInput = el.querySelector(".search-text");
2039
if (searchInput) {
2140
Input.composition(searchInput, async v => {
@@ -31,7 +50,18 @@ export function init(id, invoke, options) {
3150
});
3251
}
3352

34-
Data.set(id, { el, popover, searchInput, search });
53+
region.searchInput = searchInput;
54+
region.search = search;
55+
}
56+
57+
const disposeSearch = region => {
58+
const { searchInput, search } = region;
59+
if (searchInput) {
60+
Input.dispose(searchInput);
61+
}
62+
if (search) {
63+
EventHandler.off(search, 'click');
64+
}
3565
}
3666

3767
export function hide(id) {
@@ -46,14 +76,10 @@ export function dispose(id) {
4676
const region = Data.get(id)
4777
Data.remove(id)
4878

49-
const { popover, searchInput, search } = region;
79+
const { popover } = region;
5080
if (popover) {
5181
Popover.dispose(popover);
5282
}
53-
if (searchInput) {
54-
Input.dispose(searchInput);
55-
}
56-
if (search) {
57-
EventHandler.off(search, 'click');
58-
}
83+
84+
disposeSearch(region);
5985
}

0 commit comments

Comments
 (0)