mirror of
https://gitee.com/ant-design-blazor/ant-design-blazor.git
synced 2024-12-04 21:17:36 +08:00
7ed9c6550f
* When Panel is set as active, call parent Collapse in case it is in a accordion mode, Allow shortcut Toggle method and always null-check referenced Collapse. * Alllow de/activating child Panels by their keys. * docs: update the demo and api doc Co-authored-by: James Yeung <shunjiey@hotmail.com>
133 lines
3.6 KiB
C#
133 lines
3.6 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Threading.Tasks;
|
|
using Microsoft.AspNetCore.Components;
|
|
using OneOf;
|
|
|
|
namespace AntDesign
|
|
{
|
|
public partial class Collapse : AntDomComponentBase
|
|
{
|
|
#region Parameter
|
|
|
|
[Parameter]
|
|
public bool Accordion { get; set; }
|
|
|
|
[Parameter]
|
|
public bool Bordered { get; set; } = true;
|
|
|
|
[Parameter]
|
|
public string ExpandIconPosition { get; set; } = CollapseExpandIconPosition.Left;
|
|
|
|
[Parameter]
|
|
public string[] DefaultActiveKey { get; set; } = Array.Empty<string>();
|
|
|
|
[Parameter]
|
|
public EventCallback<string[]> OnChange { get; set; }
|
|
|
|
[Parameter]
|
|
public string ExpandIcon { get; set; } = "right";
|
|
|
|
[Parameter]
|
|
public RenderFragment<bool> ExpandIconTemplate { get; set; }
|
|
|
|
#endregion Parameter
|
|
|
|
[Parameter]
|
|
public RenderFragment ChildContent { get; set; }
|
|
|
|
private IList<Panel> Items { get; } = new List<Panel>();
|
|
|
|
private void SetClassMap()
|
|
{
|
|
ClassMapper.Clear().Add("ant-collapse")
|
|
.If("ant-collapse-icon-position-left", () => ExpandIconPosition == CollapseExpandIconPosition.Left)
|
|
.If("ant-collapse-icon-position-right", () => ExpandIconPosition == CollapseExpandIconPosition.Right)
|
|
.If("ant-collapse-borderless", () => !this.Bordered);
|
|
}
|
|
|
|
protected override async Task OnInitializedAsync()
|
|
{
|
|
SetClassMap();
|
|
await base.OnInitializedAsync();
|
|
}
|
|
|
|
internal void AddPanel(Panel panel)
|
|
{
|
|
this.Items.Add(panel);
|
|
if (panel.Key.IsIn(DefaultActiveKey))
|
|
{
|
|
panel.SetActiveInt(true);
|
|
}
|
|
|
|
StateHasChanged();
|
|
}
|
|
|
|
internal void RemovePanel(Panel panel)
|
|
{
|
|
this.Items.Remove(panel);
|
|
}
|
|
|
|
internal void Click(Panel panel)
|
|
{
|
|
if (panel == null)
|
|
{
|
|
return;
|
|
}
|
|
|
|
if (this.Accordion && !panel.Active)
|
|
{
|
|
this.Items.Where(item => item != panel && item.Active)
|
|
.ForEach(item => item.SetActiveInt(false));
|
|
}
|
|
|
|
panel.SetActiveInt(!panel.Active);
|
|
|
|
var selectedKeys = this.Items.Where(x => x.Active).Select(x => x.Key).ToArray();
|
|
OnChange.InvokeAsync(selectedKeys);
|
|
|
|
panel.OnActiveChange.InvokeAsync(panel.Active);
|
|
}
|
|
|
|
public void Activate(params string[] activeKeys)
|
|
{
|
|
var selectedKeys = new List<string>(activeKeys.Length);
|
|
|
|
foreach (var item in Items)
|
|
{
|
|
if (item.Key.IsIn(activeKeys))
|
|
{
|
|
selectedKeys.Add(item.Key);
|
|
item.SetActiveInt(true);
|
|
}
|
|
else if (this.Accordion)
|
|
{
|
|
item.SetActiveInt(false);
|
|
}
|
|
}
|
|
|
|
OnChange.InvokeAsync(selectedKeys.ToArray());
|
|
}
|
|
|
|
public void Deactivate(params string[] inactiveKeys)
|
|
{
|
|
var selectedKeys = new List<string>();
|
|
|
|
foreach (var item in Items)
|
|
{
|
|
if (item.Key.IsIn(inactiveKeys))
|
|
{
|
|
item.SetActiveInt(false);
|
|
}
|
|
else if (item.Active)
|
|
{
|
|
selectedKeys.Add(item.Key);
|
|
}
|
|
}
|
|
|
|
OnChange.InvokeAsync(selectedKeys.ToArray());
|
|
}
|
|
}
|
|
}
|