ant-design-blazor/components/switch/Switch.razor.cs
TimChen cbc5e823f0 refactor(module: all): separate the normal and template parameters (#552)
* fix: card title template

* fix: template:badge,collapse

* fix: comment refactor template

* fix: ribbonTests

* feat: descriptions refactor template

* feat: empty refactor template

* feat: list refactor template

* feat: menu refactor template

* feat: confirm add question icon

* feat: pageHeader and statistic refactor template

* feat: popconfirm refactor template

* feat: popver refactor template

* feat: result refactor template

* feat: step refactor template

* feat: switch refactor template

* feat: table refactor template

* feat: transfer refactor template

* feat: optimized code

* fix: pageheader

* refactor(module: empty): remove empty image constant images

Co-authored-by: ElderJames <shunjiey@hotmail.com>
2020-09-16 13:58:16 +08:00

77 lines
2.0 KiB
C#

using System.Threading.Tasks;
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Components.Web;
using OneOf;
namespace AntDesign
{
public partial class Switch : AntInputComponentBase<bool>
{
protected string _prefixCls = "ant-switch";
[Parameter]
public bool Checked { get; set; }
[Parameter]
public bool Disabled { get; set; }
[Parameter]
public bool Loading { get; set; }
[Parameter]
public bool Control { get; set; }
[Parameter]
public EventCallback<bool> OnChange { get; set; }
[Parameter]
public string CheckedChildren { get; set; } = string.Empty;
[Parameter]
public RenderFragment CheckedChildrenTemplate { get; set; }
[Parameter]
public string UnCheckedChildren { get; set; } = string.Empty;
[Parameter]
public RenderFragment UnCheckedChildrenTemplate { get; set; }
private bool _clickAnimating = false;
protected override void OnInitialized()
{
base.OnInitialized();
this.CurrentValue = this.CurrentValue ? this.CurrentValue : this.Checked;
ClassMapper.Clear()
.Add(_prefixCls)
.If($"{_prefixCls}-checked", () => CurrentValue)
.If($"{_prefixCls}-disabled", () => Disabled || Loading)
.If($"{_prefixCls}-loading", () => Loading)
.If($"{_prefixCls}-small", () => Size == "small")
;
}
private void HandleClick(MouseEventArgs e)
{
if (!Disabled && !Loading && !Control)
{
this.CurrentValue = !CurrentValue;
this.OnChange.InvokeAsync(CurrentValue);
}
}
private void HandleMouseOver(MouseEventArgs e)
{
_clickAnimating = true;
}
private void HandleMouseOut(MouseEventArgs e)
{
_clickAnimating = false;
}
}
}