-
Notifications
You must be signed in to change notification settings - Fork 929
Fix: TreeItem Template click does not toggle Checkbox #2374
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
base: master
Are you sure you want to change the base?
Fix: TreeItem Template click does not toggle Checkbox #2374
Conversation
Radzen.Blazor/RadzenTreeItem.razor
Outdated
| @if (Template != null) | ||
| { | ||
| <div class=@LabelClass @onkeydown:stopPropagation>@Template(this)</div> | ||
| <div class=@LabelClass @onkeydown:stopPropagation @onclick="@OnTemplateClick">@Template(this)</div> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don’t think that clicking on the container that holds the custom template to check the checkbox is a good idea. Instead, clicking on the default label should perform that action, and developers should have the ability to override this behavior when defining a custom template.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That makes sense. I agree on reflection that wrapping the container is risky if the developer intends to have interactive elements (like buttons) inside the template.
However, there's currently have a gap: if a developer uses a Template (e.g., just to add an icon next to the text), they lose the standard "click text to select" behaviour. To restore it, they currently have to re-implement the checkbox logic themselves. Since Radzen’s internal logic handles complex cascading states (parents/children updates), duplicating that logic in user-land is error-prone and could conflict with the internal state.
Proposal
Instead of the UI wrapper, could we expose the internal selection logic on the RadzenTreeItem?
If we make CheckedChange public (or introduce a helper like public async Task ToggleSelection()), developers could opt-in to the behaviour safely within their custom template:
<Template Context="item">
<span @onclick="@(() => item.ToggleSelection())">
<RadzenIcon Icon="folder" /> @item.Text
</span>
</Template>This solves the issue of the "dead" label while keeping the UI flexible and safe from event bubbling issues. If you agree, I can update the PR to expose this method instead.
| }); | ||
| } | ||
|
|
||
| internal async Task OnTemplateClick(MouseEventArgs args) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this method still needed?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
your right its no longer used, i had removed it but didnt realise the commit and push was not done/missed. i have pushed the change now
| /// <remarks>This method has no effect if the tree does not support checkboxes or if the node is | ||
| /// not checkable.</remarks> | ||
| /// <returns>A task that represents the asynchronous toggle operation.</returns> | ||
| public async Task ToggleCheckboxSelection() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In my opinion ToggleChecked() will be better.
Here is a professional PR description template formatted with Markdown, ready to be pasted into GitHub.
Fix: TreeItem Template click does not toggle Checkbox
Summary
This PR fixes an inconsistency in the
RadzenTreeItemcomponent where clicking the content of a customTemplatefails to toggle the associated checkbox, whereas clicking defaultTextworks as expected.The Issue
When
AllowCheckBoxes="true", the default rendering logic wraps the item text in a<label for="...">. This leverages native HTML behavior to toggle the checkbox when the text is clicked.However, when a
<Template>is provided, the content is wrapped in a standard<div>. As a result, clicking the template content triggers the Selection (highlight) event via event bubbling, but fails to trigger the Checked state change. This creates a confusing UX where the user expects the checkbox to toggle upon clicking the row, but it does not.The Fix
I have updated
RadzenTreeItem.razorandRadzenTreeItem.razor.csto programmatically handle the toggle logic when a Template is clicked.<div>(rather than changing it to a<label>) to ensure valid HTML semantics, as custom templates may contain interactive elements (buttons, inputs) which are illegal inside a label.OnTemplateClickmethod that checks ifAllowCheckBoxesis true and toggles theCheckedChangestate.@onclick="OnTemplateClick"to the template wrapperdiv.Technical Changes
RadzenTreeItem.razor.cs
Added handler logic:
RadzenTreeItem.razor
Updated markup to attach the handler:
Reproduction Steps
RadzenTreewithAllowCheckBoxes="true".<Template>forRadzenTreeLevel.Checklist