2020-04-23 17:13:56 +08:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
2020-08-23 14:38:24 +08:00
|
|
|
|
using System.Linq.Expressions;
|
2020-04-23 17:13:56 +08:00
|
|
|
|
using System.Threading.Tasks;
|
2020-10-25 23:01:54 +08:00
|
|
|
|
using AntDesign.core.JsInterop.EventArg;
|
2020-04-23 17:13:56 +08:00
|
|
|
|
using Microsoft.AspNetCore.Components;
|
2020-10-25 23:01:54 +08:00
|
|
|
|
using Microsoft.AspNetCore.Components.Web;
|
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
|
|
|
|
{
|
2020-08-23 14:38:24 +08:00
|
|
|
|
[Parameter] public RenderFragment ChildContent { get; set; }
|
2020-04-23 17:13:56 +08:00
|
|
|
|
|
2020-08-23 14:38:24 +08:00
|
|
|
|
[Parameter] public EventCallback<bool> CheckedChange { get; set; }
|
2020-04-23 17:13:56 +08:00
|
|
|
|
|
2020-08-23 14:38:24 +08:00
|
|
|
|
[Parameter] public Expression<Func<bool>> CheckedExpression { get; set; }
|
2020-06-02 20:27:53 +08:00
|
|
|
|
|
2020-08-23 14:38:24 +08:00
|
|
|
|
[Parameter] public bool AutoFocus { get; set; }
|
2020-04-23 17:13:56 +08:00
|
|
|
|
|
2020-08-23 14:38:24 +08:00
|
|
|
|
[Parameter] public bool Disabled { get; set; }
|
2020-04-23 17:13:56 +08:00
|
|
|
|
|
2020-08-23 14:38:24 +08:00
|
|
|
|
[Parameter] public bool Indeterminate { get; set; }
|
2020-04-23 17:13:56 +08:00
|
|
|
|
|
|
|
|
|
[Parameter]
|
2020-08-23 14:38:24 +08:00
|
|
|
|
public bool Checked
|
|
|
|
|
{
|
2020-10-25 23:01:54 +08:00
|
|
|
|
get => CurrentValue;
|
2020-08-23 14:38:24 +08:00
|
|
|
|
set
|
|
|
|
|
{
|
2020-10-25 23:01:54 +08:00
|
|
|
|
if (CurrentValue != value)
|
2020-08-23 14:38:24 +08:00
|
|
|
|
{
|
|
|
|
|
CurrentValue = value;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-04-23 17:13:56 +08:00
|
|
|
|
|
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
|
|
|
|
|
2020-10-25 23:01:54 +08:00
|
|
|
|
protected Dictionary<string, object> InputAttributes { get; set; } = new Dictionary<string, object>();
|
2020-04-23 17:13:56 +08:00
|
|
|
|
|
|
|
|
|
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-10-25 23:01:54 +08:00
|
|
|
|
UpdateAutoFocus();
|
2020-08-31 23:41:33 +08:00
|
|
|
|
CheckboxGroup?.AddItem(this);
|
2020-04-23 17:13:56 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-07-21 23:48:43 +08:00
|
|
|
|
protected override void Dispose(bool disposing)
|
|
|
|
|
{
|
2020-08-31 23:41:33 +08:00
|
|
|
|
CheckboxGroup?.RemoveItem(this);
|
2020-07-21 23:48:43 +08:00
|
|
|
|
base.Dispose(disposing);
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-25 23:01:54 +08:00
|
|
|
|
protected ClassMapper ClassMapperSpan { get; } = new ClassMapper();
|
|
|
|
|
|
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()
|
2020-10-25 23:01:54 +08:00
|
|
|
|
.Add(prefixName)
|
2021-03-12 17:02:11 +08:00
|
|
|
|
.Add($"{prefixName}-wrapper")
|
2020-10-25 23:01:54 +08:00
|
|
|
|
.If($"{prefixName}-wrapper-checked", () => Checked);
|
|
|
|
|
|
|
|
|
|
ClassMapperSpan.Clear()
|
2020-04-23 17:13:56 +08:00
|
|
|
|
.Add(prefixName)
|
|
|
|
|
.If($"{prefixName}-checked", () => Checked && !Indeterminate)
|
|
|
|
|
.If($"{prefixName}-disabled", () => Disabled)
|
2021-03-12 17:02:11 +08:00
|
|
|
|
.If($"{prefixName}-indeterminate", () => Indeterminate)
|
|
|
|
|
.If($"{prefixName}-rtl", () => RTL);
|
2020-04-23 17:13:56 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-07-30 23:54:31 +08:00
|
|
|
|
protected override void OnValueChange(bool value)
|
|
|
|
|
{
|
|
|
|
|
base.OnValueChange(value);
|
2020-10-25 23:01:54 +08:00
|
|
|
|
this.CurrentValue = value;
|
2020-07-30 23:54:31 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-04-23 17:13:56 +08:00
|
|
|
|
protected async Task InputCheckedChange(ChangeEventArgs args)
|
|
|
|
|
{
|
2020-04-24 18:32:50 +08:00
|
|
|
|
if (args != null && args.Value is bool value)
|
|
|
|
|
{
|
2020-07-30 23:54:31 +08:00
|
|
|
|
CurrentValue = value;
|
2020-04-24 18:32:50 +08:00
|
|
|
|
await InnerCheckedChange(value);
|
|
|
|
|
}
|
2020-04-23 17:13:56 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected async Task InnerCheckedChange(bool @checked)
|
|
|
|
|
{
|
|
|
|
|
if (!this.Disabled)
|
|
|
|
|
{
|
2020-07-30 23:54:31 +08:00
|
|
|
|
if (this.CheckedChange.HasDelegate)
|
|
|
|
|
{
|
|
|
|
|
await this.CheckedChange.InvokeAsync(@checked);
|
|
|
|
|
}
|
2020-04-23 17:13:56 +08:00
|
|
|
|
CheckboxGroup?.OnCheckboxChange(this);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected void UpdateAutoFocus()
|
|
|
|
|
{
|
|
|
|
|
if (this.AutoFocus)
|
|
|
|
|
{
|
2020-10-25 23:01:54 +08:00
|
|
|
|
if (InputAttributes.ContainsKey("autofocus") == false)
|
|
|
|
|
InputAttributes.Add("autofocus", "autofocus");
|
2020-04-23 17:13:56 +08:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2020-10-25 23:01:54 +08:00
|
|
|
|
if (InputAttributes.ContainsKey("autofocus") == true)
|
|
|
|
|
InputAttributes.Remove("autofocus");
|
2020-04-23 17:13:56 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|