Skip to content

Commit 81c5b1e

Browse files
committed
#8 - add page change management
1 parent 018c135 commit 81c5b1e

5 files changed

Lines changed: 72 additions & 2 deletions

File tree

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
@namespace BitBlazor.Components
22

33
<li class="page-item">
4-
<a class="page-link" @attributes="attributes" href="#">@Page</a>
4+
<a class="page-link" @attributes="attributes" href="#" @onclick:preventDefault @onclick="SetPageAsync">@Page</a>
55
</li>

src/BitBlazor/Components/Pagination/BitPageItem.razor.cs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,34 @@
22

33
namespace BitBlazor.Components;
44

5+
/// <summary>
6+
/// Represents an individual page item within a pagination component.
7+
/// </summary>
8+
/// <remarks>
9+
/// This class is typically used as a child of the BitPagination component to display a specific page number.
10+
/// It manages accessibility attributes based on the current page selection.
11+
/// </remarks>
512
public partial class BitPageItem
613
{
714
[CascadingParameter]
815
BitPagination Parent { get; set; } = default!;
916

17+
/// <summary>
18+
/// Gets or sets the current page index for pagination.
19+
/// </summary>
20+
/// <remarks>
21+
/// Setting this property determines which page of data is displayed or processed.
22+
/// Ensure that the value is within the valid range for the data source.
23+
/// </remarks>
1024
[Parameter]
1125
public int Page { get; set; }
1226

1327
private IDictionary<string, object> attributes = new Dictionary<string, object>();
1428

29+
/// <inheritdoc/>
1530
protected override void OnParametersSet()
1631
{
17-
if (Page == Parent.Page)
32+
if (Page == Parent.CurrentPage)
1833
{
1934
attributes["aria-current"] = "page";
2035
}
@@ -23,4 +38,6 @@ protected override void OnParametersSet()
2338
attributes.Remove("aria-current");
2439
}
2540
}
41+
42+
private Task SetPageAsync() => Parent.ChangePageAsync(Page);
2643
}

src/BitBlazor/Components/Pagination/BitPagination.razor.cs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,28 @@ public partial class BitPagination : BitComponentBase
7878
[Parameter]
7979
public PaginationAlignment Alignment { get; set; } = PaginationAlignment.Left;
8080

81+
internal int CurrentPage { get; private set; }
82+
83+
/// <inheritdoc/>
84+
protected override void OnParametersSet()
85+
{
86+
base.OnParametersSet();
87+
CurrentPage = Page;
88+
}
89+
90+
internal async Task ChangePageAsync(int page)
91+
{
92+
if (CurrentPage == page)
93+
{
94+
return;
95+
}
96+
97+
CurrentPage = page;
98+
await PageChanged.InvokeAsync(page);
99+
100+
StateHasChanged();
101+
}
102+
81103
private string ComputeContainerCssClass()
82104
{
83105
var builder = new CssClassBuilder("pagination-wrapper");

stories/BitBlazor.Stories/Components/Stories/Components/BitPagination.stories.razor

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
<Arg For="_ => _.Page" Value="1" />
1515
<Arg For="_ => _.NumberOfPages" Value="3" />
1616
<Arg For="_ => _.Description" Value="@("Page navigation")" />
17+
<Arg For="_ => _.Alignment" Value="PaginationAlignment.Left" />
1718
</Arguments>
1819
<Template>
1920
<div class="px-2 py-2">
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
using BitBlazor.Components;
2+
using Bunit;
3+
using Newtonsoft.Json.Linq;
4+
5+
namespace BitBlazor.Test.Components.Pagination;
6+
7+
public class BitPaginationTest
8+
{
9+
[Fact]
10+
public void BitPagination_Should_Change_Current_Page_When_Page_Link_Is_Clicked()
11+
{
12+
using var ctx = new BunitContext();
13+
14+
int page = 1;
15+
16+
var component = ctx.Render<BitPagination>(
17+
parameters => parameters
18+
.Add(p => p.NumberOfPages, 3)
19+
.Add(p => p.Description, "pagination")
20+
.Bind(p => p.Page, page, v => page = v));
21+
22+
var pageItem = component.FindComponents<BitPageItem>().First(p => p.Instance.Page == 2);
23+
var pageLink = pageItem.Find(".page-item > .page-link");
24+
25+
pageLink.Click();
26+
27+
Assert.Equal(2, page);
28+
Assert.Equal("page", pageLink.GetAttribute("aria-current"));
29+
}
30+
}

0 commit comments

Comments
 (0)