From a4863beb4f4fadb7cbacdf856c095fb920cb9141 Mon Sep 17 00:00:00 2001 From: Mohammad Aminsafaei Date: Wed, 4 Dec 2024 11:54:51 +0330 Subject: [PATCH] feat(blazorui): add InitialSelectedItems parameter to BitDorpdown #9364 (#9381) --- .../Inputs/Dropdown/BitDropdown.razor.cs | 30 +++++++++++++++++-- .../Inputs/Dropdown/BitDropdownDemo.razor.cs | 7 +++++ 2 files changed, 35 insertions(+), 2 deletions(-) diff --git a/src/BlazorUI/Bit.BlazorUI/Components/Inputs/Dropdown/BitDropdown.razor.cs b/src/BlazorUI/Bit.BlazorUI/Components/Inputs/Dropdown/BitDropdown.razor.cs index a60c48ab2a..f21c5e3b8f 100644 --- a/src/BlazorUI/Bit.BlazorUI/Components/Inputs/Dropdown/BitDropdown.razor.cs +++ b/src/BlazorUI/Bit.BlazorUI/Components/Inputs/Dropdown/BitDropdown.razor.cs @@ -126,6 +126,11 @@ namespace Bit.BlazorUI; /// [Parameter] public RenderFragment? HeaderTemplate { get; set; } + /// + /// The initial items that will be used to set selected items when using an ItemProvider. + /// + [Parameter] public IEnumerable? InitialSelectedItems { get; set; } + /// /// Determines the opening state of the callout. (two-way bound) /// @@ -779,14 +784,32 @@ protected override async Task OnInitializedAsync() if (MultiSelect) { - if (ValuesHasBeenSet is false && DefaultValues is not null) + if (ItemsProvider is not null && (InitialSelectedItems?.Any() ?? false)) + { + _selectedItems.AddRange(InitialSelectedItems); + + if (ValuesHasBeenSet is false) + { + await AssignValues(_selectedItems.Select(s => GetValue(s))); + } + } + else if (ValuesHasBeenSet is false && DefaultValues is not null) { await AssignValues(DefaultValues); } } else { - if (ValueHasBeenSet is false && DefaultValue is not null) + if (ItemsProvider is not null && (InitialSelectedItems?.Any() ?? false)) + { + _selectedItems.Add(InitialSelectedItems.First()); + + if (ValueHasBeenSet is false) + { + Value = GetValue(_selectedItems.First()); + } + } + else if (ValueHasBeenSet is false && DefaultValue is not null) { Value = DefaultValue; } @@ -1143,6 +1166,9 @@ private async ValueTask> InternalItemsProvider(ItemsP _lastShowItems = [.. providerResult.Items]; + UpdateSelectedItemsFromValues(); + await InvokeAsync(StateHasChanged); + return new ItemsProviderResult(providerResult.Items, providerResult.TotalItemCount); } diff --git a/src/BlazorUI/Demo/Client/Bit.BlazorUI.Demo.Client.Core/Pages/Components/Inputs/Dropdown/BitDropdownDemo.razor.cs b/src/BlazorUI/Demo/Client/Bit.BlazorUI.Demo.Client.Core/Pages/Components/Inputs/Dropdown/BitDropdownDemo.razor.cs index 96cacf31fa..1fcc769595 100644 --- a/src/BlazorUI/Demo/Client/Bit.BlazorUI.Demo.Client.Core/Pages/Components/Inputs/Dropdown/BitDropdownDemo.razor.cs +++ b/src/BlazorUI/Demo/Client/Bit.BlazorUI.Demo.Client.Core/Pages/Components/Inputs/Dropdown/BitDropdownDemo.razor.cs @@ -131,6 +131,13 @@ public partial class BitDropdownDemo Description = "The custom template for rendering the header items of the dropdown.", }, new() + { + Name = "InitialSelectedItems", + Type = "IEnumerable?", + DefaultValue = "null", + Description = "The initial items that will be used to set selected items when using an ItemProvider.", + }, + new() { Name = "IsOpen", Type = "bool",