Skip to content

Commit

Permalink
PR #632 cleanup (AutoClose for IHxDropdownToggle)
Browse files Browse the repository at this point in the history
  • Loading branch information
hakenr committed Oct 24, 2023
1 parent 1c1fa2a commit ef41932
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@
}

<li class="@CssClassHelper.Combine("hx-context-menu-item", this.CssClass)">
<a class="@CssClassHelper.Combine("dropdown-item", enabledEffective ? "" : "disabled")" role="@(enabledEffective ? "button" : "")" href="@Href" @onclick="HandleClick" @onclick:stopPropagation="@OnClickStopPropagation">
@if (this.Icon is not null)
{
<HxIcon Icon="this.Icon" CssClass="@CssClassHelper.Combine("hx-context-menu-item-icon", this.IconCssClass)" />
}
@this.Text
@ChildContent
</a>
<a class="@CssClassHelper.Combine("dropdown-item", enabledEffective ? "" : "disabled")" role="@(enabledEffective ? "button" : "")" href="@Href" @onclick="HandleClick" @onclick:stopPropagation="@OnClickStopPropagation">
@if (this.Icon is not null)
{
<HxIcon Icon="this.Icon" CssClass="@CssClassHelper.Combine("hx-context-menu-item-icon", this.IconCssClass)" />
}
@this.Text
@ChildContent
</a>
</li>
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ static HxDropdownToggleButton()
/// The parameter can be used to override the settings of the <see cref="DropdownContainer"/> component or to specify the auto-close behavior when the component is not used.
/// </summary>
[Parameter] public DropdownAutoClose? AutoClose { get; set; }
protected DropdownAutoClose AutoCloseEffective => AutoClose ?? DropdownContainer?.AutoClose ?? DropdownAutoClose.True;

[CascadingParameter] protected HxDropdown DropdownContainer { get; set; }
[CascadingParameter] protected HxNav NavContainer { get; set; }
Expand Down Expand Up @@ -99,13 +100,13 @@ protected override void OnParametersSet()
AdditionalAttributes ??= new Dictionary<string, object>();
AdditionalAttributes["data-bs-toggle"] = "dropdown";
AdditionalAttributes["aria-expanded"] = "false";
AdditionalAttributes["data-bs-auto-close"] = (AutoClose ?? DropdownContainer?.AutoClose ?? DropdownAutoClose.True) switch
AdditionalAttributes["data-bs-auto-close"] = AutoCloseEffective switch
{
DropdownAutoClose.True => "true",
DropdownAutoClose.False => "false",
DropdownAutoClose.Inside => "inside",
DropdownAutoClose.Outside => "outside",
_ => throw new InvalidOperationException($"Unknown {nameof(DropdownAutoClose)} value {AutoClose ?? DropdownContainer.AutoClose}.")
_ => throw new InvalidOperationException($"Unknown {nameof(DropdownAutoClose)} value {AutoCloseEffective}.")
};

if (this.DropdownOffset is not null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,15 @@ public class HxDropdownToggleElement : ComponentBase, IHxDropdownToggle, IAsyncD
/// </summary>
[Parameter] public string CssClass { get; set; }

/// <summary>
/// By default, the dropdown menu is closed when clicking inside or outside the dropdown menu (<see cref="DropdownAutoClose.True"/>).
/// You can use the AutoClose parameter to change this behavior of the dropdown.
/// <see href="https://getbootstrap.com/docs/5.3/components/dropdowns/#auto-close-behavior">https://getbootstrap.com/docs/5.3/components/dropdowns/#auto-close-behavior</see>.
/// The parameter can be used to override the settings of the <see cref="DropdownContainer"/> component or to specify the auto-close behavior when the component is not used.
/// </summary>
[Parameter] public DropdownAutoClose? AutoClose { get; set; }
protected DropdownAutoClose AutoCloseEffective => AutoClose ?? DropdownContainer?.AutoClose ?? DropdownAutoClose.True;

[Parameter] public RenderFragment ChildContent { get; set; }

[Parameter(CaptureUnmatchedValues = true)] public IDictionary<string, object> AdditionalAttributes { get; set; }
Expand Down Expand Up @@ -86,13 +95,13 @@ protected override void BuildRenderTree(RenderTreeBuilder builder)
builder.AddAttribute(1, "data-bs-toggle", "dropdown");
builder.AddAttribute(2, "aria-expanded", "false");

var dataBsAutoCloseAttributeValue = (DropdownContainer?.AutoClose ?? DropdownAutoClose.True) switch
var dataBsAutoCloseAttributeValue = AutoCloseEffective switch
{
DropdownAutoClose.True => "true",
DropdownAutoClose.False => "false",
DropdownAutoClose.Inside => "inside",
DropdownAutoClose.Outside => "outside",
_ => throw new InvalidOperationException($"Unknown {nameof(DropdownAutoClose)} value {DropdownContainer.AutoClose}.")
_ => throw new InvalidOperationException($"Unknown {nameof(DropdownAutoClose)} value {AutoCloseEffective}.")
};
builder.AddAttribute(3, "data-bs-auto-close", dataBsAutoCloseAttributeValue);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ internal interface IHxDropdownToggle
string DropdownReference { get; set; }
EventCallback OnHidden { get; set; }
EventCallback OnShown { get; set; }
DropdownAutoClose? AutoClose { get; set; }


Task HandleJsHidden();
Task HandleJsShown();
Expand Down

0 comments on commit ef41932

Please sign in to comment.