-
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: navigator render extra item (#688)
* feat: add a menu for item actions * feat: enhance navigation to receive extra items * feat: display seperator even if there's no item --------- Co-authored-by: kim <[email protected]>
- Loading branch information
Showing
7 changed files
with
311 additions
and
59 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import truncate from 'lodash.truncate'; | ||
|
||
import { Typography } from '@mui/material'; | ||
|
||
import { DiscriminatedItem, ItemType } from '@graasp/sdk'; | ||
|
||
import ItemMenu, { ItemMenuProps } from './ItemMenu'; | ||
import { CenterAlignWrapper, ITEM_NAME_MAX_LENGTH, StyledLink } from './utils'; | ||
|
||
export interface CurrentItemProps { | ||
item: DiscriminatedItem; | ||
buildBreadcrumbsItemLinkId?: (id: string) => string; | ||
buildIconId?: (id: string) => string; | ||
buildMenuId?: (id: string) => string; | ||
buildMenuItemId?: (id: string) => string; | ||
useChildren: ItemMenuProps['useChildren']; | ||
buildToItemPath: (id: string) => string; | ||
showArrow: boolean; | ||
} | ||
const CurrentItemNavigation = ({ | ||
item, | ||
buildBreadcrumbsItemLinkId, | ||
buildToItemPath, | ||
useChildren, | ||
buildIconId, | ||
buildMenuId, | ||
buildMenuItemId, | ||
showArrow, | ||
}: CurrentItemProps): JSX.Element | null => { | ||
return ( | ||
<CenterAlignWrapper> | ||
<StyledLink | ||
id={buildBreadcrumbsItemLinkId?.(item.id)} | ||
key={item.id} | ||
to={buildToItemPath(item?.id)} | ||
> | ||
<Typography> | ||
{truncate(item.name, { length: ITEM_NAME_MAX_LENGTH })} | ||
</Typography> | ||
</StyledLink> | ||
{(item.type === ItemType.FOLDER || showArrow) && ( | ||
<ItemMenu | ||
useChildren={useChildren} | ||
itemId={item.id} | ||
buildToItemPath={buildToItemPath} | ||
buildIconId={buildIconId} | ||
buildMenuItemId={buildMenuItemId} | ||
buildMenuId={buildMenuId} | ||
renderArrow={showArrow} | ||
/> | ||
)} | ||
</CenterAlignWrapper> | ||
); | ||
}; | ||
|
||
export default CurrentItemNavigation; |
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,69 @@ | ||
import { IconButtonProps, Menu, MenuItem, Typography } from '@mui/material'; | ||
|
||
import React from 'react'; | ||
import { Link } from 'react-router-dom'; | ||
|
||
import { MenuItemType } from './Navigation'; | ||
import { Separator, StyledIconButton } from './utils'; | ||
|
||
export type ExtraItemsMenuProps = { | ||
icon?: JSX.Element; | ||
menuItems: MenuItemType[]; | ||
buildIconId?: (id: string) => string; | ||
buildMenuId?: (itemId: string) => string; | ||
name: string; | ||
}; | ||
|
||
const ExtraItemsMenu = ({ | ||
icon = Separator, | ||
menuItems, | ||
buildIconId, | ||
buildMenuId, | ||
name, | ||
}: ExtraItemsMenuProps): JSX.Element => { | ||
const [anchorEl, setAnchorEl] = React.useState<null | HTMLElement>(null); | ||
const open = Boolean(anchorEl); | ||
const handleClick: IconButtonProps['onClick'] = (event) => { | ||
setAnchorEl(event.currentTarget); | ||
}; | ||
|
||
const handleClose = (): void => { | ||
setAnchorEl(null); | ||
}; | ||
|
||
return ( | ||
<> | ||
<StyledIconButton | ||
onClick={handleClick} | ||
aria-haspopup='true' | ||
id={buildIconId?.(name)} | ||
aria-expanded={open ? true : undefined} | ||
> | ||
{icon} | ||
</StyledIconButton> | ||
<Menu | ||
anchorEl={anchorEl} | ||
open={open} | ||
id={buildMenuId?.(name)} | ||
onClose={handleClose} | ||
onClick={handleClose} | ||
anchorOrigin={{ | ||
vertical: 'bottom', | ||
horizontal: 'right', | ||
}} | ||
transformOrigin={{ | ||
vertical: 'top', | ||
horizontal: 'left', | ||
}} | ||
> | ||
{menuItems?.map(({ name, path }) => ( | ||
<MenuItem key={name} component={Link} to={path}> | ||
<Typography>{name}</Typography> | ||
</MenuItem> | ||
))} | ||
</Menu> | ||
</> | ||
); | ||
}; | ||
|
||
export default ExtraItemsMenu; |
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,41 @@ | ||
import truncate from 'lodash.truncate'; | ||
|
||
import { Box, SvgIconTypeMap, Typography } from '@mui/material'; | ||
import { OverridableComponent } from '@mui/material/OverridableComponent'; | ||
|
||
import ExtraItemsMenu from './ExtraItemsMenu'; | ||
import { MenuItemType } from './Navigation'; | ||
import { CenterAlignWrapper, ITEM_NAME_MAX_LENGTH, StyledLink } from './utils'; | ||
|
||
export interface ExtraItem { | ||
name: string; | ||
path: string; | ||
Icon?: OverridableComponent<SvgIconTypeMap>; | ||
menuItems?: MenuItemType[]; | ||
} | ||
|
||
const ExtraItemsNavigation = ({ | ||
extraItems, | ||
}: { | ||
extraItems: ExtraItem[]; | ||
}): JSX.Element[] | null => { | ||
return extraItems.map(({ Icon, name, path, menuItems }) => ( | ||
<CenterAlignWrapper> | ||
{/* margin set to -2 as menu list has a default style for text indent | ||
with the same value, So to align menu items with this box menu item */} | ||
<Box display='flex' gap={2} ml={-2}> | ||
{Icon && <Icon />} | ||
<StyledLink to={path}> | ||
<Typography> | ||
{truncate(name, { length: ITEM_NAME_MAX_LENGTH })} | ||
</Typography> | ||
</StyledLink> | ||
</Box> | ||
{menuItems && menuItems.length > 0 && ( | ||
<ExtraItemsMenu menuItems={menuItems} name={name} /> | ||
)} | ||
</CenterAlignWrapper> | ||
)); | ||
}; | ||
|
||
export default ExtraItemsNavigation; |
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
Oops, something went wrong.