Skip to content

Commit ba724d2

Browse files
committed
changes:
- styling for the list component - added and input component for the options provider
1 parent 23b2832 commit ba724d2

4 files changed

Lines changed: 120 additions & 11 deletions

File tree

internal/tui/components/OptionsProvider/component.go

Lines changed: 42 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,27 @@ package optionsProvider
33
import (
44
"context"
55

6+
"github.com/charmbracelet/bubbles/key"
67
"github.com/charmbracelet/bubbles/list"
78
tea "github.com/charmbracelet/bubbletea"
9+
"github.com/charmbracelet/lipgloss"
10+
"github.com/maniac-en/req/internal/tui/keybinds"
11+
)
12+
13+
type focusedComp string
14+
15+
const (
16+
listComponent = "list"
17+
textComponent = "text"
818
)
919

1020
type OptionsProvider struct {
1121
list list.Model
22+
input OptionsInput
1223
onSelectAction tea.Msg
1324
width int
1425
height int
26+
focused focusedComp
1527
}
1628

1729
type Option struct {
@@ -38,25 +50,38 @@ func (o OptionsProvider) Update(msg tea.Msg) (OptionsProvider, tea.Cmd) {
3850
o.width = msg.Width
3951
o.list.SetSize(o.list.Width(), o.height)
4052
case tea.KeyMsg:
41-
42-
o.list, cmd = o.list.Update(msg)
43-
cmds = append(cmds, cmd)
44-
45-
switch msg.String() {
46-
47-
default:
53+
switch o.focused {
54+
case listComponent:
55+
if !o.IsFiltering() {
56+
switch {
57+
case key.Matches(msg, keybinds.Keys.InsertItem):
58+
o.list.SetSize(o.list.Width(), o.height-lipgloss.Height(o.input.View()))
59+
o.input.OnFocus()
60+
o.focused = textComponent
61+
return o, tea.Batch(cmds...)
62+
}
63+
}
4864
}
4965

5066
}
67+
switch o.focused {
68+
case listComponent:
69+
o.list, cmd = o.list.Update(msg)
70+
case textComponent:
71+
o.input, cmd = o.input.Update(msg)
72+
}
73+
cmds = append(cmds, cmd)
5174
return o, tea.Batch(cmds...)
5275
}
5376

5477
func (o OptionsProvider) View() string {
78+
if o.focused == textComponent {
79+
return lipgloss.JoinVertical(lipgloss.Left, o.list.View(), o.input.View())
80+
}
5581
return o.list.View()
5682
}
5783

58-
func (o OptionsProvider) OnFocus() {
59-
84+
func (o *OptionsProvider) OnFocus() {
6085
}
6186

6287
func (o OptionsProvider) OnBlur() {
@@ -67,6 +92,10 @@ func (o OptionsProvider) GetSelected() Option {
6792
return o.list.SelectedItem().(Option)
6893
}
6994

95+
func (o OptionsProvider) IsFiltering() bool {
96+
return o.list.FilterState() == list.Filtering
97+
}
98+
7099
func initList[T, U any](config *ListConfig[T, U]) list.Model {
71100

72101
rawItems, err := config.CrudOps.List(context.Background())
@@ -77,7 +106,7 @@ func initList[T, U any](config *ListConfig[T, U]) list.Model {
77106

78107
items := config.ItemMapper(rawItems)
79108

80-
list := list.New(items, list.NewDefaultDelegate(), 30, 30)
109+
list := list.New(items, config.Delegate, 30, 30)
81110

82111
// list configuration
83112
list.SetFilteringEnabled(config.FilteringEnabled)
@@ -93,7 +122,9 @@ func initList[T, U any](config *ListConfig[T, U]) list.Model {
93122

94123
func NewOptionsProvider[T, U any](config *ListConfig[T, U]) OptionsProvider {
95124
return OptionsProvider{
96-
list: initList(config),
125+
list: initList(config),
126+
focused: listComponent,
127+
input: NewOptionsInput(),
97128
// onSelectAction: config.OnSelectAction,
98129
}
99130
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package optionsProvider
2+
3+
import (
4+
"github.com/charmbracelet/bubbles/textinput"
5+
tea "github.com/charmbracelet/bubbletea"
6+
)
7+
8+
type OptionsInput struct {
9+
input textinput.Model
10+
height int
11+
width int
12+
editId int
13+
}
14+
15+
func NewOptionsInput() OptionsInput {
16+
input := textinput.New()
17+
input.CharLimit = 100
18+
input.Placeholder = "Add New Collection..."
19+
input.Width = 22
20+
return OptionsInput{
21+
input: input,
22+
editId: -1,
23+
}
24+
}
25+
26+
func (i OptionsInput) Init() tea.Cmd {
27+
return nil
28+
}
29+
30+
func (i OptionsInput) Update(msg tea.Msg) (OptionsInput, tea.Cmd) {
31+
var cmd tea.Cmd
32+
var cmds []tea.Cmd
33+
34+
i.input, cmd = i.input.Update(msg)
35+
cmds = append(cmds, cmd)
36+
37+
return i, tea.Batch(cmds...)
38+
}
39+
40+
func (i OptionsInput) View() string {
41+
return i.input.View()
42+
}
43+
44+
func (i *OptionsInput) SetInput(text string) {
45+
i.input.SetValue(text)
46+
}
47+
48+
func (i *OptionsInput) OnFocus(id ...int) {
49+
if len(id) > 0 {
50+
i.editId = id[0]
51+
}
52+
i.input.Focus()
53+
}
54+
55+
func (i *OptionsInput) OnBlur() {
56+
i.editId = -1
57+
i.input.Blur()
58+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package styles
2+
3+
import "github.com/charmbracelet/lipgloss"
4+
5+
var (
6+
SelectedListStyle = lipgloss.NewStyle().Foreground(accent).PaddingLeft(1).Border(lipgloss.NormalBorder(), false, false, false, true).BorderForeground(accent)
7+
)
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,29 @@
11
package views
22

33
import (
4+
"github.com/charmbracelet/bubbles/list"
45
optionsProvider "github.com/maniac-en/req/internal/tui/components/OptionsProvider"
6+
"github.com/maniac-en/req/internal/tui/styles"
57
)
68

9+
func createDelegate() list.DefaultDelegate {
10+
d := list.NewDefaultDelegate()
11+
12+
d.Styles.SelectedTitle = styles.SelectedListStyle
13+
d.Styles.SelectedDesc = styles.SelectedListStyle
14+
15+
return d
16+
}
17+
718
func defaultListConfig[T, U any]() *optionsProvider.ListConfig[T, U] {
819
config := optionsProvider.ListConfig[T, U]{
920
ShowPagination: false,
1021
ShowStatusBar: false,
1122
ShowHelp: false,
1223
ShowTitle: false,
1324
FilteringEnabled: true,
25+
26+
Delegate: createDelegate(),
1427
}
1528
return &config
1629
}

0 commit comments

Comments
 (0)