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;
|
2024-04-02 22:15:48 +08:00
|
|
|
|
using AntDesign.Internal;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
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
|
|
|
|
{
|
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; }
|
2024-04-02 22:15:48 +08:00
|
|
|
|
|
2021-04-25 01:04:21 +08:00
|
|
|
|
[Parameter] public string Label { get; set; }
|
2020-04-23 17:13:56 +08:00
|
|
|
|
|
2024-04-02 22:15:48 +08:00
|
|
|
|
[CascadingParameter] private ICheckboxGroup CheckboxGroup { get; set; }
|
|
|
|
|
|
|
|
|
|
[CascadingParameter(Name = "ItemValue")]
|
|
|
|
|
internal object ItemValue { get; set; }
|
2020-04-23 17:13:56 +08:00
|
|
|
|
|
2021-04-25 01:04:21 +08:00
|
|
|
|
internal bool IsFromOptions { get; set; }
|
2023-07-27 22:48:11 +08:00
|
|
|
|
|
|
|
|
|
private bool IsDisabled => Disabled || (CheckboxGroup?.Disabled ?? false);
|
|
|
|
|
|
2024-04-02 22:15:48 +08:00
|
|
|
|
private Dictionary<string, object> _attributes;
|
|
|
|
|
|
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);
|
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);
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-02 22:15:48 +08:00
|
|
|
|
protected override void OnParametersSet()
|
|
|
|
|
{
|
|
|
|
|
_attributes = AdditionalAttributes?.ToDictionary(x => x.Key, x => x.Value) ?? [];
|
|
|
|
|
|
|
|
|
|
var name = CheckboxGroup?.NameAttributeValue ?? NameAttributeValue;
|
|
|
|
|
if (!string.IsNullOrWhiteSpace(name))
|
|
|
|
|
{
|
|
|
|
|
_attributes.TryAdd("name", name);
|
|
|
|
|
}
|
|
|
|
|
base.OnParametersSet();
|
|
|
|
|
}
|
|
|
|
|
|
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";
|
2023-07-27 22:48:11 +08:00
|
|
|
|
|
2020-04-23 17:13:56 +08:00
|
|
|
|
protected void SetClass()
|
|
|
|
|
{
|
2023-07-27 22:48:11 +08:00
|
|
|
|
ClassMapperLabel
|
2021-04-25 01:04:21 +08:00
|
|
|
|
.Add($"{_prefixCls}-wrapper")
|
2023-07-27 22:48:11 +08:00
|
|
|
|
.If($"{_prefixCls}-wrapper-checked", () => Checked)
|
|
|
|
|
.If($"{_prefixCls}-group-item", () => CheckboxGroup != null);
|
2020-04-23 17:13:56 +08:00
|
|
|
|
|
2023-07-27 22:48:11 +08:00
|
|
|
|
ClassMapper
|
2021-04-25 01:04:21 +08:00
|
|
|
|
.Add(_prefixCls)
|
|
|
|
|
.If($"{_prefixCls}-checked", () => Checked && !Indeterminate)
|
2023-07-27 22:48:11 +08:00
|
|
|
|
.If($"{_prefixCls}-disabled", () => IsDisabled)
|
2021-04-25 01:04:21 +08:00
|
|
|
|
.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
|
|
|
|
}
|
|
|
|
|
}
|