UNPKG

9.56 kBJavaScriptView Raw
1import _objectWithoutPropertiesLoose from "@babel/runtime/helpers/esm/objectWithoutPropertiesLoose";
2import _extends from "@babel/runtime/helpers/esm/extends";
3import * as React from 'react';
4import PropTypes from 'prop-types';
5import clsx from 'clsx';
6import { withStyles, lighten, darken } from '@material-ui/core/styles';
7import Paper from '@material-ui/core/Paper';
8import SuccessOutlinedIcon from '../internal/svg-icons/SuccessOutlined';
9import ReportProblemOutlinedIcon from '../internal/svg-icons/ReportProblemOutlined';
10import ErrorOutlineIcon from '../internal/svg-icons/ErrorOutline';
11import InfoOutlinedIcon from '../internal/svg-icons/InfoOutlined';
12import CloseIcon from '../internal/svg-icons/Close';
13import IconButton from '@material-ui/core/IconButton';
14import { capitalize } from '@material-ui/core/utils';
15export const styles = theme => {
16 const getColor = theme.palette.type === 'light' ? darken : lighten;
17 const getBackgroundColor = theme.palette.type === 'light' ? lighten : darken;
18 return {
19 /* Styles applied to the root element. */
20 root: _extends({}, theme.typography.body2, {
21 borderRadius: theme.shape.borderRadius,
22 backgroundColor: 'transparent',
23 display: 'flex',
24 padding: '6px 16px'
25 }),
26
27 /* Styles applied to the root element if `variant="standard"` and `color="success"`. */
28 standardSuccess: {
29 color: getColor(theme.palette.success.main, 0.6),
30 backgroundColor: getBackgroundColor(theme.palette.success.main, 0.9),
31 '& $icon': {
32 color: theme.palette.success.main
33 }
34 },
35
36 /* Styles applied to the root element if `variant="standard"` and `color="info"`. */
37 standardInfo: {
38 color: getColor(theme.palette.info.main, 0.6),
39 backgroundColor: getBackgroundColor(theme.palette.info.main, 0.9),
40 '& $icon': {
41 color: theme.palette.info.main
42 }
43 },
44
45 /* Styles applied to the root element if `variant="standard"` and `color="warning"`. */
46 standardWarning: {
47 color: getColor(theme.palette.warning.main, 0.6),
48 backgroundColor: getBackgroundColor(theme.palette.warning.main, 0.9),
49 '& $icon': {
50 color: theme.palette.warning.main
51 }
52 },
53
54 /* Styles applied to the root element if `variant="standard"` and `color="error"`. */
55 standardError: {
56 color: getColor(theme.palette.error.main, 0.6),
57 backgroundColor: getBackgroundColor(theme.palette.error.main, 0.9),
58 '& $icon': {
59 color: theme.palette.error.main
60 }
61 },
62
63 /* Styles applied to the root element if `variant="outlined"` and `color="success"`. */
64 outlinedSuccess: {
65 color: getColor(theme.palette.success.main, 0.6),
66 border: `1px solid ${theme.palette.success.main}`,
67 '& $icon': {
68 color: theme.palette.success.main
69 }
70 },
71
72 /* Styles applied to the root element if `variant="outlined"` and `color="info"`. */
73 outlinedInfo: {
74 color: getColor(theme.palette.info.main, 0.6),
75 border: `1px solid ${theme.palette.info.main}`,
76 '& $icon': {
77 color: theme.palette.info.main
78 }
79 },
80
81 /* Styles applied to the root element if `variant="outlined"` and `color="warning"`. */
82 outlinedWarning: {
83 color: getColor(theme.palette.warning.main, 0.6),
84 border: `1px solid ${theme.palette.warning.main}`,
85 '& $icon': {
86 color: theme.palette.warning.main
87 }
88 },
89
90 /* Styles applied to the root element if `variant="outlined"` and `color="error"`. */
91 outlinedError: {
92 color: getColor(theme.palette.error.main, 0.6),
93 border: `1px solid ${theme.palette.error.main}`,
94 '& $icon': {
95 color: theme.palette.error.main
96 }
97 },
98
99 /* Styles applied to the root element if `variant="filled"` and `color="success"`. */
100 filledSuccess: {
101 color: '#fff',
102 fontWeight: theme.typography.fontWeightMedium,
103 backgroundColor: theme.palette.success.main
104 },
105
106 /* Styles applied to the root element if `variant="filled"` and `color="info"`. */
107 filledInfo: {
108 color: '#fff',
109 fontWeight: theme.typography.fontWeightMedium,
110 backgroundColor: theme.palette.info.main
111 },
112
113 /* Styles applied to the root element if `variant="filled"` and `color="warning"`. */
114 filledWarning: {
115 color: '#fff',
116 fontWeight: theme.typography.fontWeightMedium,
117 backgroundColor: theme.palette.warning.main
118 },
119
120 /* Styles applied to the root element if `variant="filled"` and `color="error"`. */
121 filledError: {
122 color: '#fff',
123 fontWeight: theme.typography.fontWeightMedium,
124 backgroundColor: theme.palette.error.main
125 },
126
127 /* Styles applied to the icon wrapper element. */
128 icon: {
129 marginRight: 12,
130 padding: '7px 0',
131 display: 'flex',
132 fontSize: 22,
133 opacity: 0.9
134 },
135
136 /* Styles applied to the message wrapper element. */
137 message: {
138 padding: '8px 0'
139 },
140
141 /* Styles applied to the action wrapper element if `action` is provided. */
142 action: {
143 display: 'flex',
144 alignItems: 'center',
145 marginLeft: 'auto',
146 paddingLeft: 16,
147 marginRight: -8
148 }
149 };
150};
151const defaultIconMapping = {
152 success: /*#__PURE__*/React.createElement(SuccessOutlinedIcon, {
153 fontSize: "inherit"
154 }),
155 warning: /*#__PURE__*/React.createElement(ReportProblemOutlinedIcon, {
156 fontSize: "inherit"
157 }),
158 error: /*#__PURE__*/React.createElement(ErrorOutlineIcon, {
159 fontSize: "inherit"
160 }),
161 info: /*#__PURE__*/React.createElement(InfoOutlinedIcon, {
162 fontSize: "inherit"
163 })
164};
165
166var _ref = /*#__PURE__*/React.createElement(CloseIcon, {
167 fontSize: "small"
168});
169
170const Alert = /*#__PURE__*/React.forwardRef(function Alert(props, ref) {
171 const {
172 action,
173 children,
174 classes,
175 className,
176 closeText = 'Close',
177 color,
178 icon,
179 iconMapping = defaultIconMapping,
180 onClose,
181 role = 'alert',
182 severity = 'success',
183 variant = 'standard'
184 } = props,
185 other = _objectWithoutPropertiesLoose(props, ["action", "children", "classes", "className", "closeText", "color", "icon", "iconMapping", "onClose", "role", "severity", "variant"]);
186
187 return /*#__PURE__*/React.createElement(Paper, _extends({
188 role: role,
189 square: true,
190 elevation: 0,
191 className: clsx(classes.root, classes[`${variant}${capitalize(color || severity)}`], className),
192 ref: ref
193 }, other), icon !== false ? /*#__PURE__*/React.createElement("div", {
194 className: classes.icon
195 }, icon || iconMapping[severity] || defaultIconMapping[severity]) : null, /*#__PURE__*/React.createElement("div", {
196 className: classes.message
197 }, children), action != null ? /*#__PURE__*/React.createElement("div", {
198 className: classes.action
199 }, action) : null, action == null && onClose ? /*#__PURE__*/React.createElement("div", {
200 className: classes.action
201 }, /*#__PURE__*/React.createElement(IconButton, {
202 size: "small",
203 "aria-label": closeText,
204 title: closeText,
205 color: "inherit",
206 onClick: onClose
207 }, _ref)) : null);
208});
209process.env.NODE_ENV !== "production" ? Alert.propTypes = {
210 // ----------------------------- Warning --------------------------------
211 // | These PropTypes are generated from the TypeScript type definitions |
212 // | To update them edit the d.ts file and run "yarn proptypes" |
213 // ----------------------------------------------------------------------
214
215 /**
216 * The action to display. It renders after the message, at the end of the alert.
217 */
218 action: PropTypes.node,
219
220 /**
221 * The content of the component.
222 */
223 children: PropTypes.node,
224
225 /**
226 * Override or extend the styles applied to the component.
227 * See [CSS API](#css) below for more details.
228 */
229 classes: PropTypes.object,
230
231 /**
232 * @ignore
233 */
234 className: PropTypes.string,
235
236 /**
237 * Override the default label for the *close popup* icon button.
238 *
239 * For localization purposes, you can use the provided [translations](/guides/localization/).
240 */
241 closeText: PropTypes.string,
242
243 /**
244 * The main color for the alert. Unless provided, the value is taken from the `severity` prop.
245 */
246 color: PropTypes.oneOf(['error', 'info', 'success', 'warning']),
247
248 /**
249 * Override the icon displayed before the children.
250 * Unless provided, the icon is mapped to the value of the `severity` prop.
251 */
252 icon: PropTypes.node,
253
254 /**
255 * The component maps the `severity` prop to a range of different icons,
256 * for instance success to `<SuccessOutlined>`.
257 * If you wish to change this mapping, you can provide your own.
258 * Alternatively, you can use the `icon` prop to override the icon displayed.
259 */
260 iconMapping: PropTypes.shape({
261 error: PropTypes.node,
262 info: PropTypes.node,
263 success: PropTypes.node,
264 warning: PropTypes.node
265 }),
266
267 /**
268 * Callback fired when the component requests to be closed.
269 * When provided and no `action` prop is set, a close icon button is displayed that triggers the callback when clicked.
270 *
271 * @param {object} event The event source of the callback.
272 */
273 onClose: PropTypes.func,
274
275 /**
276 * The ARIA role attribute of the element.
277 */
278 role: PropTypes.string,
279
280 /**
281 * The severity of the alert. This defines the color and icon used.
282 */
283 severity: PropTypes.oneOf(['error', 'info', 'success', 'warning']),
284
285 /**
286 * The variant to use.
287 */
288 variant: PropTypes.oneOf(['filled', 'outlined', 'standard'])
289} : void 0;
290export default withStyles(styles, {
291 name: 'MuiAlert'
292})(Alert);
\No newline at end of file