ant-design-blazor
ant-design-blazor copied to clipboard
Select does not recognize that a list's component was removed
Describe the bug
Hey there,
when an AntDesign.Select
is inside a component which is instantiated multiple times by a parent component (with different values) and e.g. the first of these components is removed, the remaining Select
components have the wrong selected value.
Steps to reproduce (please include code)
Minimalistic repro project: https://github.com/Velociraptor45/AntDesignSelectBug
This is the initial state (a component with a Select
+ the default guid displayed)
Clicking the '+' Button will add another component (With 'Item2' as default selection)
Now, if I were to remove the first component in that list, I'd expect to have a Select
with 'Item2' and 6e115285-6521-4212-804c-263cbcc039e9
displayed. However, after clicking the '-' Button:
Blazor updates the guid correctly, but the Select
shows 'Item1' instead of 'Item2', which is not just a visual bug, but also the selected value
However, there is a workaround by setting the Select
's Value every time OnParameterSet() is called:
Further technical details
- AntDesign Nuget Package version: 0.12.0.1
Hello,I tried your demo, and the workaround not works for me.
But I found a problem with this demo,it's not a bug, it's a principle of blazor. when you remove the first node, it will be compared with the second node(so does Vue.js).To avoid this, you can add a key for your component. like this:
@foreach(var model in _modelsDict)
{
<MyComponent @key="model.GetHashCode()"
Models="model.Value"
EnableWorkaround="false"
DefaultValue="@(model.Key == 0 ? _item1Default : _item2Default)">
</MyComponent>
}
Then it works
Hey there, sorry for the late reply. Using @key
actually works quite well. Thanks!