Добавление и исправление комментариев, начало добавление валидации, сообщение прокрути viewport на форме
This commit is contained in:
parent
c7ff7662ba
commit
e37cabadcc
14 changed files with 371 additions and 297 deletions
|
|
@ -1,21 +1,26 @@
|
|||
package fields
|
||||
|
||||
import (
|
||||
"bubble/setup/utils"
|
||||
"fmt"
|
||||
"github.com/charmbracelet/bubbles/textinput"
|
||||
tea "github.com/charmbracelet/bubbletea"
|
||||
)
|
||||
|
||||
// SelectModel структура описывающая модель поля выбора опции
|
||||
type SelectModel struct {
|
||||
label string
|
||||
description string
|
||||
options []string
|
||||
focus int
|
||||
open bool
|
||||
chosen string
|
||||
label string //Имя поля
|
||||
description string //Описание пол
|
||||
options []string //Элементы выбора
|
||||
focus int //Фокус для управления опциями
|
||||
open bool //Открыт ли список
|
||||
chosen string //Выбранный элемент
|
||||
optionsCount int //Количество элементов к показу
|
||||
}
|
||||
|
||||
// NewSelectModel создаем новую поля выбора опции
|
||||
func NewSelectModel(label, value, description string, options []string) *SelectModel {
|
||||
//Проверка, что указанный по умолчанию элемент есть в списке опции
|
||||
found := false
|
||||
for i := range options {
|
||||
if options[i] == value {
|
||||
|
|
@ -26,11 +31,12 @@ func NewSelectModel(label, value, description string, options []string) *SelectM
|
|||
value = ""
|
||||
}
|
||||
return &SelectModel{
|
||||
options: options,
|
||||
label: label,
|
||||
description: description,
|
||||
focus: len(options),
|
||||
chosen: value,
|
||||
options: options,
|
||||
label: label,
|
||||
description: description,
|
||||
focus: len(options),
|
||||
chosen: value,
|
||||
optionsCount: 10,
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -42,30 +48,37 @@ func (m *SelectModel) Update(msg tea.Msg) (FieldModel, tea.Cmd) {
|
|||
var cmd tea.Cmd
|
||||
switch msg := msg.(type) {
|
||||
case tea.KeyMsg:
|
||||
//Отслеживаем нажатия клавиш.
|
||||
//Если список открыт, то отслеживаем клавиши отдельно
|
||||
if m.open {
|
||||
switch msg.String() {
|
||||
case "esc":
|
||||
//Закрываем список
|
||||
m.open = false
|
||||
case "up":
|
||||
//Уменьшаем фокус, поднимаем его
|
||||
m.focus--
|
||||
if m.focus < 0 {
|
||||
m.focus = len(m.options) - 1
|
||||
}
|
||||
case "down":
|
||||
//Увеличиваем фокус, опускаем его
|
||||
m.focus++
|
||||
if m.focus == len(m.options) {
|
||||
m.focus = 0
|
||||
}
|
||||
case "enter":
|
||||
if len(m.options) == 0 || m.focus == len(m.options) {
|
||||
//Выбираем опцию из списка
|
||||
if len(m.options) == 0 || m.focus >= len(m.options) {
|
||||
return m, nil
|
||||
}
|
||||
m.chosen = m.options[m.focus]
|
||||
m.open = false
|
||||
m.focus = len(m.options)
|
||||
m.focus = 0
|
||||
return m, nil
|
||||
}
|
||||
} else {
|
||||
//Если список не открыт и был нажат enter, открываем его и устанавливаем фокус на основе опций.
|
||||
switch msg.String() {
|
||||
case "enter":
|
||||
m.open = true
|
||||
|
|
@ -91,12 +104,16 @@ func (m *SelectModel) View() string {
|
|||
res += fmt.Sprintf("\n%s", m.description)
|
||||
}
|
||||
if m.open && len(m.options) > 0 {
|
||||
for i := range m.options {
|
||||
start, end := utils.CalculateSelectOptionsIndex(m.focus, m.optionsCount, len(m.options))
|
||||
for i := start; i < end; i++ {
|
||||
option := m.options[i]
|
||||
prefix := "\n\t"
|
||||
if m.focus == i {
|
||||
res += "\n\t-> " + m.options[i]
|
||||
prefix += "-> "
|
||||
} else {
|
||||
res += "\n\t " + m.options[i]
|
||||
prefix += " "
|
||||
}
|
||||
res += prefix + option
|
||||
}
|
||||
}
|
||||
return res
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue