aws-northstar
Version:
NorthStar Design System
100 lines (96 loc) • 5.62 kB
JavaScript
/** *******************************************************************************************************************
Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License").
You may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License. *
******************************************************************************************************************** */
var __rest = (this && this.__rest) || function (s, e) {
var t = {};
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
t[p] = s[p];
if (s != null && typeof Object.getOwnPropertySymbols === "function")
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
t[p[i]] = s[p[i]];
}
return t;
};
import React, { useCallback, useMemo } from 'react';
import MaterialAlert from '@material-ui/lab/Alert';
import MaterialAlertTitle from '@material-ui/lab/AlertTitle';
import CheckCircleOutlineIcon from '@material-ui/icons/CheckCircleOutline';
import ReportProblemOutlinedIcon from '@material-ui/icons/ReportProblemOutlined';
import CancelOutlinedIcon from '@material-ui/icons/CancelOutlined';
import InfoOutlinedIcon from '@material-ui/icons/InfoOutlined';
import CloseIcon from '@material-ui/icons/Close';
import IconButton from '@material-ui/core/IconButton';
import { makeStyles } from '@material-ui/core/styles';
import clsx from 'clsx';
import Button from '../Button';
import Box from '../../layouts/Box';
const useStyles = makeStyles({
muiAlertMessageOverride: {
justifyContent: 'flex-start',
whiteSpace: 'pre-wrap',
},
childrenContainer: {
flexDirection: 'row',
wordWrap: 'break-word',
},
noBorderRadius: {
borderRadius: 0,
},
});
const iconMapping = {
success: React.createElement(CheckCircleOutlineIcon, { fontSize: "inherit", titleAccess: "success", "aria-label": "success" }),
warning: React.createElement(ReportProblemOutlinedIcon, { fontSize: "inherit", titleAccess: "warning", "aria-label": "warning" }),
error: React.createElement(CancelOutlinedIcon, { fontSize: "inherit", titleAccess: "error", "aria-label": "error" }),
info: React.createElement(InfoOutlinedIcon, { fontSize: "inherit", titleAccess: "info", "aria-label": "info" }),
};
/**
An alert helps the user know when they need to pay attention to a relatively small piece of information or when they need to take a special action.
**/
const Alert = (_a) => {
var { visible = true, onDismiss, onButtonClick, dismissAriaLabel = 'Close', buttonText = '', borderRadius = true } = _a, props = __rest(_a, ["visible", "onDismiss", "onButtonClick", "dismissAriaLabel", "buttonText", "borderRadius"]);
const styles = useStyles({});
const [show, setShow] = React.useState(true);
const handleButtonClick = useCallback((event) => {
event.preventDefault();
onButtonClick === null || onButtonClick === void 0 ? void 0 : onButtonClick();
}, [onButtonClick]);
const actionButton = useMemo(() => React.createElement(React.Fragment, null, buttonText && React.createElement(Button, { onClick: handleButtonClick }, buttonText)), [buttonText, handleButtonClick]);
const closeButton = useMemo(() => (React.createElement(React.Fragment, null,
actionButton,
show && (React.createElement(IconButton, { "aria-label": dismissAriaLabel, onClick: () => {
setShow(false);
onDismiss === null || onDismiss === void 0 ? void 0 : onDismiss();
} },
React.createElement(CloseIcon, null))))), [actionButton, dismissAriaLabel, show, onDismiss, setShow]);
const mapProps = useCallback((alertType, { type = 'info' }) => {
return {
severity: type,
iconMapping,
action: alertType === 'dismissible' ? closeButton : actionButton,
};
}, [closeButton, actionButton]);
const renderAlertBody = useCallback(({ header, children }, materialProps) => (React.createElement(MaterialAlert, Object.assign({ variant: "outlined" }, materialProps, { classes: {
message: styles.muiAlertMessageOverride,
root: clsx({ [styles.noBorderRadius]: !borderRadius }),
} }),
header && (React.createElement("div", { role: "heading", "aria-level": 1 },
React.createElement(MaterialAlertTitle, null, header))),
React.createElement("div", { className: styles.childrenContainer }, children))), [styles, borderRadius]);
const renderAlert = useCallback((_a) => {
var { dismissible } = _a, props = __rest(_a, ["dismissible"]);
return renderAlertBody(props, mapProps(dismissible ? 'dismissible' : 'pinned', props));
}, [renderAlertBody, mapProps]);
const testId = props['data-testid'] || props.type;
return (React.createElement(Box, { "data-testid": testId, width: "100%" }, visible && show && renderAlert(props)));
};
export default Alert;