-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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] Remove instance.getTreeItemIdAttribute
#14667
Merged
Merged
Changes from 5 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
df8dbbd
[TreeView] Remove instance.getTreeItemIdAttribute
flaviendelangle ca40bde
Fix
flaviendelangle 261ddfc
Fix
flaviendelangle 9bf56d8
Merge branch 'master' into tree-id
flaviendelangle 8dd99af
Fix
flaviendelangle a7de701
Merge branch 'master' into tree-id
flaviendelangle 5b22645
Merge branch 'master' into tree-id
flaviendelangle 2cf1c25
Empty
flaviendelangle 0bffd84
Empty
flaviendelangle File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
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
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
32 changes: 23 additions & 9 deletions
32
packages/x-tree-view/src/internals/corePlugins/useTreeViewId/useTreeViewId.ts
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,26 +1,40 @@ | ||
import * as React from 'react'; | ||
import useId from '@mui/utils/useId'; | ||
import { TreeViewPlugin } from '../../models'; | ||
import { UseTreeViewIdSignature } from './useTreeViewId.types'; | ||
import { createTreeViewDefaultId } from './useTreeViewId.utils'; | ||
|
||
export const useTreeViewId: TreeViewPlugin<UseTreeViewIdSignature> = ({ params }) => { | ||
const treeId = useId(params.id); | ||
export const useTreeViewId: TreeViewPlugin<UseTreeViewIdSignature> = ({ | ||
params, | ||
state, | ||
setState, | ||
}) => { | ||
const treeId = state.id.treeId; | ||
|
||
const getTreeItemIdAttribute = React.useCallback( | ||
(itemId: string, idAttribute: string | undefined) => idAttribute ?? `${treeId}-${itemId}`, | ||
[treeId], | ||
); | ||
React.useEffect(() => { | ||
setState((prevState) => { | ||
if (prevState.id.treeId === params.id) { | ||
return prevState; | ||
} | ||
|
||
return { | ||
...prevState, | ||
id: { ...prevState.id, treeId: params.id ?? createTreeViewDefaultId() }, | ||
}; | ||
}); | ||
}, [setState, params.id]); | ||
|
||
return { | ||
getRootProps: () => ({ | ||
id: treeId, | ||
}), | ||
instance: { | ||
getTreeItemIdAttribute, | ||
contextValue: { | ||
treeId, | ||
}, | ||
}; | ||
}; | ||
|
||
useTreeViewId.params = { | ||
id: true, | ||
}; | ||
|
||
useTreeViewId.getInitialState = ({ id }) => ({ id: { treeId: id ?? createTreeViewDefaultId() } }); |
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
33 changes: 33 additions & 0 deletions
33
packages/x-tree-view/src/internals/corePlugins/useTreeViewId/useTreeViewId.utils.ts
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,33 @@ | ||
import { TreeViewItemId } from '../../../models'; | ||
|
||
let globalTreeViewDefaultId = 0; | ||
export const createTreeViewDefaultId = () => { | ||
globalTreeViewDefaultId += 1; | ||
return `mui-tree-view-${globalTreeViewDefaultId}`; | ||
}; | ||
|
||
/** | ||
* Generate the id attribute (i.e.: the `id` attribute passed to the DOM element) of a tree item. | ||
* If the user explicitly defined an id attribute, it will be returned. | ||
* Otherwise, the method creates a unique id for the item based on the Tree View id attribute and the item `itemId` | ||
* @param {object} params The parameters to determine the id attribute of the item. | ||
* @param {TreeViewItemId} params.itemId The id of the item to get the id attribute of. | ||
* @param {string | undefined} params.idAttribute The id attribute of the item if explicitly defined by the user. | ||
* @param {string} params.treeId The id attribute of the Tree View. | ||
* @returns {string} The id attribute of the item. | ||
*/ | ||
export const generateTreeItemIdAttribute = ({ | ||
id, | ||
treeId = '', | ||
itemId, | ||
}: { | ||
id: TreeViewItemId | undefined; | ||
treeId: string | undefined; | ||
itemId: string; | ||
}): string => { | ||
if (id != null) { | ||
return id; | ||
} | ||
|
||
return `${treeId}-${itemId}`; | ||
}; |
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
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
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
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.
Quick question: any reason why we are nesting
treeId
? Is it to be able to identify the state related to a certain plugin more easily? Should we do the same for other plugin states?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.
Yes, I took the habit to namespace everything in the state
Do we have some plugins where it is not the case?
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.
UseTreeViewFocusState
andUseTreeViewLabelState
do not follow this convention from what I seeThere 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.
I think I fixed those in #14210 👼
But that's something I should definitely extract