Skip to content

Commit 358e500

Browse files
feat(Scroll): add ScrollToBottom method (#5858)
* feat(Scroll): 添加平滑滚动到底部的功能,用于聊天框和动态条目展示等 * refactor: 精简代码 * doc: 精简代码 * refactor: 移除 init 调用 * test: 增加单元测试 * test: 更新单元测试 --------- Co-authored-by: Argo Zhang <[email protected]>
1 parent 53c934a commit 358e500

File tree

8 files changed

+55
-11
lines changed

8 files changed

+55
-11
lines changed

src/BootstrapBlazor.Server/Components/Samples/Scrolls.razor

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,19 @@
99

1010
<DemoBlock Title="@Localizer["ScrollNormalTitle"]" Introduction="@Localizer["ScrollNormalIntro"]" Name="Normal">
1111
<div class="scroll-demo border">
12-
<Scroll Height="200px">
13-
<div style="height: 400px;">@Localizer["ScrollNormalDescription"]</div>
14-
<div>@Localizer["ScrollNormalBottom"]</div>
12+
<Scroll @ref="_scroll" Height="200px">
13+
<div class="m-1">@Localizer["ScrollNormalDescription"]</div>
14+
<div class="bg-primary" style="height: 100px;"></div>
15+
<div class="bg-secondary" style="height: 100px;"></div>
16+
<div class="bg-warning" style="height: 100px;"></div>
17+
<div class="bg-success" style="height: 100px;"></div>
18+
<div class="bg-info" style="height: 100px;"></div>
19+
<div class="m-1">@Localizer["ScrollNormalBottom"]</div>
1520
</Scroll>
21+
<Divider/>
22+
<div class="m-3">
23+
<Button OnClick="ScrollToBottom" Text="@Localizer["ScrollToBottom"]"></Button>
24+
</div>
1625
</div>
1726
</DemoBlock>
1827

src/BootstrapBlazor.Server/Components/Samples/Scrolls.razor.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,9 @@ private AttributeItem[] GetAttributes() =>
3333
DefaultValue = " — "
3434
}
3535
];
36+
37+
[NotNull]
38+
private Scroll? _scroll = null;
39+
40+
private Task ScrollToBottom() => _scroll.ScrollToBottom();
3641
}

src/BootstrapBlazor.Server/Locales/en-US.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1335,8 +1335,9 @@
13351335
"ScrollTitle": "Scroll",
13361336
"ScrollDescription": "Add scroll bars to components whose height or width exceeds the standard",
13371337
"ScrollTips": "The scroll bar can be rendered only when its element has a fixed height. Its <code>height</code> attribute can be set through the coat element",
1338+
"ScrollToBottom": "Scroll to bottom",
13381339
"ScrollNormalTitle": "Common usage",
1339-
"ScrollNormalIntro": "Add a scroll bar to the component. By setting the height of <code>Height</code> to 200px, the scroll bar appears when the height of the inner child element is 400px",
1340+
"ScrollNormalIntro": "Add a scroll bar to the component. By setting the height of <code>Height</code> to 200px, the scroll bar appears when the height of the inner child element is 500px",
13401341
"Desc1": "Subassembly",
13411342
"Desc2": "Component height",
13421343
"ScrollNormalDescription": "Please scroll the scroll bar on the right",

src/BootstrapBlazor.Server/Locales/zh-CN.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1335,8 +1335,9 @@
13351335
"ScrollTitle": "Scroll 滚动条",
13361336
"ScrollDescription": "给高度或者宽度超标的组件增加滚动条",
13371337
"ScrollTips": "其元素必须拥有固定高度时才可呈现滚动条,可以通过外套元素设置其 <code>height</code> 属性",
1338+
"ScrollToBottom": "滚动到底部",
13381339
"ScrollNormalTitle": "普通用法",
1339-
"ScrollNormalIntro": "给组件增加滚动条,通过设置 <code>Height</code> 高度值为 200px 使内部子元素高度为 400px 时出现滚动条",
1340+
"ScrollNormalIntro": "给组件增加滚动条,通过设置 <code>Height</code> 高度值为 200px 使内部子元素高度为 500px 时出现滚动条",
13401341
"Desc1": "子组件",
13411342
"Desc2": "组件高度",
13421343
"ScrollNormalDescription": "请滚动右侧滚动条",
Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
@namespace BootstrapBlazor.Components
2-
@inherits BootstrapComponentBase
2+
@inherits BootstrapModuleComponentBase
3+
@attribute [BootstrapModuleAutoLoader(AutoInvokeInit = false, AutoInvokeDispose = false)]
34

4-
<div @attributes="AdditionalAttributes" class="@ClassString" style="@StyleString">
5+
<div @attributes="AdditionalAttributes" id="@Id" class="@ClassString" style="@StyleString">
56
@ChildContent
67
</div>

src/BootstrapBlazor/Components/Scroll/Scroll.razor.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,4 +59,9 @@ public partial class Scroll
5959
[Inject]
6060
[NotNull]
6161
private IOptionsMonitor<BootstrapBlazorOptions>? Options { get; set; }
62+
63+
/// <summary>
64+
/// 滚动到底部
65+
/// </summary>
66+
public Task ScrollToBottom() => InvokeVoidAsync("scrollToBottom", Id);
6267
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
export function scrollToBottom(id) {
2+
const el = document.getElementById(id);
3+
const top = el.scrollHeight;
4+
el.scrollTo({
5+
top,
6+
left: 0,
7+
behavior: "smooth"
8+
});
9+
}

test/UnitTest/Components/ScrollTest.cs

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
namespace UnitTest.Components;
77

8-
public class ScrollTest : TestBase
8+
public class ScrollTest : BootstrapBlazorTestBase
99
{
1010
[Fact]
1111
public void Height_Ok()
@@ -31,20 +31,20 @@ public void ScrollWidth_Ok()
3131
builder.Add(a => a.Width, "500px");
3232
});
3333

34-
Assert.Equal("<div class=\"scroll\" style=\"width: 500px; --bb-scroll-width: 5px; --bb-scroll-hover-width: 5px;\"></div>", cut.Markup);
34+
Assert.Contains("style=\"width: 500px; --bb-scroll-width: 5px; --bb-scroll-hover-width: 5px;\"", cut.Markup);
3535

3636
cut.SetParametersAndRender(builder =>
3737
{
3838
builder.Add(a => a.ScrollWidth, 6);
3939
});
40-
Assert.Equal("<div class=\"scroll\" style=\"width: 500px; --bb-scroll-width: 6px; --bb-scroll-hover-width: 5px;\"></div>", cut.Markup);
40+
Assert.Contains("style=\"width: 500px; --bb-scroll-width: 6px; --bb-scroll-hover-width: 5px;\"", cut.Markup);
4141

4242
cut.SetParametersAndRender(builder =>
4343
{
4444
builder.Add(a => a.ScrollWidth, 6);
4545
builder.Add(a => a.ScrollHoverWidth, 12);
4646
});
47-
Assert.Equal("<div class=\"scroll\" style=\"width: 500px; --bb-scroll-width: 6px; --bb-scroll-hover-width: 12px;\"></div>", cut.Markup);
47+
Assert.Contains("style=\"width: 500px; --bb-scroll-width: 6px; --bb-scroll-hover-width: 12px;\"", cut.Markup);
4848
}
4949

5050
[Fact]
@@ -59,4 +59,17 @@ public void ChildContent_Ok()
5959

6060
Assert.Contains("I am scroll", cut.Markup);
6161
}
62+
63+
[Fact]
64+
public async Task ScrollToBottom_Ok()
65+
{
66+
var cut = Context.RenderComponent<Scroll>(builder => builder.Add(a => a.ChildContent, r =>
67+
{
68+
r.OpenElement(0, "div");
69+
r.AddContent(1, "I am scroll");
70+
r.CloseComponent();
71+
}));
72+
73+
await cut.InvokeAsync(() => cut.Instance.ScrollToBottom());
74+
}
6275
}

0 commit comments

Comments
 (0)