Работа над валидацией полей, работа над выполнением действий

This commit is contained in:
XeRo 2026-01-21 17:56:21 +05:00
parent e37cabadcc
commit dc263b76bf
8 changed files with 220 additions and 23 deletions

View file

@ -16,11 +16,21 @@ type SelectModel struct {
open bool //Открыт ли список
chosen string //Выбранный элемент
optionsCount int //Количество элементов к показу
validate textinput.ValidateFunc
err error
}
// NewSelectModel создаем новую поля выбора опции
func NewSelectModel(label, value, description string, options []string) *SelectModel {
func NewSelectModel(label, value, description string, options []string, validator textinput.ValidateFunc) *SelectModel {
//Проверка, что указанный по умолчанию элемент есть в списке опции
selectModel := &SelectModel{
options: options,
label: label,
description: description,
focus: len(options),
validate: validator,
optionsCount: 10,
}
found := false
for i := range options {
if options[i] == value {
@ -30,14 +40,9 @@ func NewSelectModel(label, value, description string, options []string) *SelectM
if !found {
value = ""
}
return &SelectModel{
options: options,
label: label,
description: description,
focus: len(options),
chosen: value,
optionsCount: 10,
}
selectModel.chosen = value
selectModel.SelectValidate()
return selectModel
}
func (m *SelectModel) Init() tea.Cmd {
@ -75,6 +80,7 @@ func (m *SelectModel) Update(msg tea.Msg) (FieldModel, tea.Cmd) {
m.chosen = m.options[m.focus]
m.open = false
m.focus = 0
m.SelectValidate()
return m, nil
}
} else {
@ -100,7 +106,9 @@ func (m *SelectModel) Update(msg tea.Msg) (FieldModel, tea.Cmd) {
func (m *SelectModel) View() string {
res := fmt.Sprintf("%s: %s", m.label, m.chosen)
if m.description != "" {
if m.err != nil && !m.open {
res += fmt.Sprintf("\nОшибка валидации: %s", m.err)
} else if m.description != "" {
res += fmt.Sprintf("\n%s", m.description)
}
if m.open && len(m.options) > 0 {
@ -141,3 +149,9 @@ func (m *SelectModel) GetLabel() string {
func (m *SelectModel) GetValue() interface{} {
return m.chosen
}
func (m *SelectModel) SelectValidate() {
if m.validate != nil {
m.err = m.validate(m.chosen)
}
}