feat(module: select): add AutoFocus to select base component (#3375)

Add AutoFocus to select components to provide same functionalty as other input components
This commit is contained in:
Luuk Glorie 2023-07-27 16:23:58 +02:00 committed by GitHub
parent a49127c190
commit ed68adf35e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 51 additions and 2 deletions

View File

@ -119,6 +119,8 @@ namespace AntDesign
/// </summary>
[Parameter] public EventCallback OnFocus { get; set; }
[Parameter] public bool AutoFocus { get; set; }
/// <summary>
/// The name of the property to be used as a group indicator.
/// If the value is set, the entries are displayed in groups.
@ -644,6 +646,16 @@ namespace AntDesign
base.OnInitialized();
}
protected override async Task OnFirstAfterRenderAsync()
{
if (AutoFocus)
{
await SetInputFocusAsync();
}
await base.OnFirstAfterRenderAsync();
}
protected void OnOverlayHide()
{
if (!IsSearchEnabled)

View File

@ -22,6 +22,7 @@ Select component to select value from options.
| AccessKey | The [accesskey](https://developer.mozilla.org/en-US/docs/Web/HTML/Global_attributes/accesskey) global attribute. | string | |
| AllowClear | Show clear button. Has no effect if Value type default is also in the list of options, unless used with `ValueOnClear`. | bool | false | |
| AutoClearSearchValue | Whether the current search will be cleared on selecting an item. | bool | true | |
| AutoFocus | get focus when component mounted | boolean | false |
| Bordered | Toggle the border style. | bool | true | |
| BoundaryAdjustMode | `Dropdown` adjustment strategy (when for example browser resize is happening) | TriggerBoundaryAdjustMode | TriggerBoundaryAdjustMode.InView |
| CustomTagLabelToValue | Converts custom tag (a string) to TItemValue type. | Func<string, TItemValue> | (label) => <br/> (TItemValue)TypeDescriptor<br/> .GetConverter(typeof(TItemValue))<br/> .ConvertFromInvariantString(label) | |

View File

@ -105,7 +105,8 @@
TItemValue="string?"
Value="null"
AllowClear>
</AntDesign.Select>);
</AntDesign.Select>
);
//Act
//normally blazor would rerender and in Select.OnParametersSet()
@ -117,6 +118,41 @@
//Assert
cut.Invoking(c => c.Find("span.ant-select-clear"))
.Should().Throw<Bunit.ElementNotFoundException>();
}
[Fact]
public void AutoFocus_focus_set_after_render()
{
//Arrange
#if !NET6_0_OR_GREATER
#pragma warning disable CS0618 // Type or member is obsolete
JSInterop.SetupVoid(JSInteropConstants.Focus, _ => true).SetVoidResult();
#pragma warning restore CS0618 // Type or member is obsolete
#endif
var dataSource = new List<PersonNullable>()
{
new PersonNullable("2", "Test 2"),
new PersonNullable("3", "Test 3"),
new PersonNullable("4", "Test 4"),
};
var cut = Render<AntDesign.Select<string?, PersonNullable>>(
@<AntDesign.Select DataSource="@dataSource"
LabelName="@nameof(PersonNullable.Name)"
ValueName="@nameof(PersonNullable.Id)"
TItem="PersonNullable"
TItemValue="string"
Value="null"
AutoFocus="true">
</AntDesign.Select>
);
//Assert
#if NET6_0_OR_GREATER
JSInterop.VerifyFocusAsyncInvoke();
#else
JSInterop.VerifyInvoke(JSInteropConstants.Focus);
#endif
}
}