ant-design-blazor/components/icon/Icon.razor.cs

122 lines
3.1 KiB
C#
Raw Normal View History

using System.Threading.Tasks;
2019-12-11 00:23:10 +08:00
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Components.Web;
namespace AntDesign
2019-12-11 00:23:10 +08:00
{
public partial class Icon : AntDomComponentBase
2019-12-11 00:23:10 +08:00
{
[Parameter]
public bool Spin { get; set; }
2019-12-11 00:23:10 +08:00
[Parameter]
public int Rotate { get; set; } = 0;
2019-12-11 00:23:10 +08:00
[Parameter]
public string Type { get; set; }
2019-12-11 00:23:10 +08:00
/// <summary>
/// 'fill' | 'outline' | 'twotone';
/// </summary>
2019-12-11 00:23:10 +08:00
[Parameter]
public string Theme { get; set; } = IconThemeType.Outline;
2019-12-11 00:23:10 +08:00
[Parameter]
public string TwotoneColor { get; set; }
2019-12-11 00:23:10 +08:00
[Parameter]
public string IconFont { get; set; }
2019-12-11 00:23:10 +08:00
[Parameter]
public string Width { get; set; } = "1em";
2019-12-11 00:23:10 +08:00
[Parameter]
public string Height { get; set; } = "1em";
2019-12-11 00:23:10 +08:00
[Parameter]
public string Fill { get; set; } = "currentColor";
[Parameter]
public string TabIndex { get; set; }
2019-12-11 00:23:10 +08:00
[Parameter]
public bool StopPropagation { get; set; }
2019-12-16 15:33:29 +08:00
[CascadingParameter]
public Button Button { get; set; }
2019-12-16 15:33:29 +08:00
2019-12-11 00:23:10 +08:00
[Parameter]
public EventCallback<MouseEventArgs> OnClick { get; set; }
2019-12-11 00:23:10 +08:00
[Inject]
public IconService IconService { get; set; }
2019-12-11 00:23:10 +08:00
[Parameter]
public RenderFragment Component { get; set; }
2019-12-11 00:23:10 +08:00
protected string _svgImg;
2019-12-11 00:23:10 +08:00
protected override void Dispose(bool disposing)
{
Button?.Icons.Remove(this);
base.Dispose(disposing);
}
2019-12-11 00:23:10 +08:00
protected override async Task OnInitializedAsync()
{
if (Type == "loading")
{
Spin = true;
}
await SetupSvgImg();
2019-12-16 15:33:29 +08:00
Button?.Icons.Add(this);
2019-12-16 15:33:29 +08:00
ClassMapper.Add($"anticon")
.GetIf(() => $"anticon-{Type}", () => !string.IsNullOrWhiteSpace(Type));
await base.OnInitializedAsync();
2019-12-11 00:23:10 +08:00
}
protected override async Task OnParametersSetAsync()
2019-12-11 00:23:10 +08:00
{
await SetupSvgImg();
await base.OnParametersSetAsync();
2019-12-11 00:23:10 +08:00
}
protected virtual async Task SetupSvgImg()
2019-12-11 00:23:10 +08:00
{
if (Component != null)
{
return;
2020-04-24 18:32:50 +08:00
}
string svgClass = Spin ? "anticon-spin" : null;
if (!string.IsNullOrEmpty(IconFont))
2020-04-24 18:32:50 +08:00
{
var svg = $"<svg><use xlink:href=#{IconFont} /></svg>";
_svgImg = IconService.GetStyledSvg(svg, svgClass, Width, Height, Fill, Rotate);
StateHasChanged();
}
else
{
var svg = IconService.GetIconImg(Type.ToLowerInvariant(), Theme.ToLowerInvariant());
_svgImg = IconService.GetStyledSvg(svg, svgClass, Width, Height, Fill, Rotate);
await InvokeAsync(StateHasChanged);
}
2019-12-11 00:23:10 +08:00
}
2019-12-16 15:33:29 +08:00
private async Task HandleOnClick(MouseEventArgs args)
2019-12-16 15:33:29 +08:00
{
if (OnClick.HasDelegate)
2019-12-16 15:33:29 +08:00
{
await OnClick.InvokeAsync(args);
2019-12-16 15:33:29 +08:00
}
}
2019-12-11 00:23:10 +08:00
}
}