Skip to content

Focus the first field of a MessageForm when it is displayed#134

Merged
Oleg-Melnik merged 3 commits into
masterfrom
dialog-focused-component
Jul 16, 2026
Merged

Focus the first field of a MessageForm when it is displayed#134
Oleg-Melnik merged 3 commits into
masterfrom
dialog-focused-component

Conversation

@Oleg-Melnik

@Oleg-Melnik Oleg-Melnik commented Jul 16, 2026

Copy link
Copy Markdown
Collaborator

Fixes #133.

What

When a dialog (or wizard page) containing a MessageForm is opened, the
focus now lands on the form's first field. Previously it landed on an
arbitrary field in the middle of the form — the first one that reported
itself invalid, which on a pristine form merely means "empty with
validation constraints".

Details

MessageForm.focus() conflated two behaviors wanted by different callers:
focusing the first field (when the form is displayed) and focusing the
first invalid field (after a failed validation). This change splits them:

  • focus() — the FocusableComponent override invoked by CommandDialog
    and CommandWizard when the form is displayed — now focuses the form's
    first field, keeping the existing oneof handling (if the first field is
    a oneof, its selected option receives the focus).
  • A new focusInvalidField() method keeps the first-invalid-field search
    and falls back to focus() when no field is invalid. The two validation
    failure paths in updateMessage() now call it, which is what their
    guarding parameter's name always implied.

Since focus requests are dispatched asynchronously and a nested form can
only be entered through its focus() method, a new internal
focusInvalidFieldRequested flag tells a nested form that the incoming
focus request targets its invalid field rather than its first one. This
preserves the previous behavior where a validation failure focuses the
invalid entry however deeply it is nested.

The focusInvalidField parameter of the private updateMessage method is
renamed to focusInvalidPart, aligning it with the public
updateValidationDisplay(focusInvalidPart) API and freeing the name for
the new method.

Other changes

  • Bumped the version to 2.0.0-SNAPSHOT.89 and regenerated pom.xml and
    dependencies.md.

Oleg-Melnik and others added 3 commits July 16, 2026 14:58
`MessageForm.focus()` used to focus the first invalid field, and only
fell back to the first field if no field was invalid. It is called both
when the form is displayed (by `CommandDialog` and `CommandWizard`) and
after a validation failure, which are two different expectations.

All fields of a form that has just been displayed are empty, and those
that have validation constraints report themselves invalid because of
this. Such a field was focused instead of the form's first one.

Focus the first field in `focus()`, and extract the search of the field
that has caused a validation failure into `focusInvalidField()`, which
the validation paths of `updateMessage()` now use.

Since focus requests are dispatched asynchronously and a nested form
receives them via its `focus()`, `focusInvalidField()` tells a nested
form to focus its invalid field as well. Otherwise, focusing an invalid
field edited by a nested form would focus that form's first field,
overwriting the invalid one chosen by the validation cascade.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
@Oleg-Melnik
Oleg-Melnik requested review from armiol and dpikhulya July 16, 2026 12:36
@Oleg-Melnik
Oleg-Melnik merged commit f97c05f into master Jul 16, 2026
5 checks passed
@Oleg-Melnik
Oleg-Melnik deleted the dialog-focused-component branch July 16, 2026 14:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

MessageForm: the first field is not focused when a dialog is opened

3 participants