diff --git a/example/src/components/UConfirmationButtonExample.js b/example/src/components/UConfirmationButtonExample.js index 1be1ccd0d..0bc95930b 100644 --- a/example/src/components/UConfirmationButtonExample.js +++ b/example/src/components/UConfirmationButtonExample.js @@ -1,12 +1,36 @@ -import React from 'react' +import React, { useState } from 'react' import { UConfirmationButton } from 'unicef-material-ui' -import { Typography, Grid } from '@material-ui/core' +import { + Typography, + Grid, + IconButton, + Menu, + MenuItem, + Link, +} from '@material-ui/core' import LinkOffIcon from '@material-ui/icons/LinkOff' +import MoreVertIcon from '@material-ui/icons/MoreVert' + export default function UConfirmationButtonExample() { - const itemId = 1 + const [anchorEl, setAnchorEl] = useState({ menu: null, popup: null }) + const openMenuVariant = Boolean(anchorEl.menu) + const openPopupVariant = Boolean(anchorEl.popup) + function handleDelete(event, id) { console.log(id) } + function handleClick(event, type) { + setAnchorEl({ + ...anchorEl, + [type]: event.currentTarget, + }) + } + function handleClose(type) { + setAnchorEl({ + ...anchorEl, + [type]: null, + }) + } return ( @@ -15,46 +39,154 @@ export default function UConfirmationButtonExample() { Confirmation button - - Popup variant - + + Confirm popup variant - - Icon variant - + + + Icon + + + + Menu item + handleClick(e, 'popup')} + > + + + handleClose('popup')} + disableEnforceFocus={true} + > + handleClose('popup')}> + + Edit + + + handleClose('popup')}> + + View + + + + + + + Button + + + + Custom icon + } + /> + - - Menu item variant - + + Confirm menu variant - - - Custom icon and confirm text - - } - /> + + + Icon + + + + Menu item + handleClick(e, 'menu')} + > + + + handleClose('menu')} + disableEnforceFocus={true} + > + handleClose('menu')}> + + Edit + + + handleClose('menu')}> + + View + + + + + + + Button + + + + Custom icon + } + /> + ) diff --git a/package.json b/package.json index 3c2b14d25..3ded84618 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@unicef/material-ui", - "version": "0.11.5", + "version": "0.11.6", "description": "UNICEF theme and components of material-ui for react", "main": "index.js", "files": [ diff --git a/src/components/UConfirmationButton/UConfirmationButton.js b/src/components/UConfirmationButton/UConfirmationButton.js index fa26e9068..b2be22f57 100644 --- a/src/components/UConfirmationButton/UConfirmationButton.js +++ b/src/components/UConfirmationButton/UConfirmationButton.js @@ -10,6 +10,7 @@ import { DialogContent, Typography, DialogActions, + Box, } from '@material-ui/core' import DeleteOutlinedIcon from '@material-ui/icons/DeleteOutlined' import UButton from '../UButton' @@ -27,9 +28,14 @@ const useStyles = makeStyles(theme => ({ })) const CONTROL_VARIANTS = { - popup: 'popup', menuItem: 'menuItem', icon: 'icon', + button: 'button', +} + +const CONFIRMATION_VARIANTS = { + menu: 'menu', + popup: 'popup', } /** @@ -49,13 +55,19 @@ export default function UConfirmationButton(props) { confirmText, confirmActionText, cancelText, + buttonVariant, + confirmVariant, } = props const [deleteAnchorEl, setDeleteAnchorEl] = useState(null) const [openDialog, setOpenDialog] = useState(false) const handleConfirm = e => { onConfirm && onConfirm(e, id) - if (variant !== CONTROL_VARIANTS.popup) handleDeleteOptionClicked(e) + if (confirmVariant === CONFIRMATION_VARIANTS.popup) { + setOpenDialog(false) + } else { + handleDeleteOptionClicked(e) + } } const handleDeleteOptionClicked = e => { @@ -63,8 +75,11 @@ export default function UConfirmationButton(props) { } const handleClick = e => { - if (variant !== CONTROL_VARIANTS.popup) setDeleteAnchorEl(e.currentTarget) - else setOpenDialog(true) + if (confirmVariant === CONFIRMATION_VARIANTS.popup) { + setOpenDialog(true) + } else { + setDeleteAnchorEl(e.currentTarget) + } } const handleCancelPopup = e => { @@ -76,8 +91,20 @@ export default function UConfirmationButton(props) { {variant === CONTROL_VARIANTS.menuItem ? ( {icon} - {buttonText} + + {buttonText} + + ) : variant === CONTROL_VARIANTS.button ? ( + + {buttonText} + ) : ( )} - {variant === CONTROL_VARIANTS.popup ? ( + {confirmVariant === CONFIRMATION_VARIANTS.popup ? ( {confirmText || ''} @@ -131,11 +158,16 @@ UConfirmationButton.propTypes = { buttonText: PropTypes.string, /** trigger confirmation function */ onConfirm: PropTypes.func.isRequired, - /** variant: menu or icon */ + /** variant: menuItem or icon or button */ variant: PropTypes.oneOf([ - CONTROL_VARIANTS.popup, CONTROL_VARIANTS.menuItem, CONTROL_VARIANTS.icon, + CONTROL_VARIANTS.button, + ]), + /** Confirm variant: popup or menu */ + confirmVariant: PropTypes.oneOf([ + CONFIRMATION_VARIANTS.popup, + CONFIRMATION_VARIANTS.menu, ]), /** if the variant is menuitem, this prop make sure the item enable or not */ enabled: PropTypes.bool, @@ -147,6 +179,8 @@ UConfirmationButton.propTypes = { confirmActionText: PropTypes.string, /** custom icon */ icon: PropTypes.element, + /** Button variant applied to menuItem button */ + buttonVariant: PropTypes.string, } UConfirmationButton.defaultProps = { @@ -157,4 +191,6 @@ UConfirmationButton.defaultProps = { confirmActionText: 'Yes, delete', cancelText: 'No', icon: , + buttonVariant: 'text', + confirmVariant: 'menu', } diff --git a/src/components/UConfirmationButton/UConfirmationButton.md b/src/components/UConfirmationButton/UConfirmationButton.md index aa42aea94..018a7a284 100644 --- a/src/components/UConfirmationButton/UConfirmationButton.md +++ b/src/components/UConfirmationButton/UConfirmationButton.md @@ -1,43 +1,179 @@ Delete button with examples: ```jsx -import { Typography, Grid } from '@material-ui/core' +import React, { useState } from 'react' +import { Typography, Grid, Menu, MenuItem, Link, IconButton } from '@material-ui/core' import LinkOffIcon from '@material-ui/icons/LinkOff' +import MoreVertIcon from '@material-ui/icons/MoreVert' - const itemId = 1 + const [anchorEl, setAnchorEl] = useState({ menu: null, popup: null }) + const openMenuVariant = Boolean(anchorEl.menu) + const openPopupVariant = Boolean(anchorEl.popup) function handleDelete(event, id) { console.log(id) } + function handleClick(event, type) { + setAnchorEl({ + ...anchorEl, + [type]: event.currentTarget, + }) + } + function handleClose(type) { + setAnchorEl({ + ...anchorEl, + [type]: null, + }) + } - - Icon variant - + + Confirm popup variant + + + + Icon + + + + Menu item + handleClick(e, 'popup')} + > + + + handleClose('popup')} + disableEnforceFocus={true} + > + handleClose('popup')}> + + Edit + + + handleClose('popup')}> + + View + + + + + + + Button + + + + Custom icon + } + /> + - - Menu item variant - + + Confirm menu variant - - Custom icon and confirm text - } - /> + + + Icon + + + + Menu item + handleClick(e, 'menu')} + > + + + handleClose('menu')} + disableEnforceFocus={true} + > + handleClose('menu')}> + + Edit + + + handleClose('menu')}> + + View + + + + + + + Button + + + + Custom icon + } + /> +