mirror of
https://gitee.com/ant-design-blazor/ant-design-blazor.git
synced 2024-12-11 09:54:01 +08:00
826df9d055
* feat: new demo for switch, radio, slider and inputnumber * fix: remove some blank in demo doc api section * fix: format the code in demo * fix: checkbox and switch Co-authored-by: ElderJames <shunjiey@hotmail.com>
99 lines
2.5 KiB
C#
99 lines
2.5 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Threading.Tasks;
|
|
using Microsoft.AspNetCore.Components;
|
|
|
|
namespace AntBlazor
|
|
{
|
|
public partial class Checkbox : AntDomComponentBase
|
|
{
|
|
[Parameter]
|
|
public RenderFragment ChildContent { get; set; }
|
|
|
|
private ElementReference _inputElement;
|
|
private ElementReference _contentElement;
|
|
|
|
[Parameter]
|
|
public EventCallback<bool> CheckedChange { get; set; }
|
|
|
|
[Parameter]
|
|
public string Value { get; set; }
|
|
|
|
[Parameter]
|
|
public bool AutoFocus { get; set; }
|
|
|
|
[Parameter]
|
|
public bool Disabled { get; set; }
|
|
|
|
[Parameter]
|
|
public bool Indeterminate { get; set; }
|
|
|
|
[Parameter]
|
|
public bool Checked { get; set; }
|
|
|
|
[CascadingParameter]
|
|
public CheckboxGroup CheckboxGroup { get; set; }
|
|
|
|
protected Dictionary<string, object> InputAttributes { get; set; }
|
|
|
|
protected override void OnParametersSet()
|
|
{
|
|
SetClass();
|
|
base.OnParametersSet();
|
|
}
|
|
|
|
protected override void OnInitialized()
|
|
{
|
|
if (this is Checkbox checkbox)
|
|
{
|
|
CheckboxGroup?.CheckboxItems.Add(checkbox);
|
|
}
|
|
}
|
|
|
|
protected void SetClass()
|
|
{
|
|
string prefixName = "ant-checkbox";
|
|
ClassMapper.Clear()
|
|
.Add(prefixName)
|
|
.If($"{prefixName}-checked", () => Checked && !Indeterminate)
|
|
.If($"{prefixName}-disabled", () => Disabled)
|
|
.If($"{prefixName}-indeterminate", () => Indeterminate);
|
|
}
|
|
|
|
protected async Task InputCheckedChange(ChangeEventArgs args)
|
|
{
|
|
if (args != null && args.Value is bool value)
|
|
{
|
|
await InnerCheckedChange(value);
|
|
}
|
|
}
|
|
|
|
protected async Task InnerCheckedChange(bool @checked)
|
|
{
|
|
if (!this.Disabled)
|
|
{
|
|
this.Checked = @checked;
|
|
await this.CheckedChange.InvokeAsync(this.Checked);
|
|
CheckboxGroup?.OnCheckboxChange(this);
|
|
}
|
|
}
|
|
|
|
protected void UpdateAutoFocus()
|
|
{
|
|
if (this.AutoFocus)
|
|
{
|
|
InputAttributes.Add("autofocus", "autofocus");
|
|
}
|
|
else
|
|
{
|
|
InputAttributes.Remove("autofocus");
|
|
}
|
|
}
|
|
|
|
protected void WriteValue(bool value)
|
|
{
|
|
this.Checked = value;
|
|
}
|
|
}
|
|
}
|