mirror of
https://github.com/Maks1mS/bubbles.git
synced 2025-01-11 22:41:03 +03:00
Add an optional character limit to text inputs
This commit is contained in:
parent
7ddc284804
commit
ea5161af98
@ -23,6 +23,10 @@ type Model struct {
|
|||||||
PlaceholderColor string
|
PlaceholderColor string
|
||||||
CursorColor string
|
CursorColor string
|
||||||
|
|
||||||
|
// CharLimit is the maximum amount of characters this input element will
|
||||||
|
// accept. If 0 or less, there's no limit.
|
||||||
|
CharLimit int
|
||||||
|
|
||||||
// Focus indicates whether user input focus should be on this input
|
// Focus indicates whether user input focus should be on this input
|
||||||
// component. When false, don't blink and ignore keyboard input.
|
// component. When false, don't blink and ignore keyboard input.
|
||||||
focus bool
|
focus bool
|
||||||
@ -138,9 +142,11 @@ func Update(msg tea.Msg, m Model) (Model, tea.Cmd) {
|
|||||||
m.Value = m.Value[m.pos:]
|
m.Value = m.Value[m.pos:]
|
||||||
m.pos = 0
|
m.pos = 0
|
||||||
return m, nil
|
return m, nil
|
||||||
case tea.KeyRune:
|
case tea.KeyRune: // input a regular character
|
||||||
m.Value = m.Value[:m.pos] + string(msg.Rune) + m.Value[m.pos:]
|
if m.CharLimit > 0 && len(m.Value) < m.CharLimit {
|
||||||
m.pos++
|
m.Value = m.Value[:m.pos] + string(msg.Rune) + m.Value[m.pos:]
|
||||||
|
m.pos++
|
||||||
|
}
|
||||||
return m, nil
|
return m, nil
|
||||||
default:
|
default:
|
||||||
return m, nil
|
return m, nil
|
||||||
|
Loading…
Reference in New Issue
Block a user