ni / nimble

The NI Nimble Design System
https://nimble.ni.dev
MIT License
32 stars 8 forks source link

Provide a way for Nimble Blazor components to request focus #2446

Open epetersoni opened 1 month ago

epetersoni commented 1 month ago

🙋 Feature Request

😯 Problem to Solve

There is an ask that on opening a NimbleDialog, we give focus to a particular Nimble field control within the dialog. I don't think this is possible with Nimble Blazor fields at the moment without resorting to some hacky JS that understands the internal implementation of the control.

💁 Proposed Solution

Some Nimble Blazor components - at least fields - expose a FocusAsync method. The internal implementation would need to find the ElementReference for the focusable part of the control and call the .NET FocusAsync method on it.

Exposing the ElementReference itself (or a wrapped version of it) could also be an option. We do have some bUnit test code that finds the input element for a field and invokes events like Change on it.

📋 Tasks

rajsite commented 1 month ago

When a dialog opens the first focusable control inside the dialog should gain focus. Can see it via the cursor in this example: https://jsbin.com/sixetez/3/edit?html,output

My question is are you seeing the first focusable control gain focus or does the blazor integration / windowing change the behavior?

If the first focusable control is immediately focused as expected, is there some other behavior being requested?

epetersoni commented 1 month ago

I am not seeing the first focusable control immediately focused. I suspect this is because the dialog content is being populated dynamically, and is not yet there at the initial dialog render.

rajsite commented 1 month ago

I am not seeing the first focusable control immediately focused. I suspect this is because the dialog content is being populated dynamically, and is not yet there at the initial dialog render.

Are you able to delay the call to ShowAsync until after the dialog contents are populated?

atmgrifter00 commented 1 month ago

Regarding the autofocus solution, I'll mention that not all of our components that can provide that behavior do (e.g. the NimbleNumberField), and further not all components that should provide that behavior do (e.g. the NimbleSelect/NimbleCombobox). The latter issue being present primarily due to FAST. We need a separate issue that tracks this (I will create it).

I suspect that we likely should ultimately expose access to the ElementReference on our component APIs. This is what Microsoft does for all of their input components at least.

rajsite commented 1 month ago

Regarding the autofocus solution, I'll mention that not all of our components that can provide that behavior do (e.g. the NimbleNumberField), and further not all components that should provide that behavior do (e.g. the NimbleSelect/NimbleCombobox). The latter issue being present primarily due to FAST. We need a separate issue that tracks this (I will create it).

I suspect that we likely should ultimately expose access to the ElementReference on our component APIs. This is what Microsoft does for all of their input components at least.

Chatted with Meyers offline and a couple points: