-
Notifications
You must be signed in to change notification settings - Fork 0
/
form.go
82 lines (73 loc) · 1.7 KB
/
form.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package main
import (
"github.com/charmbracelet/bubbles/help"
"github.com/charmbracelet/bubbles/key"
"github.com/charmbracelet/bubbles/textarea"
"github.com/charmbracelet/bubbles/textinput"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
)
type Form struct {
help help.Model
title textinput.Model
description textarea.Model
col column
index int
}
func newDefaultForm() *Form {
return NewForm("task name", "")
}
func NewForm(title, description string) *Form {
form := Form{
help: help.New(),
title: textinput.New(),
description: textarea.New(),
}
form.title.Placeholder = title
form.description.Placeholder = description
form.title.Focus()
return &form
}
func (f Form) CreateTask() Task {
return Task{"",f.col.status, f.title.Value(), f.description.Value()}
}
func (f Form) Init() tea.Cmd {
return nil
}
func (f Form) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
var cmd tea.Cmd
switch msg := msg.(type) {
case column:
f.col = msg
f.col.list.Index()
case tea.KeyMsg:
switch {
case key.Matches(msg, keys.Quit):
return f, tea.Quit
case key.Matches(msg, keys.Back):
return board.Update(nil)
case key.Matches(msg, keys.Enter):
if f.title.Focused() {
f.title.Blur()
f.description.Focus()
return f, textarea.Blink
}
// Return the completed form as a message.
return board.Update(f)
}
}
if f.title.Focused() {
f.title, cmd = f.title.Update(msg)
return f, cmd
}
f.description, cmd = f.description.Update(msg)
return f, cmd
}
func (f Form) View() string {
return lipgloss.JoinVertical(
lipgloss.Left,
"Create a new task",
f.title.View(),
f.description.View(),
f.help.View(keys))
}