mirror of
https://github.com/zoriya/bubbles.git
synced 2026-08-05 04:36:07 +00:00
feat: Introduce paginator options
This commit is contained in:
committed by
Christian Rocha
parent
f25096d1b6
commit
5110925e87
+25
-2
@@ -130,9 +130,12 @@ func (m Model) OnFirstPage() bool {
|
||||
return m.Page == 0
|
||||
}
|
||||
|
||||
// Option is used to set options in New.
|
||||
type Option func(*Model)
|
||||
|
||||
// New creates a new model with defaults.
|
||||
func New() Model {
|
||||
return Model{
|
||||
func New(opts ...Option) Model {
|
||||
m := Model{
|
||||
Type: Arabic,
|
||||
Page: 0,
|
||||
PerPage: 1,
|
||||
@@ -142,6 +145,12 @@ func New() Model {
|
||||
InactiveDot: "○",
|
||||
ArabicFormat: "%d/%d",
|
||||
}
|
||||
|
||||
for _, opt := range opts {
|
||||
opt(&m)
|
||||
}
|
||||
|
||||
return m
|
||||
}
|
||||
|
||||
// NewModel creates a new model with defaults.
|
||||
@@ -149,6 +158,20 @@ func New() Model {
|
||||
// Deprecated: use [New] instead.
|
||||
var NewModel = New
|
||||
|
||||
// WithTotalPages sets the total pages.
|
||||
func WithTotalPages(totalPages int) Option {
|
||||
return func(m *Model) {
|
||||
m.TotalPages = totalPages
|
||||
}
|
||||
}
|
||||
|
||||
// WithPerPage sets the total pages.
|
||||
func WithPerPage(perPage int) Option {
|
||||
return func(m *Model) {
|
||||
m.PerPage = perPage
|
||||
}
|
||||
}
|
||||
|
||||
// Update is the Tea update function which binds keystrokes to pagination.
|
||||
func (m Model) Update(msg tea.Msg) (Model, tea.Cmd) {
|
||||
switch msg := msg.(type) {
|
||||
|
||||
@@ -6,6 +6,32 @@ import (
|
||||
tea "github.com/charmbracelet/bubbletea"
|
||||
)
|
||||
|
||||
func TestNew(t *testing.T) {
|
||||
model := New()
|
||||
|
||||
if model.PerPage != 1 {
|
||||
t.Errorf("PerPage = %d, expected %d", model.PerPage, 1)
|
||||
}
|
||||
if model.TotalPages != 1 {
|
||||
t.Errorf("TotalPages = %d, expected %d", model.TotalPages, 1)
|
||||
}
|
||||
|
||||
perPage := 42
|
||||
totalPages := 42
|
||||
|
||||
model = New(
|
||||
WithPerPage(perPage),
|
||||
WithTotalPages(totalPages),
|
||||
)
|
||||
|
||||
if model.PerPage != perPage {
|
||||
t.Errorf("PerPage = %d, expected %d", model.PerPage, perPage)
|
||||
}
|
||||
if model.TotalPages != totalPages {
|
||||
t.Errorf("TotalPages = %d, expected %d", model.TotalPages, totalPages)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSetTotalPages(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
|
||||
Reference in New Issue
Block a user