Skip to content

Commit

Permalink
Add callback for Open and Close to RadzenDropDown (#1771)
Browse files Browse the repository at this point in the history
Co-authored-by: jmauch <[email protected]>
  • Loading branch information
mrfixij and jmauch authored Nov 5, 2024
1 parent bb675ee commit 36f39b5
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 5 deletions.
45 changes: 40 additions & 5 deletions Radzen.Blazor/RadzenDropDown.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ namespace Radzen.Blazor
/// </example>
public partial class RadzenDropDown<TValue> : DropDownBase<TValue>
{
bool isOpen;
/// <summary>
/// Specifies additional custom attributes that will be rendered by the input.
/// </summary>
Expand Down Expand Up @@ -117,8 +118,8 @@ internal override async Task ClosePopup(string key)
of = OpenOnFocus;
OpenOnFocus = false;
}

await JSRuntime.InvokeVoidAsync("Radzen.closePopup", PopupID);
isOpen = false;
await JSRuntime.InvokeVoidAsync("Radzen.closePopup", PopupID, Reference, nameof(OnClose));

if (key == "Enter")
{
Expand All @@ -138,7 +139,20 @@ protected override async Task OpenPopup(string key = "ArrowDown", bool isFilter
if (Disabled)
return;

await JSRuntime.InvokeVoidAsync(OpenOnFocus ? "Radzen.openPopup" : "Radzen.togglePopup", Element, PopupID, true);
if (!isOpen)
{
await Open.InvokeAsync(null);
}

isOpen = true;
if (OpenOnFocus)
{
await JSRuntime.InvokeVoidAsync("Radzen.openPopup", Element, PopupID, true, null, null, null, Reference, nameof(OnClose));
}
else
{
await JSRuntime.InvokeVoidAsync("Radzen.togglePopup", Element, PopupID, true, Reference, nameof(OnClose));
}
await JSRuntime.InvokeVoidAsync("Radzen.focusElement", isFilter ? UniqueID : SearchID);

if (list != null && selectedIndex != -1)
Expand Down Expand Up @@ -198,6 +212,18 @@ internal override void RenderItem(RenderTreeBuilder builder, object item)
[Parameter]
public string SelectAllText { get; set; }

/// <summary>
/// Callback for when a dropdown is opened.
/// </summary>
[Parameter]
public EventCallback Open { get; set; }

/// <summary>
/// Callback for when a dropdown is closed.
/// </summary>
[Parameter]
public EventCallback Close { get; set; }

private bool visibleChanged = false;
private bool disabledChanged = false;
private bool firstRender = true;
Expand Down Expand Up @@ -284,7 +310,8 @@ protected override async Task OnSelectItem(object item, bool isFromKey = false)
{
if (!Multiple && !isFromKey)
{
await JSRuntime.InvokeVoidAsync("Radzen.closePopup", PopupID);
isOpen = false;
await JSRuntime.InvokeVoidAsync("Radzen.closePopup", PopupID, Reference, nameof(OnClose));
}

if (ClearSearchAfterSelection)
Expand Down Expand Up @@ -337,9 +364,17 @@ public override void Dispose()
}
}

[JSInvokable]
public async Task OnClose()

Check warning on line 368 in Radzen.Blazor/RadzenDropDown.razor.cs

View workflow job for this annotation

GitHub Actions / build

Missing XML comment for publicly visible type or member 'RadzenDropDown<TValue>.OnClose()'

Check warning on line 368 in Radzen.Blazor/RadzenDropDown.razor.cs

View workflow job for this annotation

GitHub Actions / build

Missing XML comment for publicly visible type or member 'RadzenDropDown<TValue>.OnClose()'

Check warning on line 368 in Radzen.Blazor/RadzenDropDown.razor.cs

View workflow job for this annotation

GitHub Actions / build

Missing XML comment for publicly visible type or member 'RadzenDropDown<TValue>.OnClose()'

Check warning on line 368 in Radzen.Blazor/RadzenDropDown.razor.cs

View workflow job for this annotation

GitHub Actions / build

Missing XML comment for publicly visible type or member 'RadzenDropDown<TValue>.OnClose()'

Check warning on line 368 in Radzen.Blazor/RadzenDropDown.razor.cs

View workflow job for this annotation

GitHub Actions / build

Missing XML comment for publicly visible type or member 'RadzenDropDown<TValue>.OnClose()'

Check warning on line 368 in Radzen.Blazor/RadzenDropDown.razor.cs

View workflow job for this annotation

GitHub Actions / build

Missing XML comment for publicly visible type or member 'RadzenDropDown<TValue>.OnClose()'
{
isOpen = false;
await Close.InvokeAsync();
}

internal async Task PopupClose()
{
await JSRuntime.InvokeVoidAsync("Radzen.closePopup", PopupID);
isOpen = false;
await JSRuntime.InvokeVoidAsync("Radzen.closePopup", PopupID, Reference, nameof(OnClose));
}
}
}
18 changes: 18 additions & 0 deletions RadzenBlazorDemos/Pages/DropDownOpenCloseEvent.razor
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
@inherits DbContextPage

<RadzenStack Orientation="Orientation.Horizontal" AlignItems="AlignItems.Center" JustifyContent="JustifyContent.Center" Gap="0.5rem" class="rz-p-sm-12">
<RadzenLabel Text="Select Value" Component="DropDownChangeEvent" />
<RadzenDropDown TValue="string" Value=@value Data=@companyNames Open="@(() => Console.WriteLine("opened"))" Close="@(()=>Console.WriteLine("closed"))" Style="width: 100%; max-width: 400px;" Name="DropDownChangeEvent" />
</RadzenStack>

@code {
string value = "Around the Horn";
IEnumerable<string> companyNames;

protected override async Task OnInitializedAsync()
{
await base.OnInitializedAsync();

companyNames = dbContext.Customers.Select(c => c.CompanyName).Distinct();
}
}
6 changes: 6 additions & 0 deletions RadzenBlazorDemos/Pages/DropDownPage.razor
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,12 @@
<RadzenExample ComponentName="DropDown" Example="DropDownEdit">
<DropDownEdit/>
</RadzenExample>
<RadzenText Anchor="dropdown#open-and-close-event" TextStyle="TextStyle.H5" TagName="TagName.H2" class="rz-pt-8">
Open and close events
</RadzenText>
<RadzenExample ComponentName="DropDown" Example="DropDownOpenCloseEvent">
<DropDownOpenCloseEvent />
</RadzenExample>

<RadzenText Anchor="dropdown#keyboard-navigation" TextStyle="TextStyle.H5" TagName="TagName.H2" class="rz-pt-12">
Keyboard Navigation
Expand Down

0 comments on commit 36f39b5

Please sign in to comment.