Created more components
This commit is contained in:
@@ -0,0 +1,49 @@
|
||||
namespace Htmx.ApiDemo.Templates.Components;
|
||||
|
||||
/// <summary>
|
||||
/// shadcn-style Pagination. Generates prev/next and page-number buttons.
|
||||
/// urlPattern: format string where {0} is replaced by the page number, e.g. "/items?page={0}"
|
||||
/// </summary>
|
||||
public sealed class Pagination : PaginationBase
|
||||
{
|
||||
private const string BtnBase =
|
||||
"inline-flex items-center justify-center rounded-md border border-input bg-background " +
|
||||
"px-3 h-9 text-sm font-medium ring-offset-background transition-colors " +
|
||||
"hover:bg-accent hover:text-accent-foreground " +
|
||||
"focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 " +
|
||||
"disabled:pointer-events-none disabled:opacity-50";
|
||||
|
||||
private const string ActiveBtn =
|
||||
"inline-flex items-center justify-center rounded-md bg-primary text-primary-foreground " +
|
||||
"px-3 h-9 text-sm font-medium ring-offset-background " +
|
||||
"focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2";
|
||||
|
||||
private readonly byte[] _prevData;
|
||||
private readonly byte[] _pagesData;
|
||||
private readonly byte[] _nextData;
|
||||
|
||||
public Pagination(int current, int total, string urlPattern = "?page={0}")
|
||||
{
|
||||
_prevData = current <= 1
|
||||
? $"""<button type="button" class="{BtnBase}" disabled aria-label="Previous page">‹</button>""".ToUtf8Bytes()
|
||||
: $"""<a href="{string.Format(urlPattern, current - 1)}" class="{BtnBase}" aria-label="Previous page">‹</a>""".ToUtf8Bytes();
|
||||
|
||||
_nextData = current >= total
|
||||
? $"""<button type="button" class="{BtnBase}" disabled aria-label="Next page">›</button>""".ToUtf8Bytes()
|
||||
: $"""<a href="{string.Format(urlPattern, current + 1)}" class="{BtnBase}" aria-label="Next page">›</a>""".ToUtf8Bytes();
|
||||
|
||||
var sb = new System.Text.StringBuilder();
|
||||
for (int p = 1; p <= total; p++)
|
||||
{
|
||||
if (p == current)
|
||||
sb.Append($"""<span class="{ActiveBtn}" aria-current="page">{p}</span>""");
|
||||
else
|
||||
sb.Append($"""<a href="{string.Format(urlPattern, p)}" class="{BtnBase}">{p}</a>""");
|
||||
}
|
||||
_pagesData = sb.ToString().ToUtf8Bytes();
|
||||
}
|
||||
|
||||
protected override void RenderPrev(HtmxRenderContext ctx) => ctx.Writer.WriteUtf8(_prevData);
|
||||
protected override void RenderPages(HtmxRenderContext ctx) => ctx.Writer.WriteUtf8(_pagesData);
|
||||
protected override void RenderNext(HtmxRenderContext ctx) => ctx.Writer.WriteUtf8(_nextData);
|
||||
}
|
||||
Reference in New Issue
Block a user