fix(module: select): replacing a datasource with some of the same items was not in the right order (#2462)

* fix(module: select): replacing a datasource with some of the same elements was not in the right order

* add tests

* fix indent
This commit is contained in:
James Yeung 2022-05-19 22:59:36 +08:00 committed by GitHub
parent 2594eefa4d
commit 36a40efef3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 87 additions and 3 deletions

View File

@ -452,6 +452,7 @@ namespace AntDesign
return;
}
// clear DataSource
if (DataSource == null && _datasource != null)
{
SelectOptionItems.Clear();
@ -467,7 +468,8 @@ namespace AntDesign
return;
}
if (DataSource != null && !DataSource.Any() && SelectOptionItems.Any())
// clear DataSource
if (DataSource?.Any() == false && SelectOptionItems.Any())
{
SelectOptionItems.Clear();
SelectedOptionItems.Clear();
@ -484,6 +486,7 @@ namespace AntDesign
return;
}
// DataSource maybe changed
if (DataSource != null)
{
if (_datasource == null)
@ -506,10 +509,13 @@ namespace AntDesign
if (_dataSourceHasChanged)
{
OnDataSourceChanged?.Invoke();
_datasource = DataSource;
if (_isPrimitive)
{
// Maybe a workaound for issues 2439
SelectOptionItems.Clear();
SelectedOptionItems.Clear();
_dataSourceCopy = _datasource.ToList();
}
else
@ -521,6 +527,8 @@ namespace AntDesign
var cloneMethod = GetDataSourceItemCloneMethod();
_dataSourceShallowCopy = _datasource.Select(x => (TItem)cloneMethod.Invoke(x, null)).ToList();
}
OnDataSourceChanged?.Invoke();
}
}
}

View File

@ -8,7 +8,7 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="bunit.web" Version="1.3.35-preview" />
<PackageReference Include="bunit.web" Version="1.7.7" />
<PackageReference Include="FluentAssertions" Version="6.1.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.11.0" />
<PackageReference Include="Moq" Version="4.16.1" />

View File

@ -28,6 +28,11 @@
new PersonClass(4, "Emily"),
};
List<string> _personsString = new List<string>
{
"John","Lucy","Jack","Emily"
};
[Fact]
public void Action_fired_when_datasource_changed()
{
@ -276,4 +281,75 @@
handlerCalled.Should().BeTrue();
}
[Fact]
public void Object_DataSource_change_with_right_order()
{
//Arrange
JSInterop.Setup<AntDesign.JsInterop.DomRect>(JSInteropConstants.GetBoundingClientRect, _ => true)
.SetResult(new AntDesign.JsInterop.DomRect());
int value = 2;
Action<int> ValueChanged = (v) => value = v;
var cut = Render<AntDesign.Select<int, Person>>(
@<AntDesign.Select DataSource="@_persons"
LabelName="@nameof(Person.Name)"
ValueName="@nameof(Person.Id)"
Value="@value"
ValueChanged="@ValueChanged"
AllowClear
>
</AntDesign.Select>
);
var newDataSource = new List<Person>(_persons) { new Person(5, "NewPerson") };
//Act
cut.SetParametersAndRender(parameters =>
parameters
.Add(p => p.DataSource, newDataSource)
);
//Assert
var options = cut.FindAll(".ant-select-item-option-content");
var selectedItem = cut.Find(".ant-select-selection-item");
options.Should().HaveCount(5);
for (int i = 0; i < options.Count; i++)
{
options[i].TextContent.Trim().Should().Be(newDataSource[i].Name);
}
selectedItem.TextContent.Trim().Should().Be("Lucy");
value.Should().Be(2);
}
[Fact] // Issues: #2439
public void Primitive_DataSource_change_with_right_order()
{
//Arrange
JSInterop.Setup<AntDesign.JsInterop.DomRect>(JSInteropConstants.GetBoundingClientRect, _ => true)
.SetResult(new AntDesign.JsInterop.DomRect());
string value = "Lucy";
Action<string> ValueChanged = (v) => value = v;
var cut = Render<AntDesign.Select<string, string>>(
@<AntDesign.Select DataSource="@_personsString"
Value="@value"
ValueChanged="@ValueChanged"
AllowClear
>
</AntDesign.Select>
);
var newDataSource = new List<string>() { "NewPerson" }.Union(_personsString).ToList();
//Act
cut.SetParametersAndRender(parameters =>
parameters
.Add(p => p.DataSource, newDataSource)
);
//Assert
var options = cut.FindAll(".ant-select-item-option-content");
var selectedItem = cut.Find(".ant-select-selection-item");
options.Should().HaveCount(5);
for (int i = 0; i < options.Count; i++)
{
options[i].TextContent.Trim().Should().Be(newDataSource[i]);
}
selectedItem.TextContent.Trim().Should().Be("Lucy");
value.Should().Be("Lucy");
}
}