-
-
Notifications
You must be signed in to change notification settings - Fork 381
feat(Select): use DynamicElement ensure release event callback #7894
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -202,6 +202,8 @@ public bool IsUseActiveWhenValueIsNull | |
|
|
||
| private string _defaultVirtualizedItemText = ""; | ||
|
|
||
| private EventCallback<ChangeEventArgs> _onChangeEventCallback = EventCallback<ChangeEventArgs>.Empty; | ||
|
|
||
| private SelectedItem? SelectedItem { get; set; } | ||
|
|
||
| private SelectedItem? SelectedRow | ||
|
|
@@ -234,6 +236,10 @@ protected override void OnParametersSet() | |
| NoSearchDataText ??= Localizer[nameof(NoSearchDataText)]; | ||
| DropdownIcon ??= IconTheme.GetIconByKey(ComponentIcons.SelectDropdownIcon); | ||
| ClearIcon ??= IconTheme.GetIconByKey(ComponentIcons.SelectClearIcon); | ||
|
|
||
| _onChangeEventCallback = IsEditable | ||
| ? EventCallback.Factory.Create<ChangeEventArgs>(this, OnChange) | ||
| : EventCallback<ChangeEventArgs>.Empty; | ||
| } | ||
|
|
||
| /// <summary> | ||
|
|
@@ -389,6 +395,8 @@ private async Task OnClickItem(SelectedItem item) | |
| _defaultVirtualizedItemText = item.Text; | ||
| await SelectedItemChanged(item); | ||
| } | ||
|
|
||
| StateHasChanged(); | ||
| } | ||
|
Comment on lines
396
to
400
|
||
|
|
||
| private async Task SelectedItemChanged(SelectedItem item) | ||
|
|
@@ -494,11 +502,26 @@ private async Task OnChange(ChangeEventArgs args) | |
| // 修复:使用完整的未过滤列表来查找当前选中项 | ||
| // 避免在用户搜索时被外部 StateHasChanged 影响导致值被错误修改 | ||
| var allItems = GetRowsByItems(); | ||
|
|
||
| var item = GetItemWithEnumValue() | ||
| ?? allItems.Find(i => i.Value == CurrentValueAsString) | ||
| ?? allItems.Find(i => i.Active) | ||
| ?? allItems.Find(i => !i.IsDisabled); | ||
| return item; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// <inheritdoc/> | ||
| /// </summary> | ||
| /// <param name="disposing"></param> | ||
| /// <returns></returns> | ||
| protected override ValueTask DisposeAsync(bool disposing) | ||
| { | ||
| if (disposing) | ||
| { | ||
| _onChangeEventCallback = EventCallback<ChangeEventArgs>.Empty; | ||
| } | ||
|
|
||
| return base.DisposeAsync(disposing); | ||
| } | ||
| } | ||
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.
issue (complexity): Consider removing the stateful EventCallback field and explicit StateHasChanged call by handling editability and rendering declaratively or within the handler to keep the component logic simpler and more self-contained.
You can simplify this change by removing the stateful
EventCallbackfield and its disposal, and by tightening the click handler.1. Remove
_onChangeEventCallbackfield and lifecycle wiringThe field and
OnParametersSet/DisposeAsynclogic are only used to conditionally wireOnChange. You can achieve the same behavior declaratively in the.razorfile without extra mutable state or lifecycle coupling.Instead of:
Bind directly in the
.razor:Then you can delete:
and the
DisposeAsyncoverride entirely. The component instance is short‑lived; clearing a value-type callback inDisposeAsyncbrings no real benefit.If you prefer to keep the binding simple, another option is to short‑circuit inside the handler:
and then use a straightforward binding:
Either pattern removes the extra field and lifecycle complexity while preserving behavior.
2. Reconsider explicit
StateHasChangedinOnClickItemOnClickItemcurrently forces a render:If
SelectedItemChangedflows intoCurrentValue/ parameter updates that already trigger a render (which is common in Blazor forms), the explicitStateHasChanged()is redundant and can be removed:If you’ve hit a specific scenario where a manual refresh is required (e.g., async external state not bound to the component), consider adding a brief comment so future readers understand why it’s needed: