2020-04-23 17:13:56 +08:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using Microsoft.AspNetCore.Components;
|
2020-06-02 20:27:53 +08:00
|
|
|
|
using System.Linq.Expressions;
|
2020-04-23 17:13:56 +08:00
|
|
|
|
|
2020-05-29 00:33:49 +08:00
|
|
|
|
namespace AntDesign
|
2020-04-23 17:13:56 +08:00
|
|
|
|
{
|
2020-06-02 20:27:53 +08:00
|
|
|
|
public partial class Checkbox : AntInputComponentBase<bool>
|
2020-04-23 17:13:56 +08:00
|
|
|
|
{
|
|
|
|
|
[Parameter]
|
|
|
|
|
public RenderFragment ChildContent { get; set; }
|
|
|
|
|
|
2020-04-24 18:32:50 +08:00
|
|
|
|
private ElementReference _inputElement;
|
|
|
|
|
private ElementReference _contentElement;
|
2020-04-23 17:13:56 +08:00
|
|
|
|
|
|
|
|
|
[Parameter]
|
|
|
|
|
public EventCallback<bool> CheckedChange { get; set; }
|
|
|
|
|
|
2020-06-02 20:27:53 +08:00
|
|
|
|
[Parameter]
|
|
|
|
|
public Expression<Func<bool>> CheckedExpression { get; set; }
|
|
|
|
|
|
2020-04-23 17:13:56 +08:00
|
|
|
|
[Parameter]
|
|
|
|
|
public bool AutoFocus { get; set; }
|
|
|
|
|
|
|
|
|
|
[Parameter]
|
|
|
|
|
public bool Disabled { get; set; }
|
|
|
|
|
|
|
|
|
|
[Parameter]
|
|
|
|
|
public bool Indeterminate { get; set; }
|
|
|
|
|
|
|
|
|
|
[Parameter]
|
|
|
|
|
public bool Checked { get; set; }
|
|
|
|
|
|
2020-06-02 20:27:53 +08:00
|
|
|
|
[Parameter]
|
|
|
|
|
public string Label { get; set; }
|
|
|
|
|
|
2020-04-23 17:13:56 +08:00
|
|
|
|
[CascadingParameter]
|
2020-05-28 16:56:52 +08:00
|
|
|
|
public CheckboxGroup CheckboxGroup { get; set; }
|
2020-04-23 17:13:56 +08:00
|
|
|
|
|
|
|
|
|
protected Dictionary<string, object> InputAttributes { get; set; }
|
|
|
|
|
|
|
|
|
|
protected override void OnParametersSet()
|
|
|
|
|
{
|
|
|
|
|
SetClass();
|
|
|
|
|
base.OnParametersSet();
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-24 18:32:50 +08:00
|
|
|
|
protected override void OnInitialized()
|
2020-04-23 17:13:56 +08:00
|
|
|
|
{
|
2020-05-28 16:56:52 +08:00
|
|
|
|
if (this is Checkbox checkbox)
|
2020-04-23 17:13:56 +08:00
|
|
|
|
{
|
|
|
|
|
CheckboxGroup?.CheckboxItems.Add(checkbox);
|
|
|
|
|
}
|
2020-06-02 20:27:53 +08:00
|
|
|
|
|
2020-06-03 20:29:28 +08:00
|
|
|
|
Value = Checked;
|
2020-04-23 17:13:56 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected void SetClass()
|
|
|
|
|
{
|
2020-04-24 18:32:50 +08:00
|
|
|
|
string prefixName = "ant-checkbox";
|
2020-04-23 17:13:56 +08:00
|
|
|
|
ClassMapper.Clear()
|
|
|
|
|
.Add(prefixName)
|
|
|
|
|
.If($"{prefixName}-checked", () => Checked && !Indeterminate)
|
|
|
|
|
.If($"{prefixName}-disabled", () => Disabled)
|
|
|
|
|
.If($"{prefixName}-indeterminate", () => Indeterminate);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected async Task InputCheckedChange(ChangeEventArgs args)
|
|
|
|
|
{
|
2020-04-24 18:32:50 +08:00
|
|
|
|
if (args != null && args.Value is bool value)
|
|
|
|
|
{
|
|
|
|
|
await InnerCheckedChange(value);
|
2020-06-02 20:27:53 +08:00
|
|
|
|
|
2020-06-03 20:29:28 +08:00
|
|
|
|
CurrentValue = Checked;
|
2020-04-24 18:32:50 +08:00
|
|
|
|
}
|
2020-04-23 17:13:56 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected async Task InnerCheckedChange(bool @checked)
|
|
|
|
|
{
|
|
|
|
|
if (!this.Disabled)
|
|
|
|
|
{
|
|
|
|
|
this.Checked = @checked;
|
2020-06-02 20:27:53 +08:00
|
|
|
|
|
2020-04-23 17:13:56 +08:00
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|