generated from unoplatform/template
-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add chip selection support for ItemsRepeater
- Loading branch information
Showing
6 changed files
with
114 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using Microsoft.UI.Xaml.Controls; | ||
|
||
#if IS_WINUI | ||
using Microsoft.UI.Xaml; | ||
#else | ||
using Windows.UI.Xaml; | ||
#endif | ||
|
||
namespace Uno.Toolkit.UI | ||
{ | ||
public static class ChipExtensions | ||
{ | ||
#region DependencyProperty: ChipSelectionMode = ChipSelectionMode.Single | ||
|
||
public static DependencyProperty ChipSelectionModeProperty { get; } = DependencyProperty.RegisterAttached( | ||
"ChipSelectionMode", | ||
typeof(ChipSelectionMode), | ||
typeof(ChipExtensions), | ||
new PropertyMetadata(ChipSelectionMode.Single, OnChipSelectionModeChanged)); | ||
|
||
public static ChipSelectionMode GetChipSelectionMode(DependencyObject obj) => (ChipSelectionMode)obj.GetValue(ChipSelectionModeProperty); | ||
public static void SetChipSelectionMode(DependencyObject obj, ChipSelectionMode value) => obj.SetValue(ChipSelectionModeProperty, value); | ||
|
||
#endregion | ||
|
||
private static void OnChipSelectionModeChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e) | ||
{ | ||
if (sender is ItemsRepeater ir) | ||
{ | ||
ir.ElementPrepared -= OnItemsRepeaterElementPrepared; | ||
ir.ElementPrepared += OnItemsRepeaterElementPrepared; | ||
|
||
foreach (var chip in ir.GetChildren().OfType<Chip>()) | ||
{ | ||
chip | ||
} | ||
} | ||
else if (sender is Chip chip) | ||
{ | ||
chip.OnChipSelectionModeChanged(e); | ||
} | ||
} | ||
|
||
private static void EnforceItemsRepeaterChipSelection(ItemsRepeater ir) | ||
{ | ||
var mode = GetChipSelectionMode(ir); | ||
var singleSelection = default(Chip); | ||
|
||
foreach (var chip in ir.GetChildren().OfType<Chip>()) | ||
{ | ||
if (mode is ChipSelectionMode.None) | ||
{ | ||
chip.SetIsCheckedSilently(false); | ||
} | ||
else if (mode is ChipSelectionMode.Single or ChipSelectionMode.SingleOrNone && chip.IsChecked == true) | ||
{ | ||
if (singleSelection is { }) | ||
{ | ||
chip.SetIsCheckedSilently(false); | ||
} | ||
else | ||
{ | ||
singleSelection = chip; | ||
} | ||
} | ||
} | ||
if (mode is ChipSelectionMode.Single && singleSelection is null | ||
&& ir.GetChildren().OfType<Chip>().FirstOrDefault() is { } first) | ||
{ | ||
first.SetIsCheckedSilently(true); | ||
} | ||
} | ||
|
||
private static void OnItemsRepeaterElementPrepared(ItemsRepeater sender, ItemsRepeaterElementPreparedEventArgs args) | ||
{ | ||
if (args.Element is Chip chip) | ||
{ | ||
SetChipSelectionMode(chip, GetChipSelectionMode(sender)); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters