fix: jump to items from search result

This doesn't cause the previous redirect bug :)
This commit is contained in:
EnixCoda 2020-10-28 18:14:51 +08:00
parent b08cc31940
commit 59fffaf004
No known key found for this signature in database
GPG key ID: 0C1A07377913A1DD
2 changed files with 13 additions and 6 deletions

View file

@ -180,15 +180,16 @@ function ListView({
visibleNodes,
}: ListViewProps & Pick<Props, 'metaData'> & Pick<ConnectorState, 'expandTo'>) {
const listRef = React.useRef<FixedSizeList>(null)
const { focusedNode, nodes } = visibleNodes
// the change of depths indicates switch into/from search state
React.useEffect(() => {
const { focusedNode, nodes } = visibleNodes
if (listRef.current && focusedNode?.path) {
const index = nodes.findIndex(node => node.path === focusedNode.path)
if (index !== -1) {
listRef.current.scrollToItem(index, 'smart')
}
}
}, [focusedNode?.path])
}, [visibleNodes])
// For some reason, removing the deps array above results in bug:
// If scroll fast and far, then clicking on items would result in redirect
// Not know the reason :(
@ -215,7 +216,7 @@ function ListView({
ref={listRef}
itemKey={(index, { visibleNodes }) => visibleNodes?.nodes[index]?.path}
itemData={itemData}
itemCount={nodes.length}
itemCount={visibleNodes.nodes.length}
itemSize={36}
height={height}
width={width}

View file

@ -284,10 +284,16 @@ class FlattenLayer extends CompressLayer {
await traverse(
[rootNode],
async node => {
const match = path.startsWith(node.path)
if (node.path && match) {
const overflowChar = node.path[path.length + 1]
const match = path.startsWith(node.path) && (overflowChar === '/' || !overflowChar)
if (node.path) {
// rootNode.path === ''
await this.$setExpand(node, true)
if (match) {
if (node.path === path) {
// do not wait for expansion for the exact node as that will block "jumping from search"
this.$setExpand(node, true)
} else await this.$setExpand(node, true)
}
}
return match
},