generated from graasp/graasp-repo
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add custom treeView to remove hidden items
- Loading branch information
1 parent
a6e1cbf
commit 93a7c30
Showing
6 changed files
with
230 additions
and
39 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
/* eslint-disable react/forbid-prop-types */ | ||
import React from 'react'; | ||
import Skeleton from '@material-ui/lab/Skeleton'; | ||
import TreeItem from '@material-ui/lab/TreeItem'; | ||
import PropTypes from 'prop-types'; | ||
import { buildTreeItemClass } from '../../../config/selectors'; | ||
import { ITEM_TYPES } from '../../../enums'; | ||
|
||
const LoadingTreeItem = <Skeleton variant="text" />; | ||
|
||
const CustomTreeItem = ({ | ||
itemId, | ||
expandedItems = [], | ||
selectedId, | ||
useChildren, | ||
useItem, | ||
useTags, | ||
showItemFilter, | ||
}) => { | ||
const { data: item, isLoading, isError } = useItem(itemId); | ||
const { data: tags, isLoading: isTagLoading } = useTags(itemId); | ||
|
||
const showItem = item && tags && showItemFilter?.(item, tags); | ||
const isExpanded = expandedItems?.includes(itemId); | ||
|
||
const { data: children, isLoading: childrenIsLoading } = useChildren(itemId, { | ||
enabled: Boolean( | ||
item && showItem && item.get('type') === ITEM_TYPES.FOLDER && isExpanded, | ||
), | ||
}); | ||
|
||
if (isLoading || isTagLoading) { | ||
return ( | ||
<TreeItem | ||
nodeId={`loading-${itemId}`} | ||
key={itemId} | ||
label={LoadingTreeItem} | ||
/> | ||
); | ||
} | ||
|
||
// display only folders | ||
if (!showItem || !item || isError) { | ||
return null; | ||
} | ||
|
||
const renderChildrenItems = () => { | ||
if (childrenIsLoading) { | ||
return LoadingTreeItem; | ||
} | ||
|
||
const filteredChildren = children?.filter((child) => | ||
showItemFilter?.(child), | ||
); | ||
|
||
if (!filteredChildren?.size) { | ||
return null; | ||
} | ||
|
||
return filteredChildren.map(({ id: childId }) => ( | ||
<CustomTreeItem | ||
key={childId} | ||
itemId={childId} | ||
expandedItems={expandedItems} | ||
selectedId={selectedId} | ||
useChildren={useChildren} | ||
useItem={useItem} | ||
useTags={useTags} | ||
showItemFilter={showItemFilter} | ||
/> | ||
)); | ||
}; | ||
|
||
const childrenTreeItems = renderChildrenItems(); | ||
|
||
// render child with checkbox | ||
const content = childrenIsLoading ? LoadingTreeItem : <>{item.get('name')}</>; | ||
|
||
// recursive display of children | ||
return ( | ||
<TreeItem | ||
key={itemId} | ||
nodeId={itemId} | ||
label={content} | ||
className={buildTreeItemClass(itemId)} | ||
> | ||
{childrenTreeItems} | ||
</TreeItem> | ||
); | ||
}; | ||
|
||
CustomTreeItem.propTypes = { | ||
itemId: PropTypes.string.isRequired, | ||
expandedItems: PropTypes.arrayOf(PropTypes.string).isRequired, | ||
selectedId: PropTypes.string.isRequired, | ||
useChildren: PropTypes.any.isRequired, | ||
useItem: PropTypes.any.isRequired, | ||
useTags: PropTypes.any.isRequired, | ||
showItemFilter: PropTypes.any.isRequired, | ||
}; | ||
|
||
export default CustomTreeItem; |
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,72 @@ | ||
/* eslint-disable react/forbid-prop-types */ | ||
import React, { useState } from 'react'; | ||
import TreeView from '@material-ui/lab/TreeView'; | ||
import ExpandMoreIcon from '@material-ui/icons/ExpandMore'; | ||
import ChevronRightIcon from '@material-ui/icons/ChevronRight'; | ||
import TreeItem from '@material-ui/lab/TreeItem'; | ||
import PropTypes from 'prop-types'; | ||
import CustomTreeItem from './CustomTreeItem'; | ||
|
||
const DynamicTreeView = ({ | ||
id, | ||
rootLabel, | ||
rootId, | ||
useItem, | ||
useTags, | ||
useChildren, | ||
initialExpendedItems = [], | ||
items, | ||
showItemFilter = () => true, | ||
selectedId, | ||
onTreeItemSelect, | ||
}) => { | ||
const [expandedItems, setExpandedItems] = useState(initialExpendedItems); | ||
|
||
// types based on TreeView types | ||
const onSelect = (event, value) => onTreeItemSelect?.(value); | ||
|
||
// types based on TreeView types | ||
const onToggle = (event, nodeIds) => setExpandedItems(nodeIds); | ||
|
||
return ( | ||
<TreeView | ||
id={id} | ||
defaultCollapseIcon={<ExpandMoreIcon />} | ||
defaultExpandIcon={<ChevronRightIcon />} | ||
onNodeSelect={onSelect} | ||
onNodeToggle={onToggle} | ||
expanded={expandedItems} | ||
> | ||
<TreeItem nodeId={rootId} label={rootLabel}> | ||
{items.map(({ id: itemId }) => ( | ||
<CustomTreeItem | ||
key={itemId} | ||
itemId={itemId} | ||
expandedItems={expandedItems} | ||
selectedId={selectedId} | ||
useItem={useItem} | ||
useChildren={useChildren} | ||
useTags={useTags} | ||
showItemFilter={showItemFilter} | ||
/> | ||
))} | ||
</TreeItem> | ||
</TreeView> | ||
); | ||
}; | ||
|
||
DynamicTreeView.propTypes = { | ||
id: PropTypes.string.isRequired, | ||
rootLabel: PropTypes.string.isRequired, | ||
rootId: PropTypes.string.isRequired, | ||
items: PropTypes.any.isRequired, | ||
initialExpendedItems: PropTypes.arrayOf(PropTypes.string).isRequired, | ||
selectedId: PropTypes.string.isRequired, | ||
onTreeItemSelect: PropTypes.any.isRequired, | ||
useItem: PropTypes.any.isRequired, | ||
useTags: PropTypes.any.isRequired, | ||
useChildren: PropTypes.any.isRequired, | ||
showItemFilter: PropTypes.any.isRequired, | ||
}; | ||
|
||
export default DynamicTreeView; |
Oops, something went wrong.