Skip to content

Commit

Permalink
🆕 (Button): simplify the usage of icon buttons
Browse files Browse the repository at this point in the history
  • Loading branch information
capdiem committed Apr 21, 2024
1 parent a780286 commit 5450518
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 54 deletions.
62 changes: 50 additions & 12 deletions src/Component/BlazorComponent/Components/Button/BButton.razor
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,7 @@
__internal_preventDefault_onclick="@OnClickPreventDefault"
@attributes="Attributes">

@if (HasLoader)
{
@RenderPart(typeof(BButtonLoader<>))
}

@RenderPart(typeof(BButtonContent<>))
@GenContent()
</KeyTransitionElement>
}
else
Expand All @@ -39,13 +34,56 @@
__internal_stopPropagation_onclick="@OnClickStopPropagation"
__internal_preventDefault_onclick="@OnClickPreventDefault"
@attributes="Attributes">
@GenContent()
</ShowTransitionElement>
}
</CascadingValue>

@code {

@if (HasLoader)

RenderFragment GenContent() => __builder =>
{
if (HasLoader)
{
@if (Loading)
{
@RenderPart(typeof(BButtonLoader<>))
<div class="@CssProvider.GetClass("loader")" style="@CssProvider.GetStyle("loader")">
@if (LoaderContent == null)
{
<BProgressCircular @attributes="@GetAttributes(typeof(BProgressCircular))"></BProgressCircular>
}
else
{
@LoaderContent
}
</div>
}
}

@RenderPart(typeof(BButtonContent<>))
</ShowTransitionElement>
}
</CascadingValue>
<span class="@CssProvider.GetClass("content")" style="@CssProvider.GetStyle("content")">
@GenIcon("left")
@GenIcon()
@ChildContent
@GenIcon("right")
</span>
};

RenderFragment GenIcon(string? position = null) => __builder =>
{
var (icon, left, right) = position switch
{
"left" => (LeftIconName, true, false),
"right" => (RightIconName, false, true),
_ => (IconName, false, false)
};

if (string.IsNullOrWhiteSpace(icon))
{
return;
}

<BIcon Left="@left" Right="@right" @attributes="@GetAttributes(typeof(BIcon), data: position)">@icon</BIcon>
};

}
12 changes: 12 additions & 0 deletions src/Component/BlazorComponent/Components/Button/BButton.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,15 @@ protected BButton() : base(GroupType.ButtonGroup, "button")
[Parameter]
public string? Key { get; set; }

[Parameter] [MasaApiParameter(ReleasedOn = "v1.5.0")]
public string? IconName { get; set; }

[Parameter] [MasaApiParameter(ReleasedOn = "v1.5.0")]
public string? LeftIconName { get; set; }

[Parameter] [MasaApiParameter(ReleasedOn = "v1.5.0")]
public string? RightIconName { get; set; }

/// <summary>
/// Determine whether rendering a loader component
/// </summary>
Expand All @@ -71,6 +80,9 @@ protected BButton() : base(GroupType.ButtonGroup, "button")
/// Set the button's type attribute
/// </summary>
protected string TypeAttribute { get; set; } = "button";

[MemberNotNullWhen(true, nameof(IconName))]
protected bool HasBuiltInIcon => !string.IsNullOrWhiteSpace(IconName);

public bool IsDark
{
Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

0 comments on commit 5450518

Please sign in to comment.