ant-design-blazor/components/spin/Spin.razor.cs
James Yeung 83ef34b5f9 feat: add component spin (#114)
* feat: add component spin

* docs: finish spin demo

* fix: re
2020-05-15 14:31:12 +08:00

101 lines
2.6 KiB
C#

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Timers;
using Microsoft.AspNetCore.Components;
namespace AntBlazor
{
public partial class Spin : AntDomComponentBase
{
/// <summary>
/// small | default | large
/// </summary>
[Parameter]
public string Size { get; set; } = "default";
[Parameter]
public string Tip { get; set; } = null;
[Parameter]
public int Delay { get; set; } = 0;
[Parameter]
public bool Spinning { get; set; } = true;
[Parameter]
public string WrapperClassName { get; set; }
[Parameter]
public RenderFragment Indicator { get; set; }
[Parameter]
public RenderFragment ChildContent { get; set; }
private ClassMapper WrapperClassMapper { get; set; } = new ClassMapper();
private const string PrefixCls = "ant-spin";
private bool _isLoading = true;
private bool Simple => ChildContent == null;
private string ContainerClass => _isLoading ? $"{PrefixCls}-blur" : "";
private Timer _delayTimer;
private void SetClass()
{
WrapperClassMapper
.If(WrapperClassName, () => !string.IsNullOrWhiteSpace(WrapperClassName))
.If($"{PrefixCls}-nested-loading", () => !Simple);
ClassMapper.Add(PrefixCls)
.If($"{PrefixCls}-spinning", () => _isLoading)
.If($"{PrefixCls}-lg", () => Size == "large")
.If($"{PrefixCls}-sm", () => Size == "small")
.If($"{PrefixCls}-show-text", () => string.IsNullOrWhiteSpace(Tip));
}
protected override void OnInitialized()
{
SetClass();
_isLoading = Spinning;
if (Delay > 0)
{
_delayTimer = new Timer(Delay);
_delayTimer.Elapsed += DelayElapsed;
}
base.OnInitialized();
}
protected override void OnParametersSet()
{
base.OnParametersSet();
if (_delayTimer != null)
{
if (_isLoading != Spinning)
{
_delayTimer.Stop();
_delayTimer.Start();
}
else
{
_delayTimer.Stop();
}
}
}
private void DelayElapsed(object sender, ElapsedEventArgs args)
{
_isLoading = Spinning;
InvokeAsync(StateHasChanged);
}
}
}