Skip to content

Commit 494fc41

Browse files
authored
πŸ†• (Button): simplify the usage of icon buttons (#607)
1 parent f828fca commit 494fc41

6 files changed

Lines changed: 62 additions & 54 deletions

File tree

β€Žsrc/Component/BlazorComponent/Components/Button/BButton.razorβ€Ž

Lines changed: 50 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,7 @@
1717
__internal_preventDefault_onclick="@OnClickPreventDefault"
1818
@attributes="Attributes">
1919

20-
@if (HasLoader)
21-
{
22-
@RenderPart(typeof(BButtonLoader<>))
23-
}
24-
25-
@RenderPart(typeof(BButtonContent<>))
20+
@GenContent()
2621
</KeyTransitionElement>
2722
}
2823
else
@@ -39,13 +34,56 @@
3934
__internal_stopPropagation_onclick="@OnClickStopPropagation"
4035
__internal_preventDefault_onclick="@OnClickPreventDefault"
4136
@attributes="Attributes">
37+
@GenContent()
38+
</ShowTransitionElement>
39+
}
40+
</CascadingValue>
41+
42+
@code {
4243

43-
@if (HasLoader)
44+
45+
RenderFragment GenContent() => __builder =>
46+
{
47+
if (HasLoader)
48+
{
49+
@if (Loading)
4450
{
45-
@RenderPart(typeof(BButtonLoader<>))
51+
<div class="@CssProvider.GetClass("loader")" style="@CssProvider.GetStyle("loader")">
52+
@if (LoaderContent == null)
53+
{
54+
<BProgressCircular @attributes="@GetAttributes(typeof(BProgressCircular))"></BProgressCircular>
55+
}
56+
else
57+
{
58+
@LoaderContent
59+
}
60+
</div>
4661
}
62+
}
4763

48-
@RenderPart(typeof(BButtonContent<>))
49-
</ShowTransitionElement>
50-
}
51-
</CascadingValue>
64+
<span class="@CssProvider.GetClass("content")" style="@CssProvider.GetStyle("content")">
65+
@GenIcon("left")
66+
@GenIcon()
67+
@ChildContent
68+
@GenIcon("right")
69+
</span>
70+
};
71+
72+
RenderFragment GenIcon(string? position = null) => __builder =>
73+
{
74+
var (icon, left, right) = position switch
75+
{
76+
"left" => (LeftIconName, true, false),
77+
"right" => (RightIconName, false, true),
78+
_ => (IconName, false, false)
79+
};
80+
81+
if (string.IsNullOrWhiteSpace(icon))
82+
{
83+
return;
84+
}
85+
86+
<BIcon Left="@left" Right="@right" @attributes="@GetAttributes(typeof(BIcon), data: position)">@icon</BIcon>
87+
};
88+
89+
}

β€Žsrc/Component/BlazorComponent/Components/Button/BButton.razor.csβ€Ž

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,15 @@ protected BButton() : base(GroupType.ButtonGroup, "button")
6262
[Parameter]
6363
public string? Key { get; set; }
6464

65+
[Parameter] [MasaApiParameter(ReleasedOn = "v1.5.0")]
66+
public string? IconName { get; set; }
67+
68+
[Parameter] [MasaApiParameter(ReleasedOn = "v1.5.0")]
69+
public string? LeftIconName { get; set; }
70+
71+
[Parameter] [MasaApiParameter(ReleasedOn = "v1.5.0")]
72+
public string? RightIconName { get; set; }
73+
6574
/// <summary>
6675
/// Determine whether rendering a loader component
6776
/// </summary>
@@ -71,6 +80,9 @@ protected BButton() : base(GroupType.ButtonGroup, "button")
7180
/// Set the button's type attribute
7281
/// </summary>
7382
protected string TypeAttribute { get; set; } = "button";
83+
84+
[MemberNotNullWhen(true, nameof(IconName))]
85+
protected bool HasBuiltInIcon => !string.IsNullOrWhiteSpace(IconName);
7486

7587
public bool IsDark
7688
{

β€Žsrc/Component/BlazorComponent/Components/Button/Content/BButtonContent.razorβ€Ž

Lines changed: 0 additions & 7 deletions
This file was deleted.

β€Žsrc/Component/BlazorComponent/Components/Button/Content/BButtonContent.razor.csβ€Ž

Lines changed: 0 additions & 8 deletions
This file was deleted.

β€Žsrc/Component/BlazorComponent/Components/Button/Loader/BButtonLoader.razorβ€Ž

Lines changed: 0 additions & 17 deletions
This file was deleted.

β€Žsrc/Component/BlazorComponent/Components/Button/Loader/BButtonLoader.razor.csβ€Ž

Lines changed: 0 additions & 10 deletions
This file was deleted.

0 commit comments

Comments
Β (0)