-
-
Notifications
You must be signed in to change notification settings - Fork 375
fix: Select component value incorrectly changes when external StateHa… #7618
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: main
Are you sure you want to change the base?
fix: Select component value incorrectly changes when external StateHa… #7618
Conversation
…sChanged is called during search
|
Thanks for your PR, @cwtalent. Someone from the team will get assigned to your PR shortly and we'll get it reviewed. |
Reviewer's guide (collapsed on small PRs)Reviewer's GuideAdjusts Select component item resolution to use the full, unfiltered item list instead of the search-filtered list so external StateHasChanged calls no longer corrupt the selected value while searching. Sequence diagram for Select search with external StateHasChanged after fixsequenceDiagram
actor User
participant ParentComponent
participant BlazorRenderer
participant Select
participant GetItemByRows
User->>Select: Open dropdown and select A1
Select->>Select: Set CurrentValueAsString = A1
User->>Select: Type search text 2
Select->>Select: Update SearchText
Select->>Select: Filter Items into Rows (A2 only)
ParentComponent->>ParentComponent: Timer tick
ParentComponent->>ParentComponent: StateHasChanged
ParentComponent->>BlazorRenderer: Request rerender
BlazorRenderer->>Select: Rebuild component tree
Select->>GetItemByRows: Resolve selected item
GetItemByRows->>Select: GetRowsByItems
Select-->>GetItemByRows: allItems (A1, A2, A3)
GetItemByRows->>GetItemByRows: Find item where Value == CurrentValueAsString in allItems
GetItemByRows-->>Select: SelectedItem A1
Select-->>BlazorRenderer: Render with value A1
BlazorRenderer-->>User: UI shows A1 selected while search text is 2
Updated class diagram for Select GetItemByRows logicclassDiagram
class Select {
List~SelectedItem~ Rows
string CurrentValueAsString
List~SelectedItem~ GetRowsByItems()
SelectedItem GetItemByRows()
}
class SelectedItem {
string Value
bool Active
bool IsDisabled
}
Select --> SelectedItem : uses
class GetItemByRowsLogic {
List~SelectedItem~ allItems
+SelectedItem ResolveSelectedItem(string currentValueAsString)
}
Select ..> GetItemByRowsLogic : delegates selection resolution
%% Implementation intent of GetItemByRows after fix
GetItemByRowsLogic : allItems = GetRowsByItems()
GetItemByRowsLogic : ResolveSelectedItem
GetItemByRowsLogic : 1. Try GetItemWithEnumValue
GetItemByRowsLogic : 2. Find in allItems by Value == CurrentValueAsString
GetItemByRowsLogic : 3. Else first Active in allItems
GetItemByRowsLogic : 4. Else first not IsDisabled in allItems
File-Level Changes
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
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.
Hey - I've left some high level feedback:
- GetRowsByItems() is now called on every GetItemByRows() invocation; if it does any non-trivial work, consider caching its result for the duration of a render or reusing an existing backing field to avoid repeated recomputation.
- Since GetItemByRows() now ignores the filtered Rows collection entirely, double-check whether any callers relied on the previous behavior of preferring an active item within the current filtered view, and if so consider separating 'current value resolution' from 'filtered active item fallback' to preserve that nuance.
Prompt for AI Agents
Please address the comments from this code review:
## Overall Comments
- GetRowsByItems() is now called on every GetItemByRows() invocation; if it does any non-trivial work, consider caching its result for the duration of a render or reusing an existing backing field to avoid repeated recomputation.
- Since GetItemByRows() now ignores the filtered Rows collection entirely, double-check whether any callers relied on the previous behavior of preferring an active item within the current filtered view, and if so consider separating 'current value resolution' from 'filtered active item fallback' to preserve that nuance.Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
|
@microsoft-github-policy-service agree |
问题描述 / Problem Description
当 Select 组件启用搜索功能(
ShowSearch="true")且处于搜索状态时,如果父组件调用StateHasChanged(),会导致 Select 的选中值被错误地修改。复现步骤 / Steps to Reproduce
StateHasChanged()StateHasChanged()复现代码示例
根本原因 / Root Cause
在
Select.razor.cs的GetItemByRows()方法中,使用了Rows属性来查找当前选中项。而Rows属性会根据SearchText返回过滤后的数据。当用户在搜索框输入内容时:
Rows只包含搜索结果(例如只有 A2)StateHasChanged()触发组件重新渲染GetItemByRows()在过滤后的Rows中找不到 A1Rows.FirstOrDefault(),即 A2解决方案 / Solution
修改
GetItemByRows()方法,使用完整的未过滤列表GetRowsByItems()而不是过滤后的Rows来查找当前选中项。性能影响 / Performance Impact
测试验证 / Testing
修复后,按照上述"复现步骤"进行测试:
StateHasChanged()影响范围 / Impact
GetItemByRows()方法的实现Summary by Sourcery
Bug Fixes: