【发布时间】:2025-12-02 14:45:02
【问题描述】:
我有一个在这里说明问题的 CodePen:https://codepen.io/elegault/pen/QzZwLO
场景:一个 DetailsList 组件和一个搜索框(TextField 组件)。当用户在搜索框中键入时,可以过滤列表项。如果在搜索结果中,任何选定的项目仍将在搜索结果中被选中。如果它不在搜索结果中,并且后续搜索确实包含该选择,则会重新选择它。 (注意:Office UI Fabric 团队似乎意识到这应该在本地处理,但我不确定添加此功能的计划,正如GitHub issue)。
问题:每次按键后焦点都会丢失,这使得输入和编辑搜索条件变得困难,因为用户每次都必须重新插入光标。
什么不起作用:在 TextField 已经获得焦点时调用 focus() (isFocused = true) 什么都不做。调用 focus() 仅在 isFocused = false 时有效。但这仅在筛选列表中恢复选择后调用 DetailsList.focusIndex() 时才成立。
伪代码:
componentDidUpdate(previousProps: any, previousState: AppProjectListState) {
//Toggle off the current selection
this._selection.toggleIndexSelected(currentIdx);
//Set the new selection
this._selection.toggleIndexSelected(newIdx);
//Scroll the selection into view
this._detailsListRef.current.focusIndex(newIdx, false);
}
这是 TextField 或 DetailsList 组件中的某种错误吗?或者我在 React 组件生命周期中这样做的方式?或者有没有办法确保在用户键入和重新计算列表项以及修改所选索引时不会丢失 TextField 的焦点?
【问题讨论】:
标签: reactjs typescript office-ui-fabric