-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add Collpase button to item menu
- Loading branch information
Showing
6 changed files
with
106 additions
and
12 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,87 @@ | ||
import React, { useState } from 'react'; | ||
import PropTypes from 'prop-types'; | ||
import IconButton from '@material-ui/core/IconButton'; | ||
import ExpandMoreIcon from '@material-ui/icons/ExpandMore'; | ||
import ExpandLessIcon from '@material-ui/icons/ExpandLess'; | ||
import { useTranslation } from 'react-i18next'; | ||
import Tooltip from '@material-ui/core/Tooltip'; | ||
import MenuItem from '@material-ui/core/MenuItem'; | ||
import ListItemIcon from '@material-ui/core/ListItemIcon'; | ||
import { MUTATION_KEYS } from '@graasp/query-client'; | ||
import { useMutation } from '../../config/queryClient'; | ||
import { COLLAPSE_ITEM_BUTTON_CLASS } from '../../config/selectors'; | ||
import { BUTTON_TYPES } from '../../config/constants'; | ||
|
||
const CollapseButton = ({ item, type, onClick }) => { | ||
const { t } = useTranslation(); | ||
|
||
const editItem = useMutation(MUTATION_KEYS.EDIT_ITEM); | ||
const [isCollapsible, setIsCollapsible] = useState(item?.settings?.isCollapsible); | ||
|
||
const handleCollapse = () => { | ||
setIsCollapsible(!isCollapsible); | ||
|
||
editItem.mutate({ | ||
id: item.id, | ||
name: item.name, | ||
settings: { | ||
isCollapsible: !isCollapsible, | ||
}, | ||
}); | ||
onClick?.(); | ||
}; | ||
|
||
const icon = isCollapsible ? <ExpandLessIcon /> : <ExpandMoreIcon />; | ||
const text = isCollapsible ? t('Uncollapse') : t('Collapse'); | ||
|
||
switch (type) { | ||
case BUTTON_TYPES.MENU_ITEM: | ||
return ( | ||
<MenuItem | ||
key={text} | ||
onClick={handleCollapse} | ||
className={COLLAPSE_ITEM_BUTTON_CLASS} | ||
> | ||
<ListItemIcon>{icon}</ListItemIcon> | ||
{text} | ||
</MenuItem> | ||
); | ||
default: | ||
case BUTTON_TYPES.ICON_BUTTON: | ||
return ( | ||
<Tooltip title={text}> | ||
<IconButton | ||
aria-label={text} | ||
className={COLLAPSE_ITEM_BUTTON_CLASS} | ||
onClick={handleCollapse} | ||
> | ||
{icon} | ||
</IconButton> | ||
</Tooltip> | ||
); | ||
} | ||
}; | ||
|
||
CollapseButton.propTypes = { | ||
item: PropTypes.shape({ | ||
id: PropTypes.string.isRequired, | ||
name: PropTypes.string, | ||
settings: PropTypes.shape({ | ||
isCollapsible: PropTypes.bool, | ||
}), | ||
}), | ||
type: PropTypes.string, | ||
onClick: PropTypes.func, | ||
}; | ||
|
||
CollapseButton.defaultProps = { | ||
item: { | ||
settings: { | ||
isCollapsible: false, | ||
}, | ||
}, | ||
type: BUTTON_TYPES.ICON_BUTTON, | ||
onClick: null, | ||
}; | ||
|
||
export default CollapseButton; |
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