pagination.go
| 1 | package views |
| 2 | |
| 3 | import ( |
| 4 | "strconv" |
| 5 | "strings" |
| 6 | |
| 7 | g "maragu.dev/gomponents" |
| 8 | . "maragu.dev/gomponents/html" |
| 9 | ) |
| 10 | |
| 11 | // PageInfo is the pagination state of a list view. |
| 12 | // URLTemplate contains "{page}" as the placeholder for the page number, |
| 13 | // e.g. "/repo/issues?status=open&page={page}". |
| 14 | type PageInfo struct { |
| 15 | Page int |
| 16 | TotalPages int |
| 17 | URLTemplate string |
| 18 | } |
| 19 | |
| 20 | // Pagination renders previous/next links. It renders nothing for one page. |
| 21 | func Pagination(p PageInfo) g.Node { |
| 22 | if p.TotalPages <= 1 { |
| 23 | return nil |
| 24 | } |
| 25 | url := func(n int) string { |
| 26 | return strings.ReplaceAll(p.URLTemplate, "{page}", strconv.Itoa(n)) |
| 27 | } |
| 28 | return Nav(Class("pagination"), Aria("label", "Pagination"), |
| 29 | Div(Class("pagination-prev"), |
| 30 | g.If(p.Page > 1, A(Href(url(p.Page-1)), Class("pagination-btn"), g.Text("← Previous"))), |
| 31 | ), |
| 32 | Span(Class("pagination-info"), |
| 33 | g.Text("Page "+strconv.Itoa(p.Page)+" of "+strconv.Itoa(p.TotalPages))), |
| 34 | Div(Class("pagination-next"), |
| 35 | g.If(p.Page < p.TotalPages, A(Href(url(p.Page+1)), Class("pagination-btn"), g.Text("Next →"))), |
| 36 | ), |
| 37 | ) |
| 38 | } |
| 39 |