mirror of
https://gitee.com/ant-design-blazor/ant-design-blazor.git
synced 2024-12-15 17:31:42 +08:00
71bac2e85d
* Children components using CascadingParameter add itself to parent component.However, when children components disposed, they are not remove from parent component. For example, when items in checkgroup have changed , it may causes Index was outside the bounds of the array at AntDesign.CheckboxGroup.OnCheckboxChange.To fix these bug, Adding remove function in dispose process is the solution of this commit. * fix: some exception Co-authored-by: ElderJames <shunjiey@hotmail.com>
118 lines
3.0 KiB
C#
118 lines
3.0 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Threading.Tasks;
|
|
using Microsoft.AspNetCore.Components;
|
|
using System.Linq.Expressions;
|
|
|
|
namespace AntDesign
|
|
{
|
|
public partial class Checkbox : AntInputComponentBase<bool>
|
|
{
|
|
[Parameter]
|
|
public RenderFragment ChildContent { get; set; }
|
|
|
|
private ElementReference _inputElement;
|
|
private ElementReference _contentElement;
|
|
|
|
[Parameter]
|
|
public EventCallback<bool> CheckedChange { get; set; }
|
|
|
|
[Parameter]
|
|
public Expression<Func<bool>> CheckedExpression { get; set; }
|
|
|
|
[Parameter]
|
|
public bool AutoFocus { get; set; }
|
|
|
|
[Parameter]
|
|
public bool Disabled { get; set; }
|
|
|
|
[Parameter]
|
|
public bool Indeterminate { get; set; }
|
|
|
|
[Parameter]
|
|
public bool Checked { get; set; }
|
|
|
|
[Parameter]
|
|
public string Label { get; set; }
|
|
|
|
[CascadingParameter]
|
|
public CheckboxGroup CheckboxGroup { get; set; }
|
|
|
|
protected Dictionary<string, object> InputAttributes { get; set; }
|
|
|
|
protected override void OnParametersSet()
|
|
{
|
|
SetClass();
|
|
base.OnParametersSet();
|
|
}
|
|
|
|
protected override void OnInitialized()
|
|
{
|
|
if (this is Checkbox checkbox)
|
|
{
|
|
CheckboxGroup?.CheckboxItems.Add(checkbox);
|
|
}
|
|
|
|
Value = Checked;
|
|
}
|
|
|
|
protected override void Dispose(bool disposing)
|
|
{
|
|
if (this is Checkbox checkbox)
|
|
{
|
|
CheckboxGroup?.CheckboxItems.Remove(checkbox);
|
|
}
|
|
|
|
base.Dispose(disposing);
|
|
}
|
|
|
|
protected void SetClass()
|
|
{
|
|
string prefixName = "ant-checkbox";
|
|
ClassMapper.Clear()
|
|
.Add(prefixName)
|
|
.If($"{prefixName}-checked", () => Checked && !Indeterminate)
|
|
.If($"{prefixName}-disabled", () => Disabled)
|
|
.If($"{prefixName}-indeterminate", () => Indeterminate);
|
|
}
|
|
|
|
protected async Task InputCheckedChange(ChangeEventArgs args)
|
|
{
|
|
if (args != null && args.Value is bool value)
|
|
{
|
|
await InnerCheckedChange(value);
|
|
|
|
CurrentValue = Checked;
|
|
}
|
|
}
|
|
|
|
protected async Task InnerCheckedChange(bool @checked)
|
|
{
|
|
if (!this.Disabled)
|
|
{
|
|
this.Checked = @checked;
|
|
|
|
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;
|
|
}
|
|
}
|
|
}
|