Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions src/BootstrapBlazor.Server/Components/Samples/Scrolls.razor
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,19 @@

<DemoBlock Title="@Localizer["ScrollNormalTitle"]" Introduction="@Localizer["ScrollNormalIntro"]" Name="Normal">
<div class="scroll-demo border">
<Scroll Height="200px">
<div style="height: 400px;">@Localizer["ScrollNormalDescription"]</div>
<div>@Localizer["ScrollNormalBottom"]</div>
<Scroll @ref="_scroll" Height="200px">
<div class="m-1">@Localizer["ScrollNormalDescription"]</div>
<div class="bg-primary" style="height: 100px;"></div>
<div class="bg-secondary" style="height: 100px;"></div>
<div class="bg-warning" style="height: 100px;"></div>
<div class="bg-success" style="height: 100px;"></div>
<div class="bg-info" style="height: 100px;"></div>
<div class="m-1">@Localizer["ScrollNormalBottom"]</div>
</Scroll>
<Divider/>
<div class="m-3">
<Button OnClick="ScrollToBottom" Text="@Localizer["ScrollToBottom"]"></Button>
</div>
</div>
</DemoBlock>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,9 @@ private AttributeItem[] GetAttributes() =>
DefaultValue = " — "
}
];

[NotNull]
private Scroll? _scroll = null;

private Task ScrollToBottom() => _scroll.ScrollToBottom();
}
3 changes: 2 additions & 1 deletion src/BootstrapBlazor.Server/Locales/en-US.json
Original file line number Diff line number Diff line change
Expand Up @@ -1335,8 +1335,9 @@
"ScrollTitle": "Scroll",
"ScrollDescription": "Add scroll bars to components whose height or width exceeds the standard",
"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",
"ScrollToBottom": "Scroll to bottom",
"ScrollNormalTitle": "Common usage",
"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",
"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",
"Desc1": "Subassembly",
"Desc2": "Component height",
"ScrollNormalDescription": "Please scroll the scroll bar on the right",
Expand Down
3 changes: 2 additions & 1 deletion src/BootstrapBlazor.Server/Locales/zh-CN.json
Original file line number Diff line number Diff line change
Expand Up @@ -1335,8 +1335,9 @@
"ScrollTitle": "Scroll 滚动条",
"ScrollDescription": "给高度或者宽度超标的组件增加滚动条",
"ScrollTips": "其元素必须拥有固定高度时才可呈现滚动条,可以通过外套元素设置其 <code>height</code> 属性",
"ScrollToBottom": "滚动到底部",
"ScrollNormalTitle": "普通用法",
"ScrollNormalIntro": "给组件增加滚动条,通过设置 <code>Height</code> 高度值为 200px 使内部子元素高度为 400px 时出现滚动条",
"ScrollNormalIntro": "给组件增加滚动条,通过设置 <code>Height</code> 高度值为 200px 使内部子元素高度为 500px 时出现滚动条",
"Desc1": "子组件",
"Desc2": "组件高度",
"ScrollNormalDescription": "请滚动右侧滚动条",
Expand Down
5 changes: 3 additions & 2 deletions src/BootstrapBlazor/Components/Scroll/Scroll.razor
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
@namespace BootstrapBlazor.Components
@inherits BootstrapComponentBase
@inherits BootstrapModuleComponentBase
@attribute [BootstrapModuleAutoLoader(AutoInvokeInit = false, AutoInvokeDispose = false)]

<div @attributes="AdditionalAttributes" class="@ClassString" style="@StyleString">
<div @attributes="AdditionalAttributes" id="@Id" class="@ClassString" style="@StyleString">
@ChildContent
</div>
5 changes: 5 additions & 0 deletions src/BootstrapBlazor/Components/Scroll/Scroll.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,9 @@ public partial class Scroll
[Inject]
[NotNull]
private IOptionsMonitor<BootstrapBlazorOptions>? Options { get; set; }

/// <summary>
/// 滚动到底部
/// </summary>
public Task ScrollToBottom() => InvokeVoidAsync("scrollToBottom", Id);
}
9 changes: 9 additions & 0 deletions src/BootstrapBlazor/Components/Scroll/Scroll.razor.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export function scrollToBottom(id) {
const el = document.getElementById(id);
const top = el.scrollHeight;
el.scrollTo({
top,
left: 0,
behavior: "smooth"
});
}
21 changes: 17 additions & 4 deletions test/UnitTest/Components/ScrollTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

namespace UnitTest.Components;

public class ScrollTest : TestBase
public class ScrollTest : BootstrapBlazorTestBase
{
[Fact]
public void Height_Ok()
Expand All @@ -31,20 +31,20 @@ public void ScrollWidth_Ok()
builder.Add(a => a.Width, "500px");
});

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

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

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

[Fact]
Expand All @@ -59,4 +59,17 @@ public void ChildContent_Ok()

Assert.Contains("I am scroll", cut.Markup);
}

[Fact]
public async Task ScrollToBottom_Ok()
{
var cut = Context.RenderComponent<Scroll>(builder => builder.Add(a => a.ChildContent, r =>
{
r.OpenElement(0, "div");
r.AddContent(1, "I am scroll");
r.CloseComponent();
}));

await cut.InvokeAsync(() => cut.Instance.ScrollToBottom());
}
}