ant-design-blazor icon indicating copy to clipboard operation
ant-design-blazor copied to clipboard

Select does not recognize that a list's component was removed

Open Velociraptor45 opened this issue 2 years ago • 1 comments

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) grafik

Clicking the '+' Button will add another component (With 'Item2' as default selection) grafik

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: grafik

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 grafik

However, there is a workaround by setting the Select's Value every time OnParameterSet() is called: grafik

Further technical details

  • AntDesign Nuget Package version: 0.12.0.1

Velociraptor45 avatar Sep 03 '22 12:09 Velociraptor45

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

WhyILoveSpringRoll avatar Sep 15 '22 01:09 WhyILoveSpringRoll

Hey there, sorry for the late reply. Using @key actually works quite well. Thanks!

Velociraptor45 avatar Nov 12 '22 14:11 Velociraptor45