UNPKG

@spaced-out/ui-design-system

Version:
191 lines (189 loc) 7.46 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.InContextAlert = exports.AlertIcon = void 0; var React = _interopRequireWildcard(require("react")); var _common = require("../../types/common"); var _typography = require("../../types/typography"); var _classify = require("../../utils/classify"); var _qa = require("../../utils/qa"); var _Icon = require("../Icon"); var _Link = require("../Link"); var _Text = require("../Text"); var _Truncate = require("../Truncate"); var _InContextAlertModule = _interopRequireDefault(require("./InContextAlert.module.css")); var _jsxRuntime = require("react/jsx-runtime"); function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; } function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function (e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != typeof e && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (const t in e) "default" !== t && {}.hasOwnProperty.call(e, t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, t)) && (i.get || i.set) ? o(f, t, i) : f[t] = e[t]); return f; })(e, t); } const AlertIcon = _ref => { let { semantic, leftIconName, leftIconType, iconClassName, testId } = _ref; switch (semantic) { case _common.ALERT_SEMANTIC.neutral: return /*#__PURE__*/(0, _jsxRuntime.jsx)(_Icon.Icon, { color: _typography.TEXT_COLORS.neutral, name: leftIconName ? leftIconName : 'face-smile', size: "small", type: leftIconType, className: iconClassName, testId: (0, _qa.generateTestId)({ base: testId, slot: 'icon' }) }); case _common.ALERT_SEMANTIC.success: return /*#__PURE__*/(0, _jsxRuntime.jsx)(_Icon.Icon, { name: leftIconName ? leftIconName : 'circle-check', size: "small", color: _typography.TEXT_COLORS.success, type: leftIconType, className: iconClassName, testId: (0, _qa.generateTestId)({ base: testId, slot: 'icon' }) }); case _common.ALERT_SEMANTIC.information: return /*#__PURE__*/(0, _jsxRuntime.jsx)(_Icon.Icon, { name: leftIconName ? leftIconName : 'circle-info', size: "small", color: _typography.TEXT_COLORS.information, type: leftIconType, className: iconClassName, testId: (0, _qa.generateTestId)({ base: testId, slot: 'icon' }) }); case _common.ALERT_SEMANTIC.warning: return /*#__PURE__*/(0, _jsxRuntime.jsx)(_Icon.Icon, { name: leftIconName ? leftIconName : 'circle-exclamation', size: "small", color: _typography.TEXT_COLORS.warning, type: leftIconType, className: iconClassName, testId: (0, _qa.generateTestId)({ base: testId, slot: 'icon' }) }); case _common.ALERT_SEMANTIC.danger: return /*#__PURE__*/(0, _jsxRuntime.jsx)(_Icon.Icon, { name: leftIconName ? leftIconName : 'shield-exclamation', size: "small", color: _typography.TEXT_COLORS.danger, type: leftIconType, className: iconClassName, testId: (0, _qa.generateTestId)({ base: testId, slot: 'icon' }) }); default: return /*#__PURE__*/(0, _jsxRuntime.jsx)(_Icon.Icon, { color: _typography.TEXT_COLORS.neutral, name: leftIconName ? leftIconName : 'face-smile', size: "small", type: leftIconType, className: iconClassName, testId: (0, _qa.generateTestId)({ base: testId, slot: 'icon' }) }); } }; exports.AlertIcon = AlertIcon; const InContextAlert = exports.InContextAlert = /*#__PURE__*/React.forwardRef((props, ref) => { const { classNames, semantic = _common.ALERT_SEMANTIC.neutral, dismissable, children, leftIconName = '', actionText = '', actionTextColor = _typography.TEXT_COLORS.primary, disableAction = false, onAction, leftIconType, testId } = props; // Type narrowing to safely access properties only when they exist const selfDismiss = dismissable === true && 'selfDismiss' in props ? props.selfDismiss : false; const onCloseClick = dismissable === true && 'onCloseClick' in props ? props.onCloseClick : undefined; const [dismissed, setDismissed] = React.useState(false); const closeClickHandler = e => { onCloseClick?.(e); if (selfDismiss) { setDismissed(true); } }; return /*#__PURE__*/(0, _jsxRuntime.jsx)(_jsxRuntime.Fragment, { children: !dismissed && /*#__PURE__*/(0, _jsxRuntime.jsxs)("div", { className: (0, _classify.classify)(_InContextAlertModule.default.alertContainer, { [_InContextAlertModule.default.neutral]: semantic === _common.ALERT_SEMANTIC.neutral, [_InContextAlertModule.default.success]: semantic === _common.ALERT_SEMANTIC.success, [_InContextAlertModule.default.information]: semantic === _common.ALERT_SEMANTIC.information, [_InContextAlertModule.default.warning]: semantic === _common.ALERT_SEMANTIC.warning, [_InContextAlertModule.default.danger]: semantic === _common.ALERT_SEMANTIC.danger }, classNames?.wrapper), ref: ref, "data-testid": (0, _qa.generateTestId)({ base: testId, slot: 'wrapper' }), children: [/*#__PURE__*/(0, _jsxRuntime.jsx)(AlertIcon, { semantic: semantic, leftIconName: leftIconName, leftIconType: leftIconType, iconClassName: classNames?.icon, testId: testId }), React.Children.count(children) > 0 && /*#__PURE__*/(0, _jsxRuntime.jsx)(_Text.SubTitleExtraSmall, { className: (0, _classify.classify)(classNames?.alertText), children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_Truncate.Truncate, { line: 2, wordBreak: "initial", testId: (0, _qa.generateTestId)({ base: testId, slot: 'text' }), children: children }) }), !!(actionText || dismissable) && /*#__PURE__*/(0, _jsxRuntime.jsxs)("div", { className: (0, _classify.classify)(_InContextAlertModule.default.actionContainer, classNames?.actionContainer), "data-testid": (0, _qa.generateTestId)({ base: testId, slot: 'actions' }), children: [!!actionText && /*#__PURE__*/(0, _jsxRuntime.jsx)(_Link.Link, { onClick: onAction, color: actionTextColor, testId: (0, _qa.generateTestId)({ base: testId, slot: 'action-link' }), disabled: disableAction, children: actionText }), !!dismissable && /*#__PURE__*/(0, _jsxRuntime.jsx)(_Icon.ClickableIcon, { color: _typography.TEXT_COLORS.primary, ariaLabel: "close", name: "close", size: "small", type: "regular", onClick: closeClickHandler, className: _InContextAlertModule.default.closeIcon, testId: (0, _qa.generateTestId)({ base: testId, slot: 'close-button' }) })] })] }) }); });