Skip to content

Commit 0d41771

Browse files
authored
feat: Add FocusAsync methods to FluentInputBase (#191)
# Pull Request ## 📖 Description Add FocusAsync methods to FluentInputBase and give examples in the TextField, NumberField and TextArea pages. This PR exposes the ElementReference.FocusAsync() methods of the underlying element encapsulated by the FluentInputBase derived components. This makes it easier to programmatically set the focus on text based input fields. <!--- Provide some background and a description of your work. What problem does this change solve? Is this a breaking change, chore, fix, feature, etc? --> ### 🎫 Issues closes #157 <!--- * List and link relevant issues here. --> ## 👩‍💻 Reviewer Notes <!--- Provide some notes for reviewers to help them provide targeted feedback and testing. Do you recommend a smoke test for this PR? What steps should be followed? Are there particular areas of the code the reviewer should focus on? --> ## 📑 Test Plan <!--- Please provide a summary of the tests affected by this work and any unique strategies employed in testing the features/fixes. --> ## ✅ Checklist ### General <!--- Review the list and put an x in the boxes that apply. --> - [x] I have added tests for my changes. - [x] I have tested my changes. - [ ] I have updated the project documentation to reflect my changes. - [x] I have read the [CONTRIBUTING](https://github.com/Microsoft/fast-blazor/blob/master/CONTRIBUTING.md) documentation and followed the [standards](https://www.fast.design/docs/community/code-of-conduct/#our-standards) for this project. ### Component-specific <!--- Review the list and put an x in the boxes that apply. --> <!--- Remove this section if not applicable. --> - [ ] I have added a new component - [x] I have modified an existing component ## ⏭ Next Steps <!--- If there is relevant follow-up work to this PR, please list any existing issues or provide brief descriptions of what you would like to do next. -->
2 parents 7539db3 + 2688796 commit 0d41771

File tree

4 files changed

+44
-2
lines changed

4 files changed

+44
-2
lines changed

examples/FluentUI.Demo.Shared/Pages/NumberFieldPage.razor

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,13 @@
126126
<h4>With aria-label</h4>
127127
<FluentNumberField TValue="int?" aria-label="Text field with aria-label"></FluentNumberField>
128128

129+
<!-- Focus Async -->
130+
<h4>Focus Async</h4>
131+
<FluentButton @onclick="() => focusTest!.FocusAsync()">FocusAsync</FluentButton>
132+
<FluentTextField @ref=focusTest></FluentTextField>
133+
129134
@code{
135+
FluentTextField? focusTest;
130136
int exampleInt { get; set; } = 123;
131137
long exampleLong { get; set; } = 999999999999;
132138
float exampleFloat { get; set; } = 123.45f;

examples/FluentUI.Demo.Shared/Pages/TextAreaPage.razor

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,4 +89,12 @@
8989

9090
<!-- With aria label -->
9191
<h4>With aria-label</h4>
92-
<FluentTextArea aria-label="Text area with aria-label"></FluentTextArea>
92+
<FluentTextArea aria-label="Text area with aria-label"></FluentTextArea>
93+
94+
<!-- Focus Async -->
95+
<h4>Focus Async</h4>
96+
<FluentButton @onclick="() => focusTest!.FocusAsync()">FocusAsync</FluentButton>
97+
<FluentTextArea @ref=focusTest></FluentTextArea>
98+
@code {
99+
FluentTextArea? focusTest;
100+
}

examples/FluentUI.Demo.Shared/Pages/TextFieldPage.razor

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,4 +100,12 @@
100100
<FluentTextField MinLength="4">Minlength</FluentTextField>
101101

102102
<h4>Maxlength</h4>
103-
<FluentTextField MaxLength="4">Maxlength</FluentTextField>
103+
<FluentTextField MaxLength="4">Maxlength</FluentTextField>
104+
105+
<!-- Focus Async -->
106+
<h4>Focus Async</h4>
107+
<FluentButton @onclick="() => focusTest!.FocusAsync()">FocusAsync</FluentButton>
108+
<FluentTextField @ref=focusTest></FluentTextField>
109+
@code {
110+
FluentTextField? focusTest;
111+
}

src/Microsoft.Fast.Components.FluentUI/FluentInputBase.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,26 @@ public override Task SetParametersAsync(ParameterView parameters)
201201
return base.SetParametersAsync(ParameterView.Empty);
202202
}
203203

204+
/// <summary>
205+
/// Exposes the element's FocusAsync() method.
206+
/// </summary>
207+
public async void FocusAsync()
208+
{
209+
await Element!.FocusAsync();
210+
}
211+
212+
/// <summary>
213+
/// Exposes the elements FocusAsync(bool preventScroll) method.
214+
/// </summary>
215+
/// <param name="preventScroll">A Boolean value indicating whether or not the browser should scroll
216+
/// the document to bring the newly-focused element into view. A value of false for preventScroll (the default)
217+
/// means that the browser will scroll the element into view after focusing it.
218+
/// If preventScroll is set to true, no scrolling will occur.</param>
219+
public async void FocusAsync(bool preventScroll)
220+
{
221+
await Element!.FocusAsync(preventScroll);
222+
}
223+
204224
private void OnValidateStateChanged(object? sender, ValidationStateChangedEventArgs eventArgs)
205225
{
206226
UpdateAdditionalValidationAttributes();

0 commit comments

Comments
 (0)