Skip to content

Commit f5f401d

Browse files
authored
fix(Select): use ScrollIntoView function (#5047)
* refactor: 更新滚动问题 * chore: bump version 9.2.5-beta01
1 parent 88fba62 commit f5f401d

File tree

2 files changed

+18
-48
lines changed

2 files changed

+18
-48
lines changed

src/BootstrapBlazor/BootstrapBlazor.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<Project Sdk="Microsoft.NET.Sdk.Razor">
22

33
<PropertyGroup>
4-
<Version>9.2.4</Version>
4+
<Version>9.2.5-beta01</Version>
55
</PropertyGroup>
66

77
<ItemGroup>

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

Lines changed: 17 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,10 @@ export function init(id, invoke, options) {
1818
if (search) {
1919
search.focus();
2020
}
21-
const prev = popover.toggleMenu.querySelector('.dropdown-item.preActive')
22-
if (prev) {
23-
prev.classList.remove('preActive')
21+
const active = popover.toggleMenu.querySelector('.dropdown-item.active');
22+
if (active) {
23+
scrollIntoView(el, active);
2424
}
25-
scrollToActive(popover.toggleMenu, prev)
2625
}
2726

2827
const keydown = e => {
@@ -61,15 +60,19 @@ export function init(id, invoke, options) {
6160
else if (index > items.length - 1) {
6261
index = 0;
6362
}
64-
items[index].classList.add('active');
65-
const top = getTop(menu, index);
66-
const hehavior = el.getAttribute('data-bb-scroll-behavior') ?? 'smooth';
67-
menu.scrollTo({ top: top, left: 0, behavior: hehavior });
63+
current = items[index];
64+
current.classList.add('active');
65+
scrollIntoView(el, current);
6866
}
6967
}
7068
}
7169

72-
EventHandler.on(el, 'shown.bs.dropdown', shown);
70+
if (popover.isPopover) {
71+
EventHandler.on(el, 'shown.bs.popover', shown);
72+
}
73+
else {
74+
EventHandler.on(el, 'shown.bs.dropdown', shown);
75+
}
7376

7477
const input = el.querySelector(`#${id}_input`);
7578
EventHandler.on(input, 'keydown', keydown)
@@ -92,19 +95,6 @@ export function init(id, invoke, options) {
9295
Data.set(id, select);
9396
}
9497

95-
const getTop = (menu, index) => {
96-
const styles = getComputedStyle(menu)
97-
const maxHeight = parseInt(styles.maxHeight) / 2
98-
const itemHeight = getHeight(menu.querySelector('.dropdown-item'))
99-
const height = itemHeight * index
100-
const count = Math.floor(maxHeight / itemHeight);
101-
let top = 0;
102-
if (height > maxHeight) {
103-
top = itemHeight * (index > count ? index - count : index)
104-
}
105-
return top;
106-
}
107-
10898
export function show(id) {
10999
const select = Data.get(id)
110100
if (select) {
@@ -144,32 +134,12 @@ export function dispose(id) {
144134
}
145135
}
146136

147-
function scrollToActive(el, activeItem) {
148-
const virtualEl = el.querySelector('.dropdown-virtual');
149-
150-
activeItem ??= el.querySelector('.dropdown-item.active')
151-
152-
if (activeItem) {
153-
const innerHeight = getInnerHeight(el)
154-
const itemHeight = getHeight(activeItem);
155-
const index = indexOf(el, activeItem)
156-
const margin = itemHeight * index - (innerHeight - itemHeight) / 2;
157-
const hehavior = el.getAttribute('data-bb-scroll-behavior') ?? 'smooth';
158-
159-
const search = el.querySelector('.search');
160-
if (search.classList.contains('show')) {
161-
162-
}
163-
if (margin >= 0) {
164-
el.scrollTo({ top: margin, left: 0, behavior: hehavior });
165-
}
166-
else {
167-
el.scrollTo({ top: margin, left: 0, behavior: hehavior });
168-
}
169-
}
170-
}
171-
172137
function indexOf(el, element) {
173138
const items = el.querySelectorAll('.dropdown-item')
174139
return Array.prototype.indexOf.call(items, element)
175140
}
141+
142+
const scrollIntoView = (el, item) => {
143+
const behavior = el.getAttribute('data-bb-scroll-behavior') ?? 'smooth';
144+
item.scrollIntoView({ behavior: behavior, block: "center", inline: "nearest" });
145+
}

0 commit comments

Comments
 (0)