charmbracelet / bubbles

TUI components for Bubble Tea 🫧

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

List: pass item styles from Delegate to Item struct.

antonio-leitao opened this issue · comments

The Issue

Current implementation has an Item's style as part of the Delegate and not the Item. This results in all Items being rendered with the same style.

type DefaultDelegate struct {
	ShowDescription bool
	Styles          DefaultItemStyles
	UpdateFunc      func(tea.Msg, *Model) tea.Cmd
	ShortHelpFunc   func() []key.Binding
	FullHelpFunc    func() [][]key.Binding
	height          int
	spacing         int
}

Which makes it very hard to customize specially if you want the view of each Item be defined by some function of its internal state, since the render function for each item is defined in the delegate; Below:

func (d DefaultDelegate) Render(w io.Writer, m Model, index int, item Item){
	var (
		title, desc  string
		matchedRunes []int
		s            = &d.Styles
	)
(...)

        title = s.SelectedTitle.Render(title)
        desc = s.SelectedDesc.Render(desc)

Proposed solution

Should the styles be actually part of the Item struct? This would make customisation easier. Default Delegate simply renders the item according to the model's state and the item state. For example the code above would become something like:

func (d DefaultDelegate) Render(w io.Writer, m Model, index int, item Item){
	var (
		title, desc  string
		matchedRunes []int
	)
(...)
        title = item.Styles.SelectedTitle.Render(title)
        desc = item.Styles.SelectedDesc.Render(desc)

This would enhance customisation and maybe even make it the List more understandable to the user since it will have to deal less with the Delegate.

You can write your own item delegate that picks a different style for each item.