Skip to content

Commit bd9bc81

Browse files
committed
refactor: 移动代码到独立类中
1 parent 6ce8e97 commit bd9bc81

File tree

2 files changed

+70
-69
lines changed

2 files changed

+70
-69
lines changed

src/BootstrapBlazor/Components/Select/MultiSelect.razor.js

Lines changed: 0 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -26,65 +26,6 @@ export function init(id, invoke, options) {
2626
popover
2727
}
2828

29-
const onSearch = debounce(async v => {
30-
search.parentElement.classList.add('l');
31-
await invoke.invokeMethodAsync('TriggerOnSearch', v);
32-
search.parentElement.classList.remove('l');
33-
});
34-
35-
const shown = () => {
36-
if (search) {
37-
search.focus();
38-
}
39-
const active = popover.toggleMenu.querySelector('.dropdown-item.active');
40-
if (active) {
41-
scrollIntoView(el, active);
42-
}
43-
}
44-
45-
const keydown = e => {
46-
const menu = popover.toggleMenu;
47-
const key = e.key;
48-
if (key === "Enter" || key === 'NumpadEnter') {
49-
if (popover.isPopover) {
50-
popover.hide();
51-
}
52-
else {
53-
menu.classList.remove('show');
54-
}
55-
const activeItem = menu.querySelector('.active');
56-
let index = indexOf(menu, activeItem)
57-
invoke.invokeMethodAsync(confirmMethodCallback, index)
58-
}
59-
else if (key === 'ArrowUp' || key === 'ArrowDown') {
60-
e.preventDefault();
61-
62-
if (menu.querySelector('.dropdown-virtual')) {
63-
return;
64-
}
65-
66-
const items = [...menu.querySelectorAll('.dropdown-item:not(.disabled)')];
67-
if (items.length > 0) {
68-
let current = menu.querySelector('.active');
69-
if (current !== null) {
70-
current.classList.remove('active');
71-
}
72-
73-
let index = current === null ? -1 : items.indexOf(current);
74-
index = key === 'ArrowUp' ? index - 1 : index + 1;
75-
if (index < 0) {
76-
index = items.length - 1;
77-
}
78-
else if (index > items.length - 1) {
79-
index = 0;
80-
}
81-
current = items[index];
82-
current.classList.add('active');
83-
scrollIntoView(el, current);
84-
}
85-
}
86-
}
87-
8829
Input.composition(search, onSearch);
8930
EventHandler.on(search, 'keydown', keydown);
9031

@@ -182,13 +123,3 @@ export function dispose(id) {
182123
EventHandler.off(search, 'keydown');
183124
}
184125
}
185-
186-
function indexOf(el, element) {
187-
const items = el.querySelectorAll('.dropdown-item')
188-
return Array.prototype.indexOf.call(items, element)
189-
}
190-
191-
const scrollIntoView = (el, item) => {
192-
const behavior = el.getAttribute('data-bb-scroll-behavior') ?? 'smooth';
193-
item.scrollIntoView({ behavior: behavior, block: "nearest", inline: "start" });
194-
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
import { debounce } from "./utility.js"
2+
3+
const onSearch = debounce(async v => {
4+
search.parentElement.classList.add('l');
5+
await invoke.invokeMethodAsync('TriggerOnSearch', v);
6+
search.parentElement.classList.remove('l');
7+
});
8+
9+
const shown = () => {
10+
if (search) {
11+
search.focus();
12+
}
13+
const active = popover.toggleMenu.querySelector('.dropdown-item.active');
14+
if (active) {
15+
scrollIntoView(el, active);
16+
}
17+
}
18+
19+
const keydown = e => {
20+
const menu = popover.toggleMenu;
21+
const key = e.key;
22+
if (key === "Enter" || key === 'NumpadEnter') {
23+
if (popover.isPopover) {
24+
popover.hide();
25+
}
26+
else {
27+
menu.classList.remove('show');
28+
}
29+
const activeItem = menu.querySelector('.active');
30+
let index = indexOf(menu, activeItem)
31+
invoke.invokeMethodAsync(confirmMethodCallback, index)
32+
}
33+
else if (key === 'ArrowUp' || key === 'ArrowDown') {
34+
e.preventDefault();
35+
36+
if (menu.querySelector('.dropdown-virtual')) {
37+
return;
38+
}
39+
40+
const items = [...menu.querySelectorAll('.dropdown-item:not(.disabled)')];
41+
if (items.length > 0) {
42+
let current = menu.querySelector('.active');
43+
if (current !== null) {
44+
current.classList.remove('active');
45+
}
46+
47+
let index = current === null ? -1 : items.indexOf(current);
48+
index = key === 'ArrowUp' ? index - 1 : index + 1;
49+
if (index < 0) {
50+
index = items.length - 1;
51+
}
52+
else if (index > items.length - 1) {
53+
index = 0;
54+
}
55+
current = items[index];
56+
current.classList.add('active');
57+
scrollIntoView(el, current);
58+
}
59+
}
60+
}
61+
62+
const indexOf = (el, element) => {
63+
const items = el.querySelectorAll('.dropdown-item')
64+
return Array.prototype.indexOf.call(items, element)
65+
}
66+
67+
const scrollIntoView = (el, item) => {
68+
const behavior = el.getAttribute('data-bb-scroll-behavior') ?? 'smooth';
69+
item.scrollIntoView({ behavior: behavior, block: "nearest", inline: "start" });
70+
}

0 commit comments

Comments
 (0)