-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
[TreeView] Do not use outdated version of the state to compute new label first char in RichTreeView
#12512
Merged
flaviendelangle
merged 9 commits into
mui:master
from
flaviendelangle:outdated-item-node-firstChar
Mar 25, 2024
Merged
[TreeView] Do not use outdated version of the state to compute new label first char in RichTreeView
#12512
Changes from 7 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
2b0a1e5
[TreeView] Do not used outdated version of the state to compute new l…
flaviendelangle cb0faa8
Add test
flaviendelangle cf36ebd
Fix
flaviendelangle 8cf9680
Merge branch 'next' into outdated-item-node-firstChar
flaviendelangle b391139
Fix
flaviendelangle 370dc43
Merge branch 'master' into outdated-item-node-firstChar
flaviendelangle ba03b55
Merge branch 'master' into outdated-item-node-firstChar
flaviendelangle 8c3e0fc
Merge branch 'master' into outdated-item-node-firstChar
flaviendelangle 35fbec8
Review Nora
flaviendelangle File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 53 additions & 0 deletions
53
...rc/internals/plugins/useTreeViewKeyboardNavigation/useTreeViewKeyboardNavigation.test.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import * as React from 'react'; | ||
import { expect } from 'chai'; | ||
import { act, createRenderer, fireEvent } from '@mui-internal/test-utils'; | ||
import { RichTreeView } from '@mui/x-tree-view/RichTreeView'; | ||
|
||
describe('useTreeViewKeyboardNavigation', () => { | ||
const { render } = createRenderer(); | ||
|
||
it('should work after adding / removing items', () => { | ||
const { getByRole, setProps } = render( | ||
<RichTreeView | ||
items={[ | ||
{ id: 'one', label: 'one' }, | ||
{ id: 'two', label: 'two' }, | ||
{ id: 'three', label: 'three' }, | ||
{ id: 'four', label: 'four' }, | ||
]} | ||
/>, | ||
); | ||
|
||
act(() => { | ||
getByRole('treeitem', { name: 'one' }).focus(); | ||
}); | ||
|
||
fireEvent.keyDown(getByRole('treeitem', { name: 'one' }), { key: 'f' }); | ||
expect(getByRole('treeitem', { name: 'four' })).toHaveFocus(); | ||
|
||
setProps({ | ||
items: [ | ||
{ id: 'one', label: 'one' }, | ||
{ id: 'two', label: 'two' }, | ||
{ id: 'three', label: 'three' }, | ||
], | ||
}); | ||
expect(getByRole('treeitem', { name: 'one' })).toHaveFocus(); | ||
|
||
fireEvent.keyDown(getByRole('treeitem', { name: 'one' }), { key: 't' }); | ||
expect(getByRole('treeitem', { name: 'two' })).toHaveFocus(); | ||
|
||
setProps({ | ||
items: [ | ||
{ id: 'one', label: 'one' }, | ||
{ id: 'two', label: 'two' }, | ||
{ id: 'three', label: 'three' }, | ||
{ id: 'four', label: 'four' }, | ||
], | ||
}); | ||
expect(getByRole('treeitem', { name: 'two' })).toHaveFocus(); | ||
|
||
fireEvent.keyDown(getByRole('treeitem', { name: 'two' }), { key: 'f' }); | ||
expect(getByRole('treeitem', { name: 'four' })).toHaveFocus(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
import * as React from 'react'; | ||
import { useTheme } from '@mui/material/styles'; | ||
import useEventCallback from '@mui/utils/useEventCallback'; | ||
import { TreeViewPlugin } from '../../models'; | ||
import { TreeViewNode, TreeViewPlugin } from '../../models'; | ||
import { | ||
getFirstNode, | ||
getLastNode, | ||
|
@@ -13,7 +13,6 @@ import { | |
TreeViewFirstCharMap, | ||
UseTreeViewKeyboardNavigationSignature, | ||
} from './useTreeViewKeyboardNavigation.types'; | ||
import { TreeViewBaseItem } from '../../../models'; | ||
import { MuiCancellableEvent } from '../../models/MuiCancellableEvent'; | ||
|
||
function isPrintableCharacter(string: string) { | ||
|
@@ -31,36 +30,31 @@ function findNextFirstChar(firstChars: string[], startIndex: number, char: strin | |
|
||
export const useTreeViewKeyboardNavigation: TreeViewPlugin< | ||
UseTreeViewKeyboardNavigationSignature | ||
> = ({ instance, params }) => { | ||
> = ({ instance, params, state }) => { | ||
const theme = useTheme(); | ||
const isRTL = theme.direction === 'rtl'; | ||
const firstCharMap = React.useRef<TreeViewFirstCharMap>({}); | ||
const hasFirstCharMapBeenUpdatedImperatively = React.useRef(false); | ||
|
||
const updateFirstCharMap = useEventCallback( | ||
(callback: (firstCharMap: TreeViewFirstCharMap) => TreeViewFirstCharMap) => { | ||
hasFirstCharMapBeenUpdatedImperatively.current = true; | ||
firstCharMap.current = callback(firstCharMap.current); | ||
}, | ||
); | ||
|
||
React.useEffect(() => { | ||
if (hasFirstCharMapBeenUpdatedImperatively.current) { | ||
if (instance.isItemUpdatePrevented()) { | ||
return; | ||
} | ||
|
||
const newFirstCharMap: { [itemId: string]: string } = {}; | ||
|
||
const processItem = (item: TreeViewBaseItem) => { | ||
const getItemId = params.getItemId; | ||
const itemId = getItemId ? getItemId(item) : (item as { id: string }).id; | ||
newFirstCharMap[itemId] = instance.getNode(itemId).label!.substring(0, 1).toLowerCase(); | ||
item.children?.forEach(processItem); | ||
const processItem = (node: TreeViewNode) => { | ||
newFirstCharMap[node.id] = node.label!.substring(0, 1).toLowerCase(); | ||
}; | ||
|
||
params.items.forEach(processItem); | ||
Object.values(state.nodes.nodeMap).forEach(processItem); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Btw this will support stuff like It's still far from perfect because we loop through the whole dataset, but until we actually have partial dataset update it's good enough. |
||
firstCharMap.current = newFirstCharMap; | ||
}, [params.items, params.getItemId, instance]); | ||
}, [state.nodes.nodeMap, params.getItemId, instance]); | ||
|
||
const getFirstMatchingItem = (itemId: string, firstChar: string) => { | ||
let start: number; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Not directly related but I noticed this bug while writing the tests.
If we remove the currently focused item from
props.items
, then itsTreeItem
while callremoveFocusedItem
, but the state does not contain the item anymore so it crashes.