ant-design-blazor/components/checkbox/CheckboxGroup.razor.cs

67 lines
1.8 KiB
C#
Raw Normal View History

2020-01-11 01:10:05 +08:00
using System;
using System.Collections.Generic;
2020-04-24 18:32:50 +08:00
using System.Collections.ObjectModel;
using System.Linq;
using System.Threading.Tasks;
2020-01-11 01:10:05 +08:00
using Microsoft.AspNetCore.Components;
namespace AntDesign
2020-01-11 01:10:05 +08:00
{
public partial class CheckboxGroup : AntDomComponentBase
2020-01-11 01:10:05 +08:00
{
[Parameter]
public RenderFragment ChildContent { get; set; }
2020-04-24 18:32:50 +08:00
[Parameter]
public IList<Checkbox> CheckboxItems { get; set; } = new List<Checkbox>();
2020-01-11 01:10:05 +08:00
2020-04-24 18:32:50 +08:00
[Parameter]
public EventCallback<string[]> ValueChanged { get; set; }
2020-01-11 01:10:05 +08:00
[Parameter]
2020-04-24 18:32:50 +08:00
#pragma warning disable CA1819 // 属性不应返回数组
public CheckboxOption[] Options { get; set; }
2020-04-24 18:32:50 +08:00
#pragma warning restore CA1819 // 属性不应返回数组
2020-01-11 01:10:05 +08:00
[Parameter]
public IList<string> Value { get; set; } = Array.Empty<string>();
2020-04-24 18:32:50 +08:00
protected override void OnParametersSet()
{
2020-04-24 18:32:50 +08:00
foreach (string item in Value)
{
Options.Where(o => o.Value == item).ForEach(o => o.Checked = true);
}
}
2020-01-11 01:10:05 +08:00
[Parameter]
public bool Disabled { get; set; }
2020-01-11 01:10:05 +08:00
public CheckboxGroup()
2020-01-11 01:10:05 +08:00
{
ClassMapper.Add("ant-checkbox-group");
}
2020-04-24 18:32:50 +08:00
public async void OnOptionChange()
{
await this.ValueChanged.InvokeAsync(this.Options.Where(x => x.Checked).Select(x => x.Value).ToArray());
StateHasChanged();
}
internal void OnCheckboxChange(Checkbox checkboxBase)
2020-01-11 01:10:05 +08:00
{
if (checkboxBase is Checkbox checkbox)
{
int index = CheckboxItems.IndexOf(checkbox);
if (Options[index] != null)
{
Options[index].Checked = checkbox.Checked;
}
}
StateHasChanged();
2020-01-11 01:10:05 +08:00
}
}
}