Skip to content

Fix show and hide behavior for nested collapses #1087

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,18 @@
<AccordionItem Title="Accordion Item #1">
<Content>
<b>This is the first item's accordion body.</b> It is shown by default, until the collapse plugin adds the appropriate classes that we use to style each element. These classes control the overall appearance, as well as the showing and hiding via CSS transitions. You can modify any of this with custom CSS or overriding our default variables. It's also worth noting that just about any HTML can go within the .accordion-body, though the transition does limit overflow.
<Accordion>
<AccordionItem Title="Accordion Item #1.1">
<Content>
<b>This is the first item in a nested accordion body.</b>.
</Content>
</AccordionItem>
<AccordionItem Title="Accordion Item #1.2">
<Content>
<b>This is the second item in a nested accordion body.</b>.
</Content>
</AccordionItem>
</Accordion>
</Content>
</AccordionItem>
<AccordionItem Title="Accordion Item #2">
Expand Down
12 changes: 8 additions & 4 deletions blazorbootstrap/Components/Accordion/AccordionItem.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,30 +27,34 @@ protected override void OnParametersSet()

internal async Task ShowAsync() => await collapse.ShowAsync();

private async Task OnCollapseHiddenAsync()
private async Task OnCollapseHiddenAsync(string collapseId)
{
if (collapseId != collapse.Id) return;
if (Parent is not null && Parent.OnHidden.HasDelegate)
await Parent.OnHidden.InvokeAsync(new AccordionEventArgs(Name, Title));
}

private async Task OnCollapseHidingAsync()
private async Task OnCollapseHidingAsync(string collapseId)
{
if (collapseId != collapse.Id) return;
isCollapsed = true;

if (Parent is not null && Parent.OnHiding.HasDelegate)
await Parent.OnHiding.InvokeAsync(new AccordionEventArgs(Name, Title));
}

private async Task OnCollapseShowingAsync()
private async Task OnCollapseShowingAsync(string collapseId)
{
if (collapseId != collapse.Id) return;
isCollapsed = false;

if (Parent is not null && Parent.OnShowing.HasDelegate)
await Parent.OnShowing.InvokeAsync(new AccordionEventArgs(Name, Title));
}

private async Task OnCollapseShownAsync()
private async Task OnCollapseShownAsync(string collapseId)
{
if (collapseId != collapse.Id) return;
if (Parent is not null && Parent.OnShown.HasDelegate)
await Parent.OnShown.InvokeAsync(new AccordionEventArgs(Name, Title));
}
Expand Down
16 changes: 8 additions & 8 deletions blazorbootstrap/Components/Collapse/Collapse.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,16 +47,16 @@ protected override async Task OnInitializedAsync()
}

[JSInvokable]
public async Task bsHiddenCollapse() => await OnHidden.InvokeAsync();
public async Task bsHiddenCollapse(string collapseId) => await OnHidden.InvokeAsync(collapseId);

[JSInvokable]
public async Task bsHideCollapse() => await OnHiding.InvokeAsync();
public async Task bsHideCollapse(string collapseId) => await OnHiding.InvokeAsync(collapseId);

[JSInvokable]
public async Task bsShowCollapse() => await OnShowing.InvokeAsync();
public async Task bsShowCollapse(string collapseId) => await OnShowing.InvokeAsync(collapseId);

[JSInvokable]
public async Task bsShownCollapse() => await OnShown.InvokeAsync();
public async Task bsShownCollapse(string collapseId) => await OnShown.InvokeAsync(collapseId);

/// <summary>
/// Hides a collapsible element.
Expand Down Expand Up @@ -105,26 +105,26 @@ protected override async Task OnInitializedAsync()
/// This event is fired when a collapse element has been hidden from the user (will wait for CSS transitions to complete).
/// </summary>
[Parameter]
public EventCallback OnHidden { get; set; }
public EventCallback<string> OnHidden { get; set; }

/// <summary>
/// This event is fired immediately when the hide method has been called.
/// </summary>
[Parameter]
public EventCallback OnHiding { get; set; }
public EventCallback<string> OnHiding { get; set; }

/// <summary>
/// This event fires immediately when the show instance method is called.
/// </summary>
[Parameter]
public EventCallback OnShowing { get; set; }
public EventCallback<string> OnShowing { get; set; }

/// <summary>
/// This event is fired when a collapse element has been made visible to the user (will wait for CSS transitions to
/// complete).
/// </summary>
[Parameter]
public EventCallback OnShown { get; set; }
public EventCallback<string> OnShown { get; set; }

/// <summary>
/// Gets or sets the parent selector, DOM element.
Expand Down
8 changes: 4 additions & 4 deletions blazorbootstrap/wwwroot/blazor.bootstrap.js
Original file line number Diff line number Diff line change
Expand Up @@ -186,16 +186,16 @@ window.blazorBootstrap = {
return;

collapseEl.addEventListener('show.bs.collapse', (event) => {
dotNetHelper.invokeMethodAsync('bsShowCollapse');
dotNetHelper.invokeMethodAsync('bsShowCollapse', event.target?.id);
});
collapseEl.addEventListener('shown.bs.collapse', (event) => {
dotNetHelper.invokeMethodAsync('bsShownCollapse');
dotNetHelper.invokeMethodAsync('bsShownCollapse', event.target?.id);
});
collapseEl.addEventListener('hide.bs.collapse', (event) => {
dotNetHelper.invokeMethodAsync('bsHideCollapse');
dotNetHelper.invokeMethodAsync('bsHideCollapse', event.target?.id);
});
collapseEl.addEventListener('hidden.bs.collapse', (event) => {
dotNetHelper.invokeMethodAsync('bsHiddenCollapse');
dotNetHelper.invokeMethodAsync('bsHiddenCollapse', event.target?.id);
});

let options = { parent: parent, toggle: toggle };
Expand Down