|
|
|
|
|
import * as React from "react"; |
|
|
import PropTypes from "prop-types"; |
|
|
import Icon from "@mui/material/Icon"; |
|
|
import IconButton from "@mui/material/IconButton"; |
|
|
import Tooltip from "@mui/material/Tooltip"; |
|
|
|
|
|
|
|
|
class MTableAction extends React.Component { |
|
|
render() { |
|
|
let action = this.props.action; |
|
|
|
|
|
if (typeof action === "function") { |
|
|
action = action(this.props.data); |
|
|
if (!action) { |
|
|
return null; |
|
|
} |
|
|
} |
|
|
|
|
|
if (action.action) { |
|
|
action = action.action(this.props.data); |
|
|
if (!action) { |
|
|
return null; |
|
|
} |
|
|
} |
|
|
|
|
|
if (action.hidden) { |
|
|
return null; |
|
|
} |
|
|
|
|
|
const disabled = action.disabled || this.props.disabled; |
|
|
|
|
|
const handleOnClick = (event) => { |
|
|
if (action.onClick) { |
|
|
action.onClick(event, this.props.data); |
|
|
event.stopPropagation(); |
|
|
} |
|
|
}; |
|
|
|
|
|
const icon = |
|
|
typeof action.icon === "string" ? ( |
|
|
<Icon {...action.iconProps}>{action.icon}</Icon> |
|
|
) : typeof action.icon === "function" ? ( |
|
|
action.icon({ ...action.iconProps, disabled: disabled }) |
|
|
) : ( |
|
|
<action.icon /> |
|
|
); |
|
|
|
|
|
const button = ( |
|
|
<IconButton |
|
|
size={this.props.size} |
|
|
color="inherit" |
|
|
disabled={disabled} |
|
|
onClick={handleOnClick} |
|
|
> |
|
|
{icon} |
|
|
</IconButton> |
|
|
); |
|
|
|
|
|
if (action.tooltip) { |
|
|
|
|
|
|
|
|
return disabled ? ( |
|
|
<Tooltip title={action.tooltip}> |
|
|
<span>{button}</span> |
|
|
</Tooltip> |
|
|
) : ( |
|
|
<Tooltip title={action.tooltip}>{button}</Tooltip> |
|
|
); |
|
|
} else { |
|
|
return button; |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
MTableAction.defaultProps = { |
|
|
action: {}, |
|
|
data: {}, |
|
|
}; |
|
|
|
|
|
MTableAction.propTypes = { |
|
|
action: PropTypes.oneOfType([PropTypes.func, PropTypes.object]).isRequired, |
|
|
data: PropTypes.oneOfType([ |
|
|
PropTypes.object, |
|
|
PropTypes.arrayOf(PropTypes.object), |
|
|
]), |
|
|
disabled: PropTypes.bool, |
|
|
size: PropTypes.string, |
|
|
}; |
|
|
|
|
|
export default MTableAction; |
|
|
|