UNPKG

5.32 kBJavaScriptView Raw
1var _excluded = ["className", "closeClassName", "closeAriaLabel", "cssModule", "tag", "color", "isOpen", "toggle", "children", "transition", "fade", "innerRef"];
2function _extends() { _extends = Object.assign ? Object.assign.bind() : function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
3function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
4function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
5function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
6function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }
7function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }
8import React from 'react';
9import PropTypes from 'prop-types';
10import classNames from 'classnames';
11import { mapToCssModules, tagPropType } from './utils';
12import Fade from './Fade';
13var propTypes = {
14 /** Pass children so this component can wrap the child elements */
15 children: PropTypes.node,
16 /** Add custom class */
17 className: PropTypes.string,
18 /** Add custom class for close button */
19 closeClassName: PropTypes.string,
20 /** Aria label for close button */
21 closeAriaLabel: PropTypes.string,
22 /** Change color of alert */
23 color: PropTypes.string,
24 /** Change existing className with a new className */
25 cssModule: PropTypes.object,
26 /** Toggle fade animation */
27 fade: PropTypes.bool,
28 innerRef: PropTypes.oneOfType([PropTypes.object, PropTypes.string, PropTypes.func]),
29 /** Control visibility state of Alert */
30 isOpen: PropTypes.bool,
31 /** Set a custom element for this component */
32 tag: tagPropType,
33 /** Function to toggle visibility */
34 toggle: PropTypes.func,
35 /** Props to be passed to `Fade` to modify transition */
36 transition: PropTypes.shape(Fade.propTypes)
37};
38function Alert(props) {
39 var className = props.className,
40 closeClassName = props.closeClassName,
41 _props$closeAriaLabel = props.closeAriaLabel,
42 closeAriaLabel = _props$closeAriaLabel === void 0 ? 'Close' : _props$closeAriaLabel,
43 cssModule = props.cssModule,
44 _props$tag = props.tag,
45 Tag = _props$tag === void 0 ? 'div' : _props$tag,
46 _props$color = props.color,
47 color = _props$color === void 0 ? 'success' : _props$color,
48 _props$isOpen = props.isOpen,
49 isOpen = _props$isOpen === void 0 ? true : _props$isOpen,
50 toggle = props.toggle,
51 children = props.children,
52 _props$transition = props.transition,
53 transition = _props$transition === void 0 ? _objectSpread(_objectSpread({}, Fade.defaultProps), {}, {
54 unmountOnExit: true
55 }) : _props$transition,
56 _props$fade = props.fade,
57 fade = _props$fade === void 0 ? true : _props$fade,
58 innerRef = props.innerRef,
59 attributes = _objectWithoutProperties(props, _excluded);
60 var classes = mapToCssModules(classNames(className, 'alert', "alert-".concat(color), {
61 'alert-dismissible': toggle
62 }), cssModule);
63 var closeClasses = mapToCssModules(classNames('btn-close', closeClassName), cssModule);
64 var alertTransition = _objectSpread(_objectSpread(_objectSpread({}, Fade.defaultProps), transition), {}, {
65 baseClass: fade ? transition.baseClass : '',
66 timeout: fade ? transition.timeout : 0
67 });
68 return /*#__PURE__*/React.createElement(Fade, _extends({}, attributes, alertTransition, {
69 tag: Tag,
70 className: classes,
71 "in": isOpen,
72 role: "alert",
73 innerRef: innerRef
74 }), toggle ? /*#__PURE__*/React.createElement("button", {
75 type: "button",
76 className: closeClasses,
77 "aria-label": closeAriaLabel,
78 onClick: toggle
79 }) : null, children);
80}
81Alert.propTypes = propTypes;
82export default Alert;
\No newline at end of file