@@ -212,17 +212,33 @@ var _ util.Model = (*Form[any])(nil)
212212
213213func (f * Form [T ]) Focus (parentKeyMap help.KeyMap ) tea.Cmd {
214214 f .focused , f .parentKeyMap = true , parentKeyMap
215- return f .items [ f . activeIndex ]. Element . Focus (f .keymap ())
215+ return f .activeElementFocus (f .keymap ())
216216}
217217
218218func (f * Form [T ]) Blur () {
219219 f .focused , f .parentKeyMap = false , nil
220- f .items [ f . activeIndex ]. Element . Blur ()
220+ f .activeElementBlur ()
221221}
222222
223223// *[Model] implements [util.Focusable]
224224var _ util.Focusable = (* Form [any ])(nil )
225225
226+ func (f * Form [T ]) hasActiveItem () bool { return f .activeIndex < len (f .items ) }
227+
228+ func (f * Form [T ]) activeElementFocus (parentKeyMap help.KeyMap ) tea.Cmd {
229+ if ! f .hasActiveItem () {
230+ return util .AnnounceKeyMapCmd (parentKeyMap )
231+ }
232+ return f .items [f .activeIndex ].Element .Focus (parentKeyMap )
233+ }
234+
235+ func (f * Form [T ]) activeElementBlur () {
236+ if ! f .hasActiveItem () {
237+ return
238+ }
239+ f .items [f .activeIndex ].Element .Blur ()
240+ }
241+
226242func (f * Form [T ]) viewRow (rowIndex int , availableWidth int , eager bool ) []string {
227243 return slicest .MapI (f .rows [rowIndex ].items , func (i int , itemIndex int ) string {
228244 width := availableWidth / (len (f .rows [rowIndex ].items ) - i )
@@ -310,6 +326,10 @@ func (f *Form[T]) guardUnsavedChanges(action Action) tea.Cmd {
310326func (f * Form [T ]) updateElement (index int , msg tea.Msg ) tea.Cmd {
311327 var actionCmd tea.Cmd
312328
329+ if index >= len (f .items ) {
330+ return nil
331+ }
332+
313333 updateCmd , action := f .items [index ].Element .Update (msg )
314334
315335 switch action {
@@ -354,10 +374,10 @@ func (f *Form[T]) changeActiveIndex(index_delta int) tea.Cmd {
354374 return nil
355375 }
356376
357- f .items [ f . activeIndex ]. Element . Blur ()
377+ f .activeElementBlur ()
358378 f .activeIndex = newActiveIndex
359379
360- return f .items [ f . activeIndex ]. Element . Focus (f .keymap ())
380+ return f .activeElementFocus (f .keymap ())
361381}
362382
363383func (f * Form [T ]) Get () (T , error ) {
0 commit comments