@@ -378,7 +378,7 @@ type multiSelectModel struct {
378
378
message string
379
379
canceled bool
380
380
selected bool
381
- isInputMode bool // New field to track if we're adding a custom option
381
+ isCustomInputMode bool // New field to track if we're adding a custom option
382
382
customInput string // New field to store custom input
383
383
enableCustomInput bool // New field to control whether custom input is allowed
384
384
}
@@ -391,7 +391,7 @@ func (multiSelectModel) Init() tea.Cmd {
391
391
func (m multiSelectModel ) Update (msg tea.Msg ) (tea.Model , tea.Cmd ) {
392
392
var cmd tea.Cmd
393
393
394
- if m .isInputMode {
394
+ if m .isCustomInputMode {
395
395
return m .handleCustomInputMode (msg )
396
396
}
397
397
@@ -409,7 +409,7 @@ func (m multiSelectModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
409
409
case tea .KeyEnter :
410
410
// Switch to custom input mode if we're on the "+ Add custom value:" option
411
411
if m .enableCustomInput && m .cursor == len (m .filteredOptions ()) {
412
- m .isInputMode = true
412
+ m .isCustomInputMode = true
413
413
return m , nil
414
414
}
415
415
if len (m .options ) != 0 {
@@ -427,17 +427,15 @@ func (m multiSelectModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
427
427
return m , nil
428
428
429
429
case tea .KeyUp :
430
- options := m .filteredOptions ()
431
- maxIndex := len (options )
430
+ maxIndex := m .getMaxIndex ()
432
431
if m .cursor > 0 {
433
432
m .cursor --
434
433
} else {
435
434
m .cursor = maxIndex
436
435
}
437
436
438
437
case tea .KeyDown :
439
- options := m .filteredOptions ()
440
- maxIndex := len (options )
438
+ maxIndex := m .getMaxIndex ()
441
439
if m .cursor < maxIndex {
442
440
m .cursor ++
443
441
} else {
@@ -473,6 +471,16 @@ func (m multiSelectModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
473
471
return m , cmd
474
472
}
475
473
474
+ func (m multiSelectModel ) getMaxIndex () int {
475
+ options := m .filteredOptions ()
476
+ if m .enableCustomInput {
477
+ // Include the "+ Add custom value" entry
478
+ return len (options )
479
+ }
480
+ // Includes only the actual options
481
+ return len (options ) - 1
482
+ }
483
+
476
484
// handleCustomInputMode manages keyboard interactions when in custom input mode
477
485
func (m * multiSelectModel ) handleCustomInputMode (msg tea.Msg ) (tea.Model , tea.Cmd ) {
478
486
keyMsg , ok := msg .(tea.KeyMsg )
@@ -499,15 +507,44 @@ func (m *multiSelectModel) handleCustomInputMode(msg tea.Msg) (tea.Model, tea.Cm
499
507
500
508
// handleCustomInputSubmission processes the submission of custom input
501
509
func (m * multiSelectModel ) handleCustomInputSubmission () (tea.Model , tea.Cmd ) {
502
- if m .customInput != "" {
503
- m .options = append (m .options , & multiSelectOption {
504
- option : m .customInput ,
505
- chosen : true ,
506
- })
510
+ if m .customInput == "" {
511
+ m .isCustomInputMode = false
512
+ return m , nil
513
+ }
514
+
515
+ // Clear search to ensure option is visible and cursor points to the new option
516
+ m .search .SetValue ("" )
517
+
518
+ // Check for duplicates
519
+ for i , opt := range m .options {
520
+ if strings .EqualFold (opt .option , m .customInput ) {
521
+ // If the option exists but isn't chosen, select it
522
+ if ! opt .chosen {
523
+ opt .chosen = true
524
+ }
525
+
526
+ // Point cursor to the new option
527
+ m .cursor = i
528
+
529
+ // Reset custom input mode to disabled
530
+ m .isCustomInputMode = false
531
+ m .customInput = ""
532
+ return m , nil
533
+ }
507
534
}
508
- // Reset input state regardless of whether input was empty
535
+
536
+ // Add new unique option
537
+ m .options = append (m .options , & multiSelectOption {
538
+ option : m .customInput ,
539
+ chosen : true ,
540
+ })
541
+
542
+ // Point cursor to the newly added option
543
+ m .cursor = len (m .options ) - 1
544
+
545
+ // Reset custom input mode to disabled
509
546
m .customInput = ""
510
- m .isInputMode = false
547
+ m .isCustomInputMode = false
511
548
return m , nil
512
549
}
513
550
@@ -531,7 +568,7 @@ func (m multiSelectModel) View() string {
531
568
return s .String ()
532
569
}
533
570
534
- if m .isInputMode {
571
+ if m .isCustomInputMode {
535
572
_ , _ = s .WriteString (fmt .Sprintf ("%s\n Enter custom value: %s\n " , msg , m .customInput ))
536
573
return s .String ()
537
574
}
0 commit comments