Repository navigation
[Forms] Focus Autofocus inputs rendered after the page is loaded - #5474
NoobMaster (oONoobMasterOo) wants to merge 2 commits into
Conversation
The web components only apply autofocus when no other element is focused, like the native attribute. An input with Autofocus shown conditionally, or displayed after a navigation, stayed unfocused because the switch or link used to get there kept the focus. The input is now focused after its first render. Fixes microsoft#4699 Fixes microsoft#4968
There was a problem hiding this comment.
🟢 Approval recommended
The focused changes have no identified blocking issues and include regression tests, though tests were not executed.
No open issues 🎉
What changed in this PR
Fixes autofocus for Fluent UI inputs rendered conditionally or after navigation, addressing #4699 and #4968.
Changes:
- Focuses autofocus-enabled inputs after their first render and accessibility-label setup.
- Adds an internal autofocus contract and a JavaScript focus helper.
- Adds tests for text inputs, checkboxes, and inputs without autofocus.
| File | Description |
|---|---|
| tests/Core/Components/Base/InputBaseTests.cs | Adds autofocus interop tests. |
| src/Core/Components/Field/FluentField.razor.cs | Applies focus after accessibility-label setup. |
| src/Core/Components/Base/IFluentControlAutofocus.cs | Defines the internal autofocus contract. |
| src/Core/Components/Base/FluentInputBase.cs | Implements the autofocus contract. |
| src/Core.Scripts/src/Utilities/Attributes.ts | Adds an element-focus helper. |
Review effort: Balanced
|
Could you please provide a short video showing the issue before the code change, and then the expected behavior after the fix? This will make it easier to clearly see and validate the improvement. |
|
Here is the requested video, with a page showing both cases: a switch showing a Before (dev): the input doesn't get the focus in both cases, so the typed text goes nowhere. 5474-before.mp4After (this PR): the input gets the focus in both cases and the typed text goes into it. 5474-after.mp4 |
|
We've added a new requirement regarding PRs
|
Checked |
Pull Request
📖 Description
Cause
The web components apply
autofocuswithmaybeSetAutoFocus(), which focuses the element only when no other element is focused (document.activeElementisnull,bodyor the element itself), like the native attribute.Fix
FluentFieldfocuses its input after the first render whenAutofocusis set, using a newAttributes.focusscript function. Inputs withoutAutofocusdon't make any extra JS call. TheAutofocusvalue is read through a new internalIFluentControlAutofocusinterface implemented byFluentInputBase, likeIFluentControlAriaLabel, soIFluentFieldis unchanged.The focus is applied after the
aria-labelcopy, so the accessible name is set when the input is focused.🎫 Issues
👩💻 Reviewer Notes
Inputs without
Autofocusdon't make any extra JS call. The focus is applied after thearia-labelcopy inFluentField.📑 Test Plan
Unit tests
Added:
InputBase_Autofocus_FocusesInputAfterFirstRenderInputBase_Autofocus_FocusesCheckboxAfterFirstRenderInputBase_WithoutAutofocus_DoesNotFocusInputFull
tests/Coresuite on this branch: 4906 tests, 0 failed (at77ac4fac7).Manual test
Demo (Interactive Server), before / after:
FluentTextInput Autofocus(sample from #4699)FluentTextInput Autofocus(#4968)Other checks
✅ Checklist
General
AI contribution declaration
AI assistance is allowed; fully AI-generated or autonomous pull requests without meaningful human review are not accepted. See the AI contribution policy.
Component-specific
⏭ Next Steps
None.