Skip to content
This repository has been archived by the owner on Feb 3, 2023. It is now read-only.

Commit

Permalink
add hide func for form item
Browse files Browse the repository at this point in the history
  • Loading branch information
ingbyr committed Sep 12, 2022
1 parent 305dd38 commit 7dc1bcf
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 6 deletions.
11 changes: 11 additions & 0 deletions tui/form/button.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,16 @@ import (
"gohost/tui/styles"
)

var _ Item = (*Button)(nil)

func NewButton(text string) *Button {
return &Button{
Text: text,
OnClick: func() tea.Cmd { return nil },
focused: false,
focusedStyle: styles.None,
unfocusedStyle: styles.None,
HideFunc: nil,
}
}

Expand All @@ -25,6 +28,14 @@ type Button struct {
focused bool
focusedStyle lipgloss.Style
unfocusedStyle lipgloss.Style
HideFunc HideCondition
}

func (b *Button) Hide() bool {
if b.HideFunc == nil {
return false
}
return b.HideFunc()
}

func (b *Button) Init() tea.Cmd {
Expand Down
10 changes: 9 additions & 1 deletion tui/form/choice.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,12 @@ func NewChoice(items []list.DefaultItem) *Choices {
}

type Choices struct {
items []list.DefaultItem
SelectedPrefix string
UnselectedPrefix string
MorePlaceHold string
ShowMorePlaceHold bool
HideFunc HideCondition
items []list.DefaultItem
focused bool
focusedStyle lipgloss.Style
unfocusedStyle lipgloss.Style
Expand All @@ -46,6 +47,13 @@ type Choices struct {
selectedIndex int
}

func (c *Choices) Hide() bool {
if c.HideFunc == nil {
return false
}
return c.HideFunc()
}

func (c *Choices) SetFocusedStyle(style lipgloss.Style) {
c.focusedStyle = style
}
Expand Down
4 changes: 4 additions & 0 deletions tui/form/form.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,10 @@ func (v *Form) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
func (v *Form) View() string {
var b strings.Builder
for i := range v.Items {
item := v.Items[i]
if item.Hide() {
continue
}
b.WriteString(v.Items[i].View())
if i < len(v.Items)-1 {
b.WriteString(strings.Repeat(cfg.LineBreak, v.Spacing+1))
Expand Down
3 changes: 3 additions & 0 deletions tui/form/item.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,7 @@ type Item interface {
InterceptKey(m tea.KeyMsg) bool
SetFocusedStyle(style lipgloss.Style)
SetUnfocusedStyle(style lipgloss.Style)
Hide() bool
}

type HideCondition func() bool
9 changes: 9 additions & 0 deletions tui/form/textinput.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,27 @@ func NewTextInput() *TextInput {
Model: textinput.New(),
focusedStyle: styles.None,
unfocusedStyle: styles.None,
HideFunc: nil,
}
t.Unfocus()
return t
}

type TextInput struct {
textinput.Model
HideFunc HideCondition
focused bool
focusedStyle lipgloss.Style
unfocusedStyle lipgloss.Style
}

func (t *TextInput) Hide() bool {
if t.HideFunc == nil {
return false
}
return t.HideFunc()
}

func (t *TextInput) SetFocusedStyle(style lipgloss.Style) {
t.focusedStyle = style
}
Expand Down
13 changes: 8 additions & 5 deletions tui/node_view.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,17 @@ func NewNodeView(model *Model) *NodeView {
descTextInput := form.NewTextInput()
descTextInput.Prompt = "Description: "

urlTextInput := form.NewTextInput()
urlTextInput.Prompt = "Url: "

// Node type choices
nodeTypeChoices := form.NewChoice([]list.DefaultItem{NodeGroup, NodeLocalHost, NodeRemoteHost})
nodeTypeChoices.Spacing = 1
nodeTypeChoices.ShowMorePlaceHold = false

urlTextInput := form.NewTextInput()
urlTextInput.Prompt = "Url: "
urlTextInput.HideFunc = func() bool {
return nodeTypeChoices.SelectedItem() != NodeRemoteHost
}

// Confirm button
confirmButton := form.NewButton("[ Confirm ]")
confirmButton.OnClick = func() tea.Cmd {
Expand Down Expand Up @@ -70,7 +73,7 @@ func NewNodeView(model *Model) *NodeView {
groupNode := gohost.NewTreeNode(node)
groupNode.SetParent(parent)
groupNode.SetDepth(parent.Depth() + 1)
if err := svc.SaveGroupNode(groupNode);err != nil {
if err := svc.SaveGroupNode(groupNode); err != nil {
panic(err)
}
cmd = model.treeView.RefreshTreeNodes()
Expand All @@ -94,8 +97,8 @@ func NewNodeView(model *Model) *NodeView {
nodeForm.SetItemUnfocusedStyle(styles.UnfocusedFormItem)
nodeForm.AddItem(nameTextInput)
nodeForm.AddItem(descTextInput)
nodeForm.AddItem(urlTextInput)
nodeForm.AddItem(nodeTypeChoices)
nodeForm.AddItem(urlTextInput)
nodeForm.AddItem(confirmButton)

return nodeForm
Expand Down

0 comments on commit 7dc1bcf

Please sign in to comment.