Skip to content

Commit 3ce3848

Browse files
authored
TransferList: Fix TransferAll to Affect Only Selected Items (#519)
* TransferList TransferAll Affects Only Searched Item * Fix Main Behavior and Add Test
1 parent 4e98ad5 commit 3ce3848

File tree

4 files changed

+119
-9
lines changed

4 files changed

+119
-9
lines changed
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
@namespace MudExtensions.UnitTests.TestComponents
2+
@using MudBlazor.Extensions
3+
4+
<MudGrid>
5+
<MudItem xs="12" sm="8" Class="d-flex flex-column align-center justify-center">
6+
<MudTransferList @ref="_transferList" T="string" @bind-StartCollection="_startCollection" @bind-EndCollection="_endCollection" Vertical="_vertical" Color="_color"
7+
StyleListCommon="background-color: var(--mud-palette-background-gray); width: 200px" MultiSelection="_multiSelection" MaxItems="_maxItems" SelectAllType="_selectAllType"
8+
PreventTransfer="@(new Func<bool, bool>(CheckTransfer))" OrderFunc="@(_orderOnTransfer == false ? null : new Func<ICollection<string>, ICollection<string>>(OrderMethod))" ButtonVariant="_buttonVariant"
9+
AllowDoubleClick="_allowDoubleClick" SearchBoxStart="_searchboxStart" SearchBoxEnd="_searchboxEnd"
10+
StartTitle="@_startTitle" EndTitle="@_endTitle" />
11+
</MudItem>
12+
13+
<MudItem xs="12" sm="4">
14+
<MudStack Spacing="4">
15+
<MudText><b>Start Collection:</b> @string.Join(", ", _startCollection ?? new List<string>())</MudText>
16+
<MudText><b>End Collection:</b> @string.Join(", ", _endCollection ?? new List<string>())</MudText>
17+
<MudSwitchM3 @bind-Value="_vertical" Label="Vertical" Color="Color.Secondary" />
18+
<MudSwitchM3 @bind-Value="_multiSelection" Label="MultiSelection" Color="Color.Secondary" />
19+
<MudSwitchM3 @bind-Value="_preventTurkeyTransfer" Label="Prevent Transfer If Turkey Selected" Color="Color.Secondary" />
20+
<MudSwitchM3 @bind-Value="_orderOnTransfer" Label="Order on Transfer" Color="Color.Secondary" />
21+
<MudSwitchM3 @bind-Value="_allowDoubleClick" Label="Allow Double Click" Color="Color.Secondary" />
22+
<MudSwitchM3 @bind-Value="_searchboxStart" Label="SearchBox Start" Color="Color.Secondary" />
23+
<MudSwitchM3 @bind-Value="_searchboxEnd" Label="SearchBox End" Color="Color.Secondary" />
24+
<MudNumericField @bind-Value="_maxItems" Clearable="true" Label="MaxItems" Variant="Variant.Outlined" Margin="Margin.Dense" />
25+
<MudSelectExtended @bind-Value="_selectAllType" ItemCollection="@(Enum.GetValues<SelectAllType>())" Label="SelectAll Type" Variant="Variant.Outlined" Margin="Margin.Dense" Dense="true" />
26+
<MudSelectExtended @bind-Value="_color" ItemCollection="@(Enum.GetValues<Color>())" Label="Color" Variant="Variant.Outlined" Margin="Margin.Dense" Dense="true" />
27+
<MudSelectExtended @bind-Value="_buttonVariant" ItemCollection="@(Enum.GetValues<Variant>())" Label="Button Variant" Variant="Variant.Outlined" Margin="Margin.Dense" Dense="true" />
28+
<MudTextFieldExtended @bind-Value="_startTitle" Label="Start Title" Variant="Variant.Outlined" Immediate="true" />
29+
<MudTextFieldExtended @bind-Value="_endTitle" Label="End Title" Variant="Variant.Outlined" Immediate="true" />
30+
</MudStack>
31+
</MudItem>
32+
</MudGrid>
33+
34+
@code{
35+
MudTransferList<string> _transferList = new();
36+
ICollection<string> _startCollection = new List<string>() { "Sweden", "Hungary", "Turkey", "England", "Egypt" };
37+
ICollection<string> _endCollection = new List<string>() { "Brazil", "China", "Germany", "USA", "South Africa" };
38+
39+
bool _vertical;
40+
bool _multiSelection;
41+
bool _preventTurkeyTransfer;
42+
bool _orderOnTransfer;
43+
bool _allowDoubleClick;
44+
bool _searchboxStart;
45+
bool _searchboxEnd;
46+
int? _maxItems;
47+
SelectAllType _selectAllType = SelectAllType.Buttons;
48+
Color _color = Color.Primary;
49+
Variant _buttonVariant = Variant.Text;
50+
string _startTitle = "Country Group 1";
51+
string _endTitle = "Country Group 2";
52+
53+
private bool CheckTransfer(bool startToEnd)
54+
{
55+
var valuesStart = _transferList.GetStartListSelectedValues();
56+
var valuesEnd = _transferList.GetEndListSelectedValues();
57+
if (_preventTurkeyTransfer == true && (valuesStart?.Contains("Turkey") == true || valuesEnd?.Contains("Turkey") == true))
58+
{
59+
return true;
60+
}
61+
return false;
62+
}
63+
64+
private ICollection<string> OrderMethod(ICollection<string> e)
65+
{
66+
return e.Order().ToList();
67+
}
68+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
using Bunit;
2+
using FluentAssertions;
3+
using MudExtensions.UnitTests.TestComponents;
4+
5+
namespace MudExtensions.UnitTests.Components
6+
{
7+
[TestFixture]
8+
public class TransferListTests : BunitTest
9+
{
10+
[Test]
11+
public async Task TransferListTransferAllTest()
12+
{
13+
var comp = Context.RenderComponent<TransferListTest>();
14+
var transferList = comp.FindComponent<MudTransferList<string>>();
15+
transferList.Instance.StartCollection.Should().Contain("Turkey");
16+
transferList.Instance.StartCollection.Should().NotContain("China");
17+
await comp.InvokeAsync(() => transferList.Instance.TransferAll(true));
18+
transferList.Instance.StartCollection.Should().NotContain("Turkey");
19+
transferList.Instance.EndCollection.Should().Contain("Turkey");
20+
await comp.InvokeAsync(() => transferList.Instance.TransferAll(false));
21+
transferList.Instance.StartCollection.Should().Contain("Turkey");
22+
transferList.Instance.EndCollection.Should().NotContain("Turkey");
23+
}
24+
25+
}
26+
}

CodeBeam.MudBlazor.Extensions/Components/ListExtended/MudListExtended.razor.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1657,7 +1657,7 @@ protected internal ValueTask ScrollToMiddleAsync(MudListItemExtended<T?>? item)
16571657
///
16581658
/// </summary>
16591659
/// <returns></returns>
1660-
protected ICollection<T?>? GetSearchedItems()
1660+
protected internal ICollection<T?>? GetSearchedItems()
16611661
{
16621662
if (!SearchBox || ItemCollection == null || _searchString == null)
16631663
{

CodeBeam.MudBlazor.Extensions/Components/TransferList/MudTransferList.razor.cs

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -340,24 +340,40 @@ protected internal async Task TransferAll(bool startToEnd = true)
340340
}
341341
if (startToEnd == true)
342342
{
343-
foreach (var item in StartCollection)
343+
var transferredValues = new List<T>();
344+
foreach (var item in _startList.GetSearchedItems() ?? [])
344345
{
345-
EndCollection.Add(item);
346+
if (item != null)
347+
{
348+
transferredValues.Add(item);
349+
}
350+
}
351+
352+
foreach (var item in transferredValues ?? [])
353+
{
354+
EndCollection?.Add(item);
355+
StartCollection?.Remove(item);
346356
}
347-
StartCollection.Clear();
348-
_startList.Clear();
349357
OrderItems();
350358
await EndCollectionChanged.InvokeAsync(EndCollection);
351359
await StartCollectionChanged.InvokeAsync(StartCollection);
352360
}
353361
else if (startToEnd == false)
354362
{
355-
foreach (var item in EndCollection)
363+
var transferredValues = new List<T>();
364+
foreach (var item in _endList.GetSearchedItems() ?? [])
365+
{
366+
if (item != null)
367+
{
368+
transferredValues.Add(item);
369+
}
370+
}
371+
372+
foreach (var item in transferredValues ?? [])
356373
{
357-
StartCollection.Add(item);
374+
StartCollection?.Add(item);
375+
EndCollection?.Remove(item);
358376
}
359-
EndCollection.Clear();
360-
_endList.Clear();
361377
OrderItems();
362378
await StartCollectionChanged.InvokeAsync(StartCollection);
363379
await EndCollectionChanged.InvokeAsync(EndCollection);

0 commit comments

Comments
 (0)