-
Notifications
You must be signed in to change notification settings - Fork 9.9k
/
Paginator.razor.cs
52 lines (41 loc) · 1.99 KB
/
Paginator.razor.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using Microsoft.AspNetCore.Components.QuickGrid.Infrastructure;
namespace Microsoft.AspNetCore.Components.QuickGrid;
/// <summary>
/// A component that provides a user interface for <see cref="PaginationState"/>.
/// </summary>
public partial class Paginator : IDisposable
{
private readonly EventCallbackSubscriber<PaginationState> _totalItemCountChanged;
/// <summary>
/// Specifies the associated <see cref="PaginationState"/>. This parameter is required.
/// </summary>
[Parameter, EditorRequired] public PaginationState State { get; set; } = default!;
/// <summary>
/// Optionally supplies a template for rendering the page count summary.
/// </summary>
[Parameter] public RenderFragment? SummaryTemplate { get; set; }
/// <summary>
/// Constructs an instance of <see cref="Paginator" />.
/// </summary>
public Paginator()
{
// The "total item count" handler doesn't need to do anything except cause this component to re-render
_totalItemCountChanged = new(new EventCallback<PaginationState>(this, null));
}
private Task GoFirstAsync() => GoToPageAsync(0);
private Task GoPreviousAsync() => GoToPageAsync(State.CurrentPageIndex - 1);
private Task GoNextAsync() => GoToPageAsync(State.CurrentPageIndex + 1);
private Task GoLastAsync() => GoToPageAsync(State.LastPageIndex.GetValueOrDefault(0));
private bool CanGoBack => State.CurrentPageIndex > 0;
private bool CanGoForwards => State.CurrentPageIndex < State.LastPageIndex;
private Task GoToPageAsync(int pageIndex)
=> State.SetCurrentPageIndexAsync(pageIndex);
/// <inheritdoc />
protected override void OnParametersSet()
=> _totalItemCountChanged.SubscribeOrMove(State.TotalItemCountChangedSubscribable);
/// <inheritdoc />
public void Dispose()
=> _totalItemCountChanged.Dispose();
}