-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[TreeView] Remove
instance.getTreeItemIdAttribute
(mui#14667)
- Loading branch information
1 parent
42b7b13
commit 4bce2f6
Showing
12 changed files
with
93 additions
and
40 deletions.
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