Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Descriptions Component does not support dynamic Description collection #3326

Open
congzhangzh opened this issue Jun 29, 2023 · 5 comments · May be fixed by #3330
Open

Descriptions Component does not support dynamic Description collection #3326

congzhangzh opened this issue Jun 29, 2023 · 5 comments · May be fixed by #3330

Comments

@congzhangzh
Copy link

congzhangzh commented Jun 29, 2023

Describe the bug

the dynamic collection does not work!

Steps to reproduce (please include code)

basic idea:

<Descriptions Title="User Info">
    <DescriptionsItem Title="UserName">Zhou Maomao</DescriptionsItem>
    <DescriptionsItem Title="Telephone">18100000000</DescriptionsItem>
    <DescriptionsItem Title="Live">Hangzhou, Zhejiang</DescriptionsItem>
    @if(showAdvance){
      <DescriptionsItem Title="Remark">Empty</DescriptionsItem>
      <DescriptionsItem Title="Address">
          No. 18, Wantang Road, Xihu District, Hangzhou, Zhejiang, China
      </DescriptionsItem>
     }
</Descriptions>
<button @onclick="@(()=>showAdvance=!showAdvance)" >switch mode</button>

private bool showAdvance=false;
@code{
}

Exceptions (if any)

n.a

Further technical details

AntDesign v0.15.1

ref:

https://antblazor.com/zh-CN/components/descriptions

@congzhangzh
Copy link
Author

I add a more interesting code to show the strange behavior:

the status and the counter response all work, but the descriptions & description does not!

@page "/counter"

<PageTitle>Counter</PageTitle>

<h1>Counter</h1>

<p role="status">Current count: @currentCount</p>

<button class="btn btn-primary" @onclick="IncrementCount">Click me</button>

<Descriptions Title="User Info">
    <DescriptionsItem Title="UserName">Zhou Maomao</DescriptionsItem>
    <DescriptionsItem Title="Telephone">18100000000</DescriptionsItem>
    <DescriptionsItem Title="Live">Hangzhou, Zhejiang</DescriptionsItem>
    @if(showAdvance){
      <DescriptionsItem Title="Remark">Empty</DescriptionsItem>
      <DescriptionsItem Title="Address">
          No. 18, Wantang Road, Xihu District, Hangzhou, Zhejiang, China
      </DescriptionsItem>
     }
</Descriptions>
<button @onclick="@(()=> { showAdvance = !showAdvance; currentCount++;})" >switch mode</button>
@currentCount <br/>
@showAdvance
@code {
    private int currentCount = 0;

    private bool showAdvance=false;
    private void IncrementCount()
    {
        currentCount++;
    }
}

@congzhangzh
Copy link
Author

Fyi. The same problem: dotnet/aspnetcore#47218

as the descriptionitem add itself to parent items in initialization which is later after descriptions parent set parameters, it will have nochance to trigger parent’s preparematrix internal status, which make later render render old data?

One solution is always prepare matrix before render? Another way is to use render fragments?

@ElderJames
Copy link
Member

I'm not sure if the render fragment will work, maybe it will reproduce the initialization logic when the render is refreshed.

@congzhangzh
Copy link
Author

I'm not sure if the render fragment will work, maybe it will reproduce the initialization logic when the render is refreshed.

you are right, it does not work! error as before!

@page "/counter"

<PageTitle>Counter</PageTitle>

<h1>Counter</h1>

<p role="status">Current count: @currentCount</p>

<button class="btn btn-primary" @onclick="IncrementCount">Click me</button>

<Descriptions Title="@(currentCount.ToString())">
    
    @GenDescriptions()
    
</Descriptions>
<button @onclick="@(()=> { showAdvance = !showAdvance; currentCount++;})" >switch mode</button>
@currentCount <br/>
@showAdvance
@code {
    private int currentCount = 0;

    RenderFragment GenDescriptions() => @<text><DescriptionsItem Title="UserName">Zhou Maomao</DescriptionsItem>
                                            <DescriptionsItem Title="Telephone">18100000000</DescriptionsItem>
                                            <DescriptionsItem Title="Live">Hangzhou, Zhejiang</DescriptionsItem>
                                            @if (showAdvance)
                                            {
                                                <DescriptionsItem Title="Remark">Empty</DescriptionsItem>
                                                <DescriptionsItem Title="Address">
                                                    No. 18, Wantang Road, Xihu District, Hangzhou, Zhejiang, China
                                                </DescriptionsItem>
                                            }</text>;
    private bool showAdvance=false;
    private void IncrementCount()
    {
        currentCount++;
    }
}

@congzhangzh congzhangzh linked a pull request Jul 1, 2023 that will close this issue
1 task
@congzhangzh
Copy link
Author

congzhangzh commented Jul 1, 2023

unless blazor provides something like OnBeforeRender/override StateChanged, there is no perfect solution, as the item init/dispose happens too later, which will cause Descriptions to use old items data.

on another side, the preparematrix is really just something about render internal, not related to the logical part, call before StateChanged internal is perfect

ref:

  1. OnBeforeRender dotnet/blazor#1716 (comment)
  2. https://chrissainty.com/understanding-cascading-values-and-cascading-parameters/
  3. https://blazor-university.com/components/component-lifecycles/images/component-lifecycle-1.jpg

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants