2020-03-12 18:23:00 +08:00
|
|
|
@namespace AntBlazor
|
|
|
|
@inherits AntDomComponentBase
|
|
|
|
|
|
|
|
<button type="button" role="switch"
|
2020-04-23 17:13:56 +08:00
|
|
|
aria-checked="@_isChecked"
|
|
|
|
disabled="@Disabled"
|
2020-03-12 18:23:00 +08:00
|
|
|
class="@ClassMapper.Class"
|
|
|
|
style="@Style"
|
|
|
|
@onclick="@OnClickHandler">
|
2020-04-23 17:13:56 +08:00
|
|
|
@if (Loading)
|
2020-03-12 18:23:00 +08:00
|
|
|
{
|
|
|
|
<AntIcon type="loading" Class="@($"{prefixCls}-loading-icon")" />
|
|
|
|
}
|
|
|
|
<span class="@($"{prefixCls}-inner")">
|
2020-04-23 17:13:56 +08:00
|
|
|
@(_isChecked ? CheckedChildren : UnCheckedChildren)
|
2020-03-12 18:23:00 +08:00
|
|
|
</span>
|
|
|
|
</button>
|
|
|
|
|
|
|
|
@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]
|
|
|
|
public RenderFragment CheckedChildren { get; set; }
|
|
|
|
|
|
|
|
[Parameter]
|
|
|
|
public RenderFragment UnCheckedChildren { get; set; }
|
|
|
|
|
|
|
|
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
|
|
|
protected void OnClickHandler(MouseEventArgs ev)
|
|
|
|
{
|
2020-04-23 17:13:56 +08:00
|
|
|
if (!Disabled && !Loading && !Control)
|
2020-03-12 18:23:00 +08:00
|
|
|
{
|
2020-04-23 17:13:56 +08:00
|
|
|
this.updateValue(!this._isChecked);
|
2020-03-12 18:23:00 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void updateValue(bool value)
|
|
|
|
{
|
2020-04-23 17:13:56 +08:00
|
|
|
if (this._isChecked != value)
|
|
|
|
{
|
|
|
|
this._isChecked = value;
|
|
|
|
this.OnChange.InvokeAsync(this._isChecked);
|
2020-03-12 18:23:00 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|