2020-05-29 00:33:49 +08:00
|
|
|
|
@namespace AntDesign
|
2020-05-18 14:46:42 +08:00
|
|
|
|
@using OneOf;
|
2020-05-28 16:56:52 +08:00
|
|
|
|
@using System.Timers
|
2020-03-12 18:23:00 +08:00
|
|
|
|
@inherits AntDomComponentBase
|
|
|
|
|
|
2020-05-28 15:44:07 +08:00
|
|
|
|
<button type="button" role="switch"
|
2020-05-28 16:56:52 +08:00
|
|
|
|
aria-checked="@(_isChecked?"true":"false")"
|
2020-05-28 15:44:07 +08:00
|
|
|
|
disabled="@Disabled"
|
|
|
|
|
class="@ClassMapper.Class"
|
|
|
|
|
style="@Style"
|
2020-05-28 16:56:52 +08:00
|
|
|
|
ant-click-animating="@(clickAnimating?"true":"false")"
|
|
|
|
|
@onmouseover="HandleMouseOver"
|
|
|
|
|
@onmouseout="HandleMouseOut"
|
|
|
|
|
@onclick="HandleClick">
|
2020-05-28 15:44:07 +08:00
|
|
|
|
@if (Loading)
|
2020-05-18 14:46:42 +08:00
|
|
|
|
{
|
2020-05-28 15:44:07 +08:00
|
|
|
|
<AntIcon Type="loading" Class="@($"{prefixCls}-loading-icon")" />
|
2020-05-18 14:46:42 +08:00
|
|
|
|
}
|
2020-05-28 15:44:07 +08:00
|
|
|
|
<div class="@($"{prefixCls}-handle")"></div>
|
|
|
|
|
<span class="@($"{prefixCls}-inner")">
|
|
|
|
|
@if (_isChecked)
|
2020-05-18 14:46:42 +08:00
|
|
|
|
{
|
2020-05-28 15:44:07 +08:00
|
|
|
|
if (CheckedChildren.IsT0)
|
|
|
|
|
{
|
|
|
|
|
@(CheckedChildren.AsT0)
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
@(CheckedChildren.AsT1)
|
|
|
|
|
}
|
2020-05-18 14:46:42 +08:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2020-05-28 15:44:07 +08:00
|
|
|
|
if (UnCheckedChildren.IsT0)
|
|
|
|
|
{
|
|
|
|
|
@(UnCheckedChildren.AsT0)
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
@(UnCheckedChildren.AsT1)
|
|
|
|
|
}
|
2020-05-18 14:46:42 +08:00
|
|
|
|
}
|
2020-05-28 15:44:07 +08:00
|
|
|
|
</span>
|
2020-05-28 16:56:52 +08:00
|
|
|
|
|
|
|
|
|
<!--animation-->
|
|
|
|
|
@if (_isChecked)
|
|
|
|
|
{
|
|
|
|
|
<div class="ant-click-animating-node"></div>
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
<div class="ant-click-animating-node"></div>
|
|
|
|
|
}
|
2020-05-28 15:44:07 +08:00
|
|
|
|
</button>
|
2020-03-12 18:23:00 +08:00
|
|
|
|
|
|
|
|
|
@code {
|
|
|
|
|
|
|
|
|
|
protected string prefixCls = "ant-switch";
|
|
|
|
|
|
|
|
|
|
protected override Task OnParametersSetAsync()
|
|
|
|
|
{
|
|
|
|
|
ClassMapper.Clear()
|
|
|
|
|
.Add(prefixCls)
|
2020-04-23 17:13:56 +08:00
|
|
|
|
.If($"{prefixCls}-checked", () => _isChecked)
|
|
|
|
|
.If($"{prefixCls}-disabled", () => Disabled || Loading)
|
|
|
|
|
.If($"{prefixCls}-loading", () => Loading)
|
|
|
|
|
.If($"{prefixCls}-small", () => Size.Equals("small"))
|
2020-03-12 18:23:00 +08:00
|
|
|
|
;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return base.OnParametersSetAsync();
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-23 17:13:56 +08:00
|
|
|
|
private bool _isChecked = false;
|
2020-03-12 18:23:00 +08:00
|
|
|
|
|
|
|
|
|
[Parameter]
|
2020-04-23 17:13:56 +08:00
|
|
|
|
public bool Checked { get; set; }
|
2020-03-12 18:23:00 +08:00
|
|
|
|
|
|
|
|
|
[Parameter]
|
2020-04-23 17:13:56 +08:00
|
|
|
|
public bool Disabled { get; set; }
|
2020-03-12 18:23:00 +08:00
|
|
|
|
|
|
|
|
|
[Parameter]
|
2020-04-23 17:13:56 +08:00
|
|
|
|
public bool Loading { get; set; }
|
2020-03-12 18:23:00 +08:00
|
|
|
|
|
|
|
|
|
[Parameter]
|
2020-04-23 17:13:56 +08:00
|
|
|
|
public bool Control { get; set; }
|
2020-03-12 18:23:00 +08:00
|
|
|
|
|
|
|
|
|
[Parameter]
|
2020-04-23 17:13:56 +08:00
|
|
|
|
public string Size { get; set; } = "default";
|
2020-03-12 18:23:00 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
[Parameter]
|
|
|
|
|
public EventCallback<bool> OnChange { get; set; }
|
|
|
|
|
|
|
|
|
|
[Parameter]
|
2020-05-18 14:46:42 +08:00
|
|
|
|
public OneOf<string,RenderFragment> CheckedChildren { get; set; }
|
2020-03-12 18:23:00 +08:00
|
|
|
|
|
|
|
|
|
[Parameter]
|
2020-05-18 14:46:42 +08:00
|
|
|
|
public OneOf<string,RenderFragment> UnCheckedChildren { get; set; }
|
2020-03-12 18:23:00 +08:00
|
|
|
|
|
2020-05-28 16:56:52 +08:00
|
|
|
|
private bool clickAnimating = false;
|
2020-03-12 18:23:00 +08:00
|
|
|
|
|
|
|
|
|
protected override void OnInitialized()
|
|
|
|
|
{
|
2020-04-23 17:13:56 +08:00
|
|
|
|
this._isChecked = Checked;
|
2020-03-12 18:23:00 +08:00
|
|
|
|
base.OnInitialized();
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-28 16:56:52 +08:00
|
|
|
|
private void UpdateValue(bool value)
|
2020-03-12 18:23:00 +08:00
|
|
|
|
{
|
2020-05-28 16:56:52 +08:00
|
|
|
|
if (this._isChecked != value)
|
2020-03-12 18:23:00 +08:00
|
|
|
|
{
|
2020-05-28 16:56:52 +08:00
|
|
|
|
this._isChecked = value;
|
|
|
|
|
this.OnChange.InvokeAsync(this._isChecked);
|
2020-03-12 18:23:00 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-28 16:56:52 +08:00
|
|
|
|
private void HandleClick(MouseEventArgs ev)
|
2020-03-12 18:23:00 +08:00
|
|
|
|
{
|
2020-05-28 16:56:52 +08:00
|
|
|
|
if (!Disabled && !Loading && !Control)
|
2020-04-23 17:13:56 +08:00
|
|
|
|
{
|
2020-05-28 16:56:52 +08:00
|
|
|
|
this.UpdateValue(!this._isChecked);
|
2020-03-12 18:23:00 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-28 16:56:52 +08:00
|
|
|
|
private void HandleMouseOver(MouseEventArgs e)
|
|
|
|
|
{
|
|
|
|
|
clickAnimating = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void HandleMouseOut(MouseEventArgs e)
|
|
|
|
|
{
|
|
|
|
|
clickAnimating = false;
|
|
|
|
|
}
|
2020-03-12 18:23:00 +08:00
|
|
|
|
}
|