ant-design-blazor/components/checkbox/Checkbox.razor.cs
Andrzej Bakun 555fdb9a43 fix(module: checkbox & switch): Checked parameter binding (#1394)
* fix(module:checkbox): allow binding to Checked parameter

* fix(module:switch): allow binding to Checked parameter

* fix(modules): checkbox & switch get AntInputBoolComponentBase class

* test(module:switch): add tests

* test(module:checkbox): add tests

* test(module:divider): simplify by using id:ignore

* docs(module:checkbox): bind example

* docs(module:switch): bind example

* fix(module:switch): add Control + docs + tests

* feat(module:checkboxgroup): add layout and mixed mode

* fix: review comments + tests

* fix(module:checkboxgroup): parameter name should be MixedMode

added more tests

* fix demo

* fix(module:checkboxgroup): allow toggling between modes

Co-authored-by: James Yeung <shunjiey@hotmail.com>
2021-04-25 01:04:21 +08:00

80 lines
2.6 KiB
C#

using System;
using System.Linq.Expressions;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Components;
namespace AntDesign
{
public partial class Checkbox : AntInputBoolComponentBase
{
[Parameter] public RenderFragment ChildContent { get; set; }
//[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; }
[Parameter] public Expression<Func<bool>> CheckedExpression { get; set; }
[Parameter] public bool Indeterminate { get; set; }
[Parameter] public string Label { get; set; }
[CascadingParameter] public CheckboxGroup CheckboxGroup { get; set; }
internal bool IsFromOptions { get; set; }
private bool _isInitalized;
protected override void OnInitialized()
{
base.OnInitialized();
SetClass();
CheckboxGroup?.AddItem(this);
_isInitalized = true;
}
protected override void Dispose(bool disposing)
{
CheckboxGroup?.RemoveItem(this);
base.Dispose(disposing);
}
protected ClassMapper ClassMapperLabel { get; } = new ClassMapper();
private string _prefixCls = "ant-checkbox";
protected void SetClass()
{
ClassMapperLabel.Clear()
.Add(_prefixCls)
.Add($"{_prefixCls}-wrapper")
.If($"{_prefixCls}-wrapper-checked", () => Checked);
ClassMapper.Clear()
.Add(_prefixCls)
.If($"{_prefixCls}-checked", () => Checked && !Indeterminate)
.If($"{_prefixCls}-disabled", () => Disabled)
.If($"{_prefixCls}-indeterminate", () => Indeterminate)
.If($"{_prefixCls}-rtl", () => RTL);
}
protected async Task InputCheckedChange(ChangeEventArgs args)
{
if (args != null && args.Value is bool value)
{
await base.ChangeValue(value);
if (CheckedChange.HasDelegate) //kept for compatibility reasons with previous versions
await CheckedChange.InvokeAsync(value);
CheckboxGroup?.OnCheckboxChange(this);
}
}
internal void SetValue(bool value) => Checked = value;
protected override void OnValueChange(bool value)
{
if (_isInitalized)
base.OnValueChange(value);
}
}
}