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

MudTreeViewItem: Add 'BodyContent' which renders instead of text, end text and end icon. #3831

Merged
merged 34 commits into from
Apr 1, 2023

Conversation

rpc-scandinavia
Copy link
Contributor

@rpc-scandinavia rpc-scandinavia commented Jan 28, 2022

Description

The 'Content' render fragment replaces the entire tree view item, which mean that you have to handle the toggle button and logic your self.

I needed to use the nice logic of the tree view (loading, expanding/collapsing etc.), but provide a custom body.

I also needed a way to reload the tree view item.

How Has This Been Tested?

Uses it in mu project.

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)

Checklist:

  • The PR is submitted to the correct branch (dev).
  • My code follows the code style of this project.
  • I've added relevant tests.

@mikes-gh
Copy link
Contributor

mikes-gh commented Feb 1, 2022

This doesn't build and would need tests for the new API added. But before you spend more time on this we would need to see what the new API brings. @tungi52

@rpc-scandinavia
Copy link
Contributor Author

Interesting with a new API in sight.

I use this in my project, with "MudTreeView" renamed to "MudTreeView1". The compile error was one of the "MudTreeView1" left over.

@codecov
Copy link

codecov bot commented Feb 4, 2022

Codecov Report

Patch coverage: 95.00% and project coverage change: -0.01 ⚠️

Comparison is base (e0afebd) 91.37% compared to head (519b0d6) 91.37%.

Additional details and impacted files
@@            Coverage Diff             @@
##              dev    #3831      +/-   ##
==========================================
- Coverage   91.37%   91.37%   -0.01%     
==========================================
  Files         397      397              
  Lines       14919    14929      +10     
==========================================
+ Hits        13632    13641       +9     
- Misses       1287     1288       +1     
Impacted Files Coverage Δ
...MudBlazor/Components/TreeView/MudTreeView.razor.cs 100.00% <ø> (ø)
...lazor/Components/TreeView/MudTreeViewItem.razor.cs 97.43% <92.85%> (-0.73%) ⬇️
...udBlazor/Components/TreeView/MudTreeViewItem.razor 100.00% <100.00%> (ø)

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

☔ View full report in Codecov by Sentry.
📢 Do you have feedback about the report comment? Let us know in this issue.

@tungi52
Copy link
Contributor

tungi52 commented Feb 5, 2022

The new BodyContent would be nice, if someone need a fully customizable content for a MudTreeItem. Also I understand why the Reload method is necessary, at the moment there is no way to update children from server. I'm only not sure about the naming, on the other hand it is ok. However ReloadParent seems a bit meaningless to me, can you explain, why it is necessary? Also some tests would be great.

@mikes-gh
Copy link
Contributor

mikes-gh commented Feb 7, 2022

I know this is still in discussion but BodyContent should be ChildContent in order to be consistent with the rest of the library

@rpc-scandinavia
Copy link
Contributor Author

I know this is still in discussion but BodyContent should be ChildContent in order to be consistent with the rest of the library

I just wanted to make that change, but realised that ChildContent is already used.

@tungi52
Copy link
Contributor

tungi52 commented Feb 11, 2022

Thanks! For me this is ok. Can you add test(s) and an example to demonstrate the new features?

@rpc-scandinavia
Copy link
Contributor Author

Thanks! For me this is ok. Can you add test(s) and an example to demonstrate the new features?

I will look at that.

@tungi52
Copy link
Contributor

tungi52 commented Feb 19, 2022

@rpc-scandinavia This doesn't build for me.

@ScarletKuro
Copy link
Member

I didn't see this PR when I was creating this isssue #4503 but it looks like this changes should also solve the problems described in the ticket. I will leave the ticket open tho, until this is merged.

@soend
Copy link

soend commented Jun 15, 2022

Anything else stopping this PR to get accepted and merged? I also need a way to use ServerData with custom MudTreeViewItem content. This PR would solve that issue.

@just-the-benno
Copy link
Contributor

I'm sorry that it took as so long to get back.

@rpc-scandinavia, is there still life in this PR? Can we resurrect it?

@rpc-scandinavia
Copy link
Contributor Author

is there still life in this PR? Can we resurrect it?

Well, everything worked, the only thing missing was the test - and I don't know how to make the test I want. Anyway, there has been so many changes at your end, that I can't get my GitHub copy to work - which probably mean that I have to delete my copy, fork it again, make all the changes again, and create a new pull request. So much hazzle for around 10 lines of code.

I have been using my solution since the first post, and I think we ended up with something good. There was however this question:

With 3) you can consider if BodyContent should be named BodyContentWithContext for consistency.

I only want to spend more time on this GitHub pull, if you put it into the project.

@rpc-scandinavia
Copy link
Contributor Author

Deleted my fork, and forked the project again.

@rpc-scandinavia
Copy link
Contributor Author

Ok @henon, I corrected the spelling errors and added some simple tests.

@rpc-scandinavia
Copy link
Contributor Author

@henon, another thing in MudTreeViewItem.razor.cs, I had to replace all:

StateHasChanged();

With:

InvokeAsync(() => StateHasChanged());

Because of:

Unhandled exception. System.InvalidOperationException: The current thread is not associated with the Dispatcher. Use InvokeAsync() to switch execution to the Dispatcher when triggering rendering or component state.

Do you think that should be done in this PR ?

@ScarletKuro
Copy link
Member

I wonder if it's possible to test the new Reload method that was introduced.

another thing in MudTreeViewItem.razor.cs, I had to replace all:

Can you be more specific where, there are many of them
But if the method is Task, i would propose await InvokeAsync(StateHasChanged);

FYI: I think github it dead and you can't push rn.

@ScarletKuro
Copy link
Member

ScarletKuro commented Mar 27, 2023

Not related to this PR, but why is TryInvokeServerLoadFunc an async void instead of async Task. I see no reason for it and it's always called from a Task and you would be able to await it.
Why _childItems.ForEach(async c => await c.SelectItem(value, source)); (void async lambda) instead of

foeach (var item in _childItems){
    await item.SelectItem(value, source)
}

I recommend to avoid async void unless totally necessary i.e. in event handler, for example.

I kinda want to propose to make Reload ->ReloadAsync with an Task since MudTree has async semantics. the MudTreeRoot.ServerData is async and it would allow me to make a PR with some inner changes to improve the component without breaking the public API.

@henon
Copy link
Collaborator

henon commented Mar 27, 2023

Yes and of course the ReloadAsync function can also be tested easily. I.e. by changin the server data before calling it and checking that the changed data is reflected in the tree.

@henon
Copy link
Collaborator

henon commented Mar 27, 2023

Do you think that should be done in this PR ?

Only if you are sure it is necessary. The first question to ask is why were they called from the wrong thread. What caused this?

@ScarletKuro
Copy link
Member

Only if you are sure it is necessary. The first question to ask is why were they called from the wrong thread. What caused this?

It's only my guess, but maybe he modified some state in a test, like changing value of [parameter] property.
Those should be called like this in a test
await comp.InvokeAsync(() => item.Instance.Disabled = true);
await comp.InvokeAsync(() => comp.Instance.SelectFirst());

@rpc-scandinavia
Copy link
Contributor Author

Not related to this PR, but why is TryInvokeServerLoadFunc an async void instead of async Task.

I saw that as well, I am working on another PR where I call TryInvokeServerLoadFunc and noticed that. So I changed it.

And my InvokeAsync are awaited.

@ScarletKuro
Copy link
Member

I saw that as well

Ok, but lets keep it as is in this PR.

Please, change void Reload to void ReloadAsync and add test for this method like henon suggested:

Yes and of course the ReloadAsync function can also be tested easily. I.e. by changin the server data before calling it and checking that the changed data is reflected in the tree.

And then I guess we are good to go and merge this PR.

}
else if (MudTreeRoot != null)
{
MudTreeRoot.CallStateHasChanged();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you rebase to latest changes there is new interface now and you can do something like this
((IMudStateHasChanged)MudTreeRoot).StateHasChanged() and remove the CallStateHasChanged

@rpc-scandinavia
Copy link
Contributor Author

@ScarletKuro, I have made the async and IMudStateHasChanged changes.

@ScarletKuro
Copy link
Member

lgtm

@henon henon changed the title MudTreeViewItem: Added 'BodyContent' which renders insted of text, end text and end icon. MudTreeViewItem: Add 'BodyContent' which renders instead of text, end text and end icon. Apr 1, 2023
@henon henon merged commit 79e91c1 into MudBlazor:dev Apr 1, 2023
@henon
Copy link
Collaborator

henon commented Apr 1, 2023

Thanks for your contribution @rpc-scandinavia !

ScarletKuro pushed a commit to ScarletKuro/MudBlazor that referenced this pull request Apr 4, 2023
… text and end icon. (MudBlazor#3831)

MudTreeViewItem: Added 'BodyContent' which renders instead of text, end text and end icon.
ilovepilav pushed a commit to ilovepilav/MudBlazor that referenced this pull request Nov 25, 2023
… text and end icon. (MudBlazor#3831)

MudTreeViewItem: Added 'BodyContent' which renders instead of text, end text and end icon.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
API change API that needs approval enhancement New feature or request needs tests
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

7 participants