2020-04-23 17:13:56 +08:00
|
|
|
|
using System;
|
2020-08-23 14:38:24 +08:00
|
|
|
|
using System.Linq.Expressions;
|
2020-04-23 17:13:56 +08:00
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using Microsoft.AspNetCore.Components;
|
|
|
|
|
|
2020-05-29 00:33:49 +08:00
|
|
|
|
namespace AntDesign
|
2020-04-23 17:13:56 +08:00
|
|
|
|
{
|
2021-04-25 01:04:21 +08:00
|
|
|
|
public partial class Checkbox : AntInputBoolComponentBase
|
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
|
|
|
|
|
2021-04-25 01:04:21 +08:00
|
|
|
|
//[Obsolete] attribute does not work with [Parameter] for now. Tracking issue: https://github.com/dotnet/aspnetcore/issues/30967
|
|
|
|
|
[Obsolete("Instead use @bing-Checked or EventCallback<bool> CheckedChanged .")]
|
|
|
|
|
[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 Indeterminate { get; set; }
|
2021-04-25 01:04:21 +08:00
|
|
|
|
[Parameter] public string Label { get; set; }
|
2020-04-23 17:13:56 +08:00
|
|
|
|
|
2021-04-25 01:04:21 +08:00
|
|
|
|
[CascadingParameter] public CheckboxGroup CheckboxGroup { get; set; }
|
2020-04-23 17:13:56 +08:00
|
|
|
|
|
2021-04-25 01:04:21 +08:00
|
|
|
|
internal bool IsFromOptions { get; set; }
|
|
|
|
|
private bool _isInitalized;
|
2020-04-24 18:32:50 +08:00
|
|
|
|
protected override void OnInitialized()
|
2020-04-23 17:13:56 +08:00
|
|
|
|
{
|
2021-04-25 01:04:21 +08:00
|
|
|
|
base.OnInitialized();
|
|
|
|
|
SetClass();
|
2020-08-31 23:41:33 +08:00
|
|
|
|
CheckboxGroup?.AddItem(this);
|
2021-04-25 01:04:21 +08:00
|
|
|
|
_isInitalized = true;
|
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);
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-25 01:04:21 +08:00
|
|
|
|
protected ClassMapper ClassMapperLabel { get; } = new ClassMapper();
|
2020-10-25 23:01:54 +08:00
|
|
|
|
|
2021-04-25 01:04:21 +08:00
|
|
|
|
private string _prefixCls = "ant-checkbox";
|
2020-04-23 17:13:56 +08:00
|
|
|
|
protected void SetClass()
|
|
|
|
|
{
|
2021-04-25 01:04:21 +08:00
|
|
|
|
ClassMapperLabel.Clear()
|
|
|
|
|
.Add($"{_prefixCls}-wrapper")
|
|
|
|
|
.If($"{_prefixCls}-wrapper-checked", () => Checked);
|
2020-04-23 17:13:56 +08:00
|
|
|
|
|
2021-04-25 01:04:21 +08:00
|
|
|
|
ClassMapper.Clear()
|
|
|
|
|
.Add(_prefixCls)
|
|
|
|
|
.If($"{_prefixCls}-checked", () => Checked && !Indeterminate)
|
|
|
|
|
.If($"{_prefixCls}-disabled", () => Disabled)
|
|
|
|
|
.If($"{_prefixCls}-indeterminate", () => Indeterminate)
|
|
|
|
|
.If($"{_prefixCls}-rtl", () => RTL);
|
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)
|
|
|
|
|
{
|
2021-04-25 01:04:21 +08:00
|
|
|
|
await base.ChangeValue(value);
|
2020-04-23 17:13:56 +08:00
|
|
|
|
|
2021-04-25 01:04:21 +08:00
|
|
|
|
if (CheckedChange.HasDelegate) //kept for compatibility reasons with previous versions
|
|
|
|
|
await CheckedChange.InvokeAsync(value);
|
2020-04-23 17:13:56 +08:00
|
|
|
|
CheckboxGroup?.OnCheckboxChange(this);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-25 01:04:21 +08:00
|
|
|
|
internal void SetValue(bool value) => Checked = value;
|
|
|
|
|
|
2020-04-23 17:13:56 +08:00
|
|
|
|
}
|
|
|
|
|
}
|