Skip to content
Merged
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
16 changes: 14 additions & 2 deletions maui/src/Accordion/AccordionItemView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,21 @@ internal bool CanCollapseItemOnSingleAndMultipleExpandMode()
return false;
}

var expandedItems = Accordion.Items.Where(x => x._accordionItemView != null && x._accordionItemView.IsExpanded).ToList();
var expandedCount = 0;
foreach (var item in Accordion.Items)
{
if (item._accordionItemView != null && item._accordionItemView.IsExpanded)
{
expandedCount++;
if (expandedCount > 1)
{
break;
}
}
}

return (Accordion.ExpandMode == AccordionExpandMode.Single ||
Accordion.ExpandMode == AccordionExpandMode.Multiple) && expandedItems.Count == 1;
Accordion.ExpandMode == AccordionExpandMode.Multiple) && expandedCount == 1;
}

#endregion
Expand Down
38 changes: 34 additions & 4 deletions maui/src/Accordion/SfAccordion.cs
Original file line number Diff line number Diff line change
Expand Up @@ -540,8 +540,22 @@ internal void UpdateIsExpandValueBasedOnIndex(int index, bool isExpanded)
/// </summary>
internal void UpdateSelection()
{
var selectedItems = Items.Where(x => x._accordionItemView != null && x._accordionItemView.IsSelected).ToList();
if (selectedItems.Count == 1 && Items[selectedItems[0]._itemIndex] is AccordionItem items && items._accordionItemView is AccordionItemView accordionItem)
AccordionItem? selectedItem = null;
int selectedCount = 0;
foreach (var item in Items)
{
if (item._accordionItemView != null && item._accordionItemView.IsSelected)
{
selectedItem ??= item;
selectedCount++;
if (selectedCount > 1)
{
break;
}
}
}

if (selectedCount == 1 && selectedItem != null && Items[selectedItem._itemIndex] is AccordionItem accordionItemEntry && accordionItemEntry._accordionItemView is AccordionItemView accordionItem)
{
accordionItem.IsSelected = false;
}
Expand All @@ -560,7 +574,15 @@ internal void UpdateAccordionItemsBasedOnExpandModes(bool isExpandModeChanged)
return;
}

var expandedItems = Items.Where(x => x._accordionItemView != null && x._accordionItemView.IsExpanded).ToList();
var expandedItems = new List<AccordionItem>();
foreach (var item in Items)
{
if (item._accordionItemView != null && item._accordionItemView.IsExpanded)
{
expandedItems.Add(item);
}
}

switch (ExpandMode)
{
case AccordionExpandMode.Single:
Expand Down Expand Up @@ -1575,7 +1597,15 @@ void IKeyboardListener.OnPreviewKeyDown(Syncfusion.Maui.Toolkit.Internals.KeyEve
/// <param name="e">The <see cref="KeyEventArgs"/> containing the event data for the key that was pressed.</param>
void IKeyboardListener.OnKeyDown(KeyEventArgs e)
{
var selectedItem = Items.FirstOrDefault(x => x._accordionItemView != null && x._accordionItemView.IsSelected);
AccordionItem? selectedItem = null;
foreach (var item in Items)
{
if (item._accordionItemView != null && item._accordionItemView.IsSelected)
{
selectedItem = item;
break;
}
}
if (e.Key == KeyboardKey.Down || (e.Key == KeyboardKey.Tab && !e.IsShiftKeyPressed))
{
OnDownKeyPressed(selectedItem);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1605,7 +1605,61 @@ public void UpdateSelection_ShouldDeselectItem_WhenSingleItemIsSelected()
InvokePrivateMethod(accordion, "UpdateSelection");
Assert.False(accordion.Items[0]._accordionItemView?.IsSelected);
}


[Fact]
public void UpdateSelection_ShouldNotDeselect_WhenNoItemsAreSelected()
{
var accordion = new SfAccordion();
accordion.Items.Add(new AccordionItem { _itemIndex = 0, _accordionItemView = new AccordionItemView() });
accordion.Items.Add(new AccordionItem { _itemIndex = 1, _accordionItemView = new AccordionItemView() });
accordion.UpdateSelection();
// No exception thrown, items remain unselected
Assert.False(accordion.Items[0]._accordionItemView!.IsSelected);
Assert.False(accordion.Items[1]._accordionItemView!.IsSelected);
}

[Fact]
public void UpdateSelection_ShouldNotThrow_WhenItemsHaveNullAccordionItemView()
{
var accordion = new SfAccordion();
accordion.Items.Add(new AccordionItem { _itemIndex = 0 });
accordion.Items.Add(new AccordionItem { _itemIndex = 1 });
var exception = Record.Exception(() => accordion.UpdateSelection());
Assert.Null(exception);
}

[Fact]
public void UpdateAccordionItemsBasedOnExpandModes_SingleMode_CollapsesExtraItems_WhenMultipleExpanded()
{
var accordion = new SfAccordion
{
ExpandMode = AccordionExpandMode.Single
};
var item1 = new AccordionItem { _itemIndex = 0, _accordionItemView = new AccordionItemView { IsExpanded = true } };
var item2 = new AccordionItem { _itemIndex = 1, _accordionItemView = new AccordionItemView { IsExpanded = true } };
var item3 = new AccordionItem { _itemIndex = 2, _accordionItemView = new AccordionItemView { IsExpanded = true } };
accordion.Items.Add(item1);
accordion.Items.Add(item2);
accordion.Items.Add(item3);
accordion.IsViewLoaded = true;
accordion.UpdateAccordionItemsBasedOnExpandModes(false);
// After collapsing extras, items at indices 0 and 1 should be collapsed (IsExpanded set on AccordionItem)
Assert.False(item1.IsExpanded);
Assert.False(item2.IsExpanded);
}

[Fact]
public void UpdateAccordionItemsBasedOnExpandModes_EmptyItems_ShouldNotThrow()
{
var accordion = new SfAccordion
{
ExpandMode = AccordionExpandMode.Single
};
accordion.IsViewLoaded = true;
var exception = Record.Exception(() => accordion.UpdateAccordionItemsBasedOnExpandModes(false));
Assert.Null(exception);
}

#endregion

}
Expand Down