Skip to content

Commit

Permalink
Standardise the use of Selected and SelectedChanged (MudBlazor#8886)
Browse files Browse the repository at this point in the history
  • Loading branch information
BieleckiLtd authored May 7, 2024
1 parent f47a442 commit 5e66ce7
Show file tree
Hide file tree
Showing 18 changed files with 71 additions and 69 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@
<DocsPageSection>
<SectionHeader Title="Binding chips in a selection">
<Description>
You can bind a Chip's <CodeInline>IsSelected</CodeInline> parameter to manipulate the selection.
You can bind a Chip's <CodeInline>@nameof(MudChip<T>.Selected)</CodeInline> parameter to manipulate the selection.
</Description>
</SectionHeader>
<SectionContent ShowCode="false" Code="@nameof(ChipSetChipBindingExample)" Block="true">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
@for (int i = 0; i < _included.Length; i++)
{
var index = i;
<MudChip Value="@_ingredients[index]" @bind-IsSelected="_included[index]" Color="Color.Primary" Variant="@Variant.Text" />
<MudChip Value="@_ingredients[index]" @bind-Selected="_included[index]" Color="Color.Primary" Variant="@Variant.Text" />
}
</MudChipSet>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
@for (int i = 0; i < _included.Length; i++)
{
var index = i;
<MudChip Value="@_ingredients[index]" @bind-IsSelected="_included[index]" Color="Color.Primary" Variant="@Variant.Text" />
<MudChip Value="@_ingredients[index]" @bind-Selected="_included[index]" Color="Color.Primary" Variant="@Variant.Text" />
}
</MudChipSet>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<MudTreeView SelectionMode="SelectionMode.MultiSelection" Items="TreeItems" Style="width: 500px;">
<ItemTemplate>
<MudTreeViewItem @bind-Selected="@context.IsSelected" @bind-Expanded="@context.Expanded" Icon="@context.Icon"
<MudTreeViewItem @bind-Selected="@context.Selected" @bind-Expanded="@context.Expanded" Icon="@context.Icon"
Text="@context.Title" EndText="@context.Number?.ToString()" EndTextTypo="@Typo.caption" Items="@context.TreeItems" />
</ItemTemplate>
</MudTreeView>
Expand All @@ -17,7 +17,7 @@

public int? Number { get; set; } = null;

public bool IsSelected { get; set; }
public bool Selected { get; set; }

public bool Expanded { get; set; }

Expand Down
2 changes: 1 addition & 1 deletion src/MudBlazor.UnitTests/Components/ChipSetTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -373,7 +373,7 @@ public async Task ChipSet_CheckMark_Parameter()
.Add(x => x.CloseIcon, Icons.Material.Filled.Plagiarism)
.Add(x => x.Ripple, false)
.Add(x => x.IconColor, Color.Error)
.Add(x => x.IsSelected, true)
.Add(x => x.Selected, true)
).Instance;
await comp.InvokeAsync(() => chip.UpdateSelectionStateAsync(true));
chip.ShowCheckMark.Should().Be(false); // because not in a chipset
Expand Down
4 changes: 2 additions & 2 deletions src/MudBlazor.UnitTests/Components/DataGridTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2929,9 +2929,9 @@ public async Task DataGridCellContextTest()
var column = dataGrid.Instance.RenderedColumns.First();
var cell = new Cell<DataGridCellContextTest.Model>(dataGrid.Instance, column, item);

cell._cellContext.IsSelected.Should().Be(false);
cell._cellContext.Selected.Should().Be(false);
await cell._cellContext.Actions.SetSelectedItemAsync(true);
cell._cellContext.IsSelected.Should().Be(true);
cell._cellContext.Selected.Should().Be(true);

await cell._cellContext.Actions.ToggleHierarchyVisibilityForItemAsync();
cell._cellContext.OpenHierarchies.Should().Contain(item);
Expand Down
2 changes: 1 addition & 1 deletion src/MudBlazor.UnitTests/Components/SelectTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -518,7 +518,7 @@ public void MultiSelect_SelectAll2()
items.Should().HaveCount(7);
foreach (var item in items)
{
item.Instance.IsSelected.Should().BeTrue();
item.Instance.Selected.Should().BeTrue();
item.FindComponent<MudListItem<string>>().Instance.Icon.Should().Be("<path d=\"M0 0h24v24H0z\" fill=\"none\"/><path d=\"M19 3H5c-1.11 0-2 .9-2 2v14c0 1.1.89 2 2 2h14c1.11 0 2-.9 2-2V5c0-1.1-.89-2-2-2zm-9 14l-5-5 1.41-1.41L10 14.17l7.59-7.59L19 8l-9 9z\"/>");
}

Expand Down
2 changes: 2 additions & 0 deletions src/MudBlazor/Base/MudComponentBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,8 @@ protected void DetectIllegalRazorParametersV7()
case "IsCheckedChanged":
case "IsVisible":
case "IsVisibleChanged":
case "IsSelected":
case "IsSelectedChanged":
NotifyIllegalParameter(parameter);
break;
}
Expand Down
40 changes: 20 additions & 20 deletions src/MudBlazor/Components/Chip/MudChip.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,26 +18,26 @@ public partial class MudChip<T> : MudComponentBase, IAsyncDisposable
public MudChip()
{
using var registerScope = CreateRegisterScope();
IsSelectedState = registerScope.RegisterParameter<bool>(nameof(IsSelected))
.WithParameter(() => IsSelected)
.WithEventCallback(() => IsSelectedChanged)
.WithChangeHandler(OnIsSelectedChangedAsync);
SelectedState = registerScope.RegisterParameter<bool>(nameof(Selected))
.WithParameter(() => Selected)
.WithEventCallback(() => SelectedChanged)
.WithChangeHandler(OnSelectedChangedAsync);
}

private Task OnIsSelectedChangedAsync(ParameterChangedEventArgs<bool> args)
private Task OnSelectedChangedAsync(ParameterChangedEventArgs<bool> args)
{
if (ChipSet == null)
return Task.CompletedTask;
return ChipSet.OnChipIsSelectedChangedAsync(this, args.Value);
return ChipSet.OnChipSelectedChangedAsync(this, args.Value);
}

internal async Task UpdateSelectionStateAsync(bool isSelected)
internal async Task UpdateSelectionStateAsync(bool selected)
{
await IsSelectedState.SetValueAsync(isSelected);
await SelectedState.SetValueAsync(selected);
StateHasChanged();
}

internal readonly ParameterState<bool> IsSelectedState;
internal readonly ParameterState<bool> SelectedState;

/// <summary>
/// The service used to navigate the browser to another URL.
Expand All @@ -59,7 +59,7 @@ internal async Task UpdateSelectionStateAsync(bool isSelected)
.AddClass("mud-ripple", IsClickable && GetRipple())
.AddClass("mud-chip-label", GetLabel())
.AddClass("mud-disabled", GetDisabled())
.AddClass("mud-chip-selected", IsSelectedState.Value)
.AddClass("mud-chip-selected", SelectedState.Value)
.AddClass(Class)
.Build();

Expand All @@ -71,8 +71,8 @@ internal Variant GetVariant()
var variant = Variant ?? chipSetVariant;
return variant switch
{
MudBlazor.Variant.Text => IsSelectedState.Value ? MudBlazor.Variant.Filled : MudBlazor.Variant.Text,
MudBlazor.Variant.Filled => IsSelectedState.Value ? MudBlazor.Variant.Text : MudBlazor.Variant.Filled,
MudBlazor.Variant.Text => SelectedState.Value ? MudBlazor.Variant.Filled : MudBlazor.Variant.Text,
MudBlazor.Variant.Filled => SelectedState.Value ? MudBlazor.Variant.Text : MudBlazor.Variant.Filled,
MudBlazor.Variant.Outlined => MudBlazor.Variant.Outlined,
_ => MudBlazor.Variant.Outlined
};
Expand All @@ -81,7 +81,7 @@ internal Variant GetVariant()
private Color GetColor()
{
var selectedColor = GetSelectedColor();
if (IsSelectedState.Value && selectedColor != MudBlazor.Color.Inherit)
if (SelectedState.Value && selectedColor != MudBlazor.Color.Inherit)
{
return selectedColor;
}
Expand Down Expand Up @@ -185,7 +185,7 @@ private Color GetColor()
public string? Icon { get; set; }

/// <summary>
/// The icon to display when <see cref="IsSelected"/> is <c>true</c>.
/// The icon to display when <see cref="Selected"/> is <c>true</c>.
/// </summary>
/// <remarks>
/// Defaults to <c>null</c>.
Expand Down Expand Up @@ -303,7 +303,7 @@ private Color GetColor()
[Parameter]
public EventCallback<MudChip<T>> OnClose { get; set; }

internal bool ShowCheckMark => IsSelectedState.Value && ChipSet?.CheckMark == true;
internal bool ShowCheckMark => SelectedState.Value && ChipSet?.CheckMark == true;

/// <summary>
/// Whether this chip is selected.
Expand All @@ -313,13 +313,13 @@ private Color GetColor()
/// </remarks>
[Parameter]
[Category(CategoryTypes.Chip.Behavior)]
public bool IsSelected { get; set; }
public bool Selected { get; set; }

/// <summary>
/// Occurs when the <see cref="IsSelected"/> property has changed.
/// Occurs when the <see cref="Selected"/> property has changed.
/// </summary>
[Parameter]
public EventCallback<bool> IsSelectedChanged { get; set; }
public EventCallback<bool> SelectedChanged { get; set; }

internal T? GetValue()
{
Expand All @@ -345,8 +345,8 @@ protected internal async Task OnClickAsync(MouseEventArgs ev)
}
if (ChipSet != null)
{
await IsSelectedState.SetValueAsync(!IsSelectedState.Value);
await ChipSet.OnChipIsSelectedChangedAsync(this, IsSelectedState.Value);
await SelectedState.SetValueAsync(!SelectedState.Value);
await ChipSet.OnChipSelectedChangedAsync(this, SelectedState.Value);
}
if (Href != null)
{
Expand Down
18 changes: 9 additions & 9 deletions src/MudBlazor/Components/ChipSet/MudChipSet.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -278,8 +278,8 @@ private async Task UpdateSelectedValueAsync(T? newValue, bool updateChips = true
foreach (var chip in _chips.ToArray())
{
var value = chip.GetValue();
var isSelected = Comparer.Equals(value, newValue);
await chip.UpdateSelectionStateAsync(isSelected);
var selected = Comparer.Equals(value, newValue);
await chip.UpdateSelectionStateAsync(selected);
}
}
await _selectedValue.SetValueAsync(newValue);
Expand All @@ -304,12 +304,12 @@ private async Task UpdateChipsAsync()
foreach (var chip in _chips.ToArray())
{
var value = chip.GetValue();
bool isSelected;
bool selected;
if (MultiSelection)
isSelected = value is not null && _selection.Contains(value);
selected = value is not null && _selection.Contains(value);
else
isSelected = Comparer.Equals(_selectedValue, value);
await chip.UpdateSelectionStateAsync(isSelected);
selected = Comparer.Equals(_selectedValue, value);
await chip.UpdateSelectionStateAsync(selected);
}
}

Expand Down Expand Up @@ -359,7 +359,7 @@ internal async Task RemoveAsync(MudChip<T> chip)
StateHasChanged();
}

internal async Task OnChipIsSelectedChangedAsync(MudChip<T> chip, bool isSelected)
internal async Task OnChipSelectedChangedAsync(MudChip<T> chip, bool selected)
{
var value = chip.GetValue();
if (!MultiSelection)
Expand All @@ -372,15 +372,15 @@ internal async Task OnChipIsSelectedChangedAsync(MudChip<T> chip, bool isSelecte
else
{
// Toggle Selection
await UpdateSelectedValueAsync(isSelected ? value : default);
await UpdateSelectedValueAsync(selected ? value : default);
}
return;
}
// Multi Selection
if (value is null)
return;
var newSelection = new HashSet<T>(_selection, Comparer);
if (isSelected)
if (selected)
{
newSelection.Add(value);
}
Expand Down
2 changes: 1 addition & 1 deletion src/MudBlazor/Components/DataGrid/CellContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public class CellContext<T>

public CellActions Actions { get; }

public bool IsSelected
public bool Selected
{
get
{
Expand Down
6 changes: 3 additions & 3 deletions src/MudBlazor/Components/DataGrid/HeaderCell.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public partial class HeaderCell<T> : MudComponentBase, IDisposable
[Parameter] public RenderFragment ChildContent { get; set; }

private SortDirection _initialDirection;
private bool _isSelected;
private bool _selected;

[Parameter]
public SortDirection SortDirection
Expand Down Expand Up @@ -239,13 +239,13 @@ private void OnGridSortChanged(Dictionary<string, SortDefinition<T>> activeSorts

private void OnSelectedAllItemsChanged(bool value)
{
_isSelected = value;
_selected = value;
StateHasChanged();
}

private void OnSelectedItemsChanged(HashSet<T> items)
{
_isSelected = items.Count == DataGrid.GetFilteredItemsCount();
_selected = items.Count == DataGrid.GetFilteredItemsCount();
StateHasChanged();
}

Expand Down
2 changes: 1 addition & 1 deletion src/MudBlazor/Components/DataGrid/SelectColumn.razor
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
}
</HeaderTemplate>
<CellTemplate>
<MudCheckBox T="bool" Size="@Size" Value="@context.IsSelected" ValueChanged="@context.Actions.SetSelectedItemAsync" />
<MudCheckBox T="bool" Size="@Size" Value="@context.Selected" ValueChanged="@context.Actions.SetSelectedItemAsync" />
</CellTemplate>
</TemplateColumn>

Expand Down
8 changes: 4 additions & 4 deletions src/MudBlazor/Components/List/MudList.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -319,8 +319,8 @@ private void UpdateSelectedItem(T? value)
{
foreach (var item in _items.ToArray())
{
var isSelected = value is not null && Comparer.Equals(value, item.GetValue());
item.SetSelected(isSelected);
var selected = value is not null && Comparer.Equals(value, item.GetValue());
item.SetSelected(selected);
}
foreach (var childList in _childLists.ToArray())
{
Expand All @@ -336,8 +336,8 @@ internal void UpdateSelectedItems(HashSet<T> selection)
foreach (var listItem in _items.ToArray())
{
var itemValue = listItem.GetValue();
var isSelected = itemValue is not null && selection.Contains(itemValue);
listItem.SetSelected(isSelected);
var selected = itemValue is not null && selection.Contains(itemValue);
listItem.SetSelected(selected);
}
foreach (var childList in _childLists.ToArray())
{
Expand Down
22 changes: 11 additions & 11 deletions src/MudBlazor/Components/Select/MudSelectItem.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,21 +33,21 @@ internal IMudSelect IMudSelect
_parent.CheckGenericTypeMatch(this);
if (MudSelect == null)
return;
var isSelected = MudSelect.Add(this);
var selected = MudSelect.Add(this);
if (_parent.MultiSelection)
{
MudSelect.SelectionChangedFromOutside += OnUpdateSelectionStateFromOutside;
InvokeAsync(() => OnUpdateSelectionStateFromOutside(MudSelect.SelectedValues));
}
else
{
IsSelected = isSelected;
Selected = selected;
}
}
}

private IMudShadowSelect _shadowParent;
private bool _isSelected;
private bool _selected;

[CascadingParameter]
internal IMudShadowSelect IMudShadowSelect
Expand All @@ -73,9 +73,9 @@ private void OnUpdateSelectionStateFromOutside(IEnumerable<T> selection)
{
if (selection == null)
return;
var old_is_selected = IsSelected;
IsSelected = selection.Contains(Value);
if (old_is_selected != IsSelected)
var old_selected = Selected;
Selected = selection.Contains(Value);
if (old_selected != Selected)
InvokeAsync(StateHasChanged);
}

Expand All @@ -102,12 +102,12 @@ protected bool MultiSelection
/// <summary>
/// Selected state of the option. Only works if the parent is a mulit-select
/// </summary>
internal bool IsSelected
internal bool Selected
{
get => _isSelected;
get => _selected;
set
{
_isSelected = value;
_selected = value;
}
}

Expand All @@ -120,7 +120,7 @@ protected string CheckBoxIcon
{
if (!MultiSelection)
return null;
return IsSelected ? Icons.Material.Filled.CheckBox : Icons.Material.Filled.CheckBoxOutlineBlank;
return Selected ? Icons.Material.Filled.CheckBox : Icons.Material.Filled.CheckBoxOutlineBlank;
}
}

Expand All @@ -138,7 +138,7 @@ protected string DisplayString
private void OnClicked()
{
if (MultiSelection)
IsSelected = !IsSelected;
Selected = !Selected;

MudSelect?.SelectOption(Value);
InvokeAsync(StateHasChanged);
Expand Down
Loading

0 comments on commit 5e66ce7

Please sign in to comment.