2025-12-26 17:45:37 +05:00
|
|
|
|
package fields
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
|
"fmt"
|
|
|
|
|
|
"github.com/charmbracelet/bubbles/cursor"
|
|
|
|
|
|
"github.com/charmbracelet/bubbles/textinput"
|
|
|
|
|
|
tea "github.com/charmbracelet/bubbletea"
|
|
|
|
|
|
)
|
|
|
|
|
|
|
2026-01-19 17:44:49 +05:00
|
|
|
|
// InputModel структура описывающая модель поля ввода данных
|
2025-12-26 17:45:37 +05:00
|
|
|
|
type InputModel struct {
|
2026-01-19 17:44:49 +05:00
|
|
|
|
label string
|
|
|
|
|
|
description string
|
|
|
|
|
|
input textinput.Model
|
2025-12-26 17:45:37 +05:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-20 17:40:21 +05:00
|
|
|
|
type FieldValidator func(value any) error
|
|
|
|
|
|
|
2026-01-19 17:44:49 +05:00
|
|
|
|
// NewInputModel создает новую модель поля ввода данных
|
2026-01-20 17:40:21 +05:00
|
|
|
|
func NewInputModel(label, value, description string, validator textinput.ValidateFunc) *InputModel {
|
|
|
|
|
|
|
2025-12-26 17:45:37 +05:00
|
|
|
|
t := textinput.New()
|
2026-01-20 17:40:21 +05:00
|
|
|
|
t.Validate = validator
|
2025-12-26 17:45:37 +05:00
|
|
|
|
t.SetValue(value)
|
|
|
|
|
|
t.Cursor.SetMode(cursor.CursorBlink)
|
|
|
|
|
|
return &InputModel{
|
2026-01-19 17:44:49 +05:00
|
|
|
|
label: label,
|
|
|
|
|
|
description: description,
|
|
|
|
|
|
input: t,
|
2025-12-26 17:45:37 +05:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (m *InputModel) Init() tea.Cmd {
|
|
|
|
|
|
return textinput.Blink
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-19 17:44:49 +05:00
|
|
|
|
// Update отслеживает действия с полем
|
2025-12-26 17:45:37 +05:00
|
|
|
|
func (m *InputModel) Update(msg tea.Msg) (FieldModel, tea.Cmd) {
|
|
|
|
|
|
var cmd tea.Cmd
|
|
|
|
|
|
m.input, cmd = m.input.Update(msg)
|
|
|
|
|
|
return m, cmd
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (m *InputModel) View() string {
|
|
|
|
|
|
res := fmt.Sprintf("%s: %v", m.label, m.input.View())
|
2026-01-20 17:40:21 +05:00
|
|
|
|
if m.input.Err != nil {
|
|
|
|
|
|
res += fmt.Sprintf("\nОшибка валидации: %s", m.input.Err)
|
|
|
|
|
|
} else if m.description != "" {
|
2026-01-19 17:44:49 +05:00
|
|
|
|
res += fmt.Sprintf("\n%s", m.description)
|
|
|
|
|
|
}
|
2025-12-26 17:45:37 +05:00
|
|
|
|
return res
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (m *InputModel) Focus() {
|
|
|
|
|
|
m.input.Focus()
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (m *InputModel) Blur() {
|
|
|
|
|
|
m.input.Blur()
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (m *InputModel) CaptureKeys() bool {
|
|
|
|
|
|
//return m.input.Focused()
|
|
|
|
|
|
return false
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (m *InputModel) GetLabel() string {
|
|
|
|
|
|
return m.label
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (m *InputModel) GetValue() interface{} {
|
|
|
|
|
|
return m.input.Value()
|
|
|
|
|
|
}
|