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

128 lines
3.4 KiB
C#
Raw Normal View History

2019-12-11 00:23:10 +08:00
using System;
2019-12-17 10:21:42 +08:00
using System.Collections.Concurrent;
2019-12-11 00:23:10 +08:00
using System.Net.Http;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Components.Web;
using OneOf;
2019-12-11 00:23:10 +08:00
namespace AntBlazor
{
public partial class AntIcon : 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 OneOf<int, string> 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; } = AntIconThemeType.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
2019-12-16 15:33:29 +08:00
[CascadingParameter]
public AntButton Button { get; set; }
2019-12-11 00:23:10 +08:00
[Parameter]
public EventCallback<MouseEventArgs> Onclick { get; set; }
2019-12-11 00:23:10 +08:00
[Inject]
private HttpClient HttpClient { get; set; }
2019-12-11 00:23:10 +08:00
[Inject]
private NavigationManager NavigationManager { get; set; }
private static readonly ConcurrentDictionary<string, string> _svgCache = new ConcurrentDictionary<string, string>();
private string SvgImg { get; set; }
2019-12-11 00:23:10 +08:00
private string SvgStyle { get; set; }
private string _iconSvg;
private Uri _baseUrl;
public void SetClassSet()
2019-12-11 00:23:10 +08:00
{
string prefixName = "anticon";
ClassMapper.Add(prefixName)
.If($"{prefixName}-spin", () => Spin || this.Type == "loading");
2019-12-11 00:23:10 +08:00
SvgStyle = $"focusable=\"false\" width=\"{Width}\" height=\"{Height}\" fill=\"{Fill}\"";
2019-12-11 00:23:10 +08:00
}
protected override async Task OnInitializedAsync()
{
this.SetClassSet();
2019-12-18 16:57:00 +08:00
_baseUrl = NavigationManager.ToAbsoluteUri(NavigationManager.BaseUri);
2019-12-16 15:33:29 +08:00
if (this is AntIcon icon)
{
Button?.Icons.Add(icon);
}
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 async Task SetupSvgImg()
2019-12-11 00:23:10 +08:00
{
try
{
if (_svgCache.TryGetValue($"{Theme}-{Type}", out var svg))
{
_iconSvg = svg;
}
else
{
_iconSvg = await HttpClient.GetStringAsync(new Uri(_baseUrl, $"_content/AntBlazor/icons/{Theme.ToLower()}/{Type.ToLower()}.svg"));
_svgCache.TryAdd($"{Theme}-{Type}", _iconSvg);
}
SvgImg = _iconSvg.Insert(_iconSvg.IndexOf("svg", StringComparison.Ordinal) + 3, $" {SvgStyle} ");
}
catch
{
// ignored
}
2019-12-18 16:57:00 +08:00
StateHasChanged();
2019-12-11 00:23:10 +08:00
}
2019-12-16 15:33:29 +08:00
public async Task OnClick(MouseEventArgs args)
{
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
}
}