mirror of
https://gitee.com/ant-design-blazor/ant-design-blazor.git
synced 2024-12-15 01:11:52 +08:00
b067385d79
Co-authored-by: James Yeung <shunjiey@hotmail.com>
69 lines
2.0 KiB
C#
69 lines
2.0 KiB
C#
using System.Threading.Tasks;
|
|
using Microsoft.AspNetCore.Components;
|
|
using Microsoft.AspNetCore.Components.Web;
|
|
|
|
namespace AntDesign
|
|
{
|
|
public partial class Switch : AntInputBoolComponentBase
|
|
{
|
|
[Parameter]
|
|
public bool Loading { get; set; }
|
|
|
|
[Parameter]
|
|
public string CheckedChildren { get; set; } = string.Empty;
|
|
|
|
[Parameter]
|
|
public RenderFragment CheckedChildrenTemplate { get; set; }
|
|
|
|
/// <summary>
|
|
/// The status of Switch is completely up to the user and no longer
|
|
/// automatically changes the data based on the click event.
|
|
/// </summary>
|
|
[Parameter]
|
|
public bool Control { get; set; }
|
|
|
|
[Parameter]
|
|
public EventCallback OnClick { get; set; }
|
|
|
|
[Parameter]
|
|
public string UnCheckedChildren { get; set; } = string.Empty;
|
|
|
|
[Parameter]
|
|
public RenderFragment UnCheckedChildrenTemplate { get; set; }
|
|
|
|
private bool _clickAnimating = false;
|
|
|
|
private string _prefixCls = "ant-switch";
|
|
|
|
protected override void OnInitialized()
|
|
{
|
|
base.OnInitialized();
|
|
SetClass();
|
|
}
|
|
|
|
protected void SetClass()
|
|
{
|
|
ClassMapper.Clear()
|
|
.Add(_prefixCls)
|
|
.If($"{_prefixCls}-checked", () => CurrentValue)
|
|
.If($"{_prefixCls}-disabled", () => Disabled || Loading)
|
|
.If($"{_prefixCls}-loading", () => Loading)
|
|
.If($"{_prefixCls}-small", () => Size == "small")
|
|
.If($"{_prefixCls}-rtl", () => RTL);
|
|
}
|
|
|
|
private async Task HandleClick(MouseEventArgs e)
|
|
{
|
|
if (!Control)
|
|
await base.ChangeValue(!CurrentValue);
|
|
if (OnClick.HasDelegate)
|
|
await OnClick.InvokeAsync(null);
|
|
}
|
|
|
|
private void HandleMouseOver(MouseEventArgs e) => _clickAnimating = true;
|
|
|
|
private void HandleMouseOut(MouseEventArgs e) => _clickAnimating = false;
|
|
|
|
}
|
|
}
|