wix-style-react
Version:
207 lines (174 loc) • 7.3 kB
JavaScript
import _extends from "@babel/runtime/helpers/extends";
import _objectWithoutProperties from "@babel/runtime/helpers/objectWithoutProperties";
import _classCallCheck from "@babel/runtime/helpers/classCallCheck";
import _createClass from "@babel/runtime/helpers/createClass";
import _inherits from "@babel/runtime/helpers/inherits";
import _possibleConstructorReturn from "@babel/runtime/helpers/possibleConstructorReturn";
import _getPrototypeOf from "@babel/runtime/helpers/getPrototypeOf";
import _defineProperty from "@babel/runtime/helpers/defineProperty";
var _excluded = ["label"],
_excluded2 = ["label"];
function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; }
function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } }
import React from 'react';
import PropTypes from 'prop-types';
import isEmpty from 'lodash/isEmpty';
import Text from '../Text';
import TextButton from '../TextButton';
import Button from '../Button';
import CloseButton from '../CloseButton/CloseButton';
import { TYPES, dataHooks } from './constants';
import { st, classes } from './FloatingNotification.st.css';
var buttonPropTypes = PropTypes.shape({
label: PropTypes.node,
as: PropTypes.node,
href: PropTypes.string,
onClick: PropTypes.func
});
/**
* Displays simple and temporary messages or destructive events
*/
var FloatingNotification = /*#__PURE__*/function (_React$PureComponent) {
_inherits(FloatingNotification, _React$PureComponent);
var _super = _createSuper(FloatingNotification);
function FloatingNotification() {
_classCallCheck(this, FloatingNotification);
return _super.apply(this, arguments);
}
_createClass(FloatingNotification, [{
key: "render",
value: function render() {
var _this$props = this.props,
type = _this$props.type,
className = _this$props.className,
dataHook = _this$props.dataHook,
width = _this$props.width,
fullWidth = _this$props.fullWidth;
var icon = this._getIcon();
var content = this._getContent();
var textButton = this._getTextButton();
var button = this._getButton();
var close = this._getClose();
var style = {
width: width
};
width && (style.maxWidth = width);
return /*#__PURE__*/React.createElement("div", {
"data-hook": dataHook,
className: st(classes.root, {
fullWidth: fullWidth,
type: type
}, className),
style: style
}, icon, content, textButton, button, /*#__PURE__*/React.createElement("div", {
className: classes.gap
}), close);
}
}, {
key: "_getIcon",
value: function _getIcon() {
var _this$props2 = this.props,
prefixIcon = _this$props2.prefixIcon,
type = _this$props2.type;
return prefixIcon ? /*#__PURE__*/React.createElement("div", {
className: st(classes.icon, {
type: type
})
}, prefixIcon) : null;
}
}, {
key: "_getContent",
value: function _getContent() {
var _this$props3 = this.props,
text = _this$props3.text,
type = _this$props3.type;
return /*#__PURE__*/React.createElement(Text, {
size: "small",
ellipsis: true,
light: type === TYPES.dark,
dataHook: dataHooks.notificationText,
className: classes.text
}, text);
}
}, {
key: "_getTextButton",
value: function _getTextButton() {
var _this$props4 = this.props,
textButtonProps = _this$props4.textButtonProps,
type = _this$props4.type;
var label = textButtonProps.label,
rest = _objectWithoutProperties(textButtonProps, _excluded);
return !isEmpty(textButtonProps) ? /*#__PURE__*/React.createElement(TextButton, _extends({}, rest, {
underline: "always",
skin: type !== TYPES.dark ? 'dark' : 'light',
size: "small",
className: classes.textButton,
dataHook: dataHooks.textButton
}), label) : null;
}
}, {
key: "_getButton",
value: function _getButton() {
var _this$props5 = this.props,
buttonProps = _this$props5.buttonProps,
type = _this$props5.type;
var label = buttonProps.label,
rest = _objectWithoutProperties(buttonProps, _excluded2);
return !isEmpty(buttonProps) ? /*#__PURE__*/React.createElement(Button, _extends({}, rest, {
className: classes.button,
size: "tiny",
priority: type !== TYPES.dark ? 'secondary' : 'primary',
skin: type !== TYPES.dark ? 'dark' : 'standard',
dataHook: dataHooks.button
}), label) : null;
}
}, {
key: "_getClose",
value: function _getClose() {
var _this$props6 = this.props,
showCloseButton = _this$props6.showCloseButton,
onClose = _this$props6.onClose,
type = _this$props6.type;
return showCloseButton ? /*#__PURE__*/React.createElement(CloseButton, {
size: "medium",
skin: type !== TYPES.dark ? 'dark' : 'light',
className: classes.close,
dataHook: dataHooks.closeButton,
onClick: onClose
}) : null;
}
}]);
return FloatingNotification;
}(React.PureComponent);
_defineProperty(FloatingNotification, "displayName", 'FloatingNotification');
_defineProperty(FloatingNotification, "propTypes", {
/** Applied as data-hook HTML attribute that can be used in the tests */
dataHook: PropTypes.string,
/** A css class to be applied to the component's root element */
className: PropTypes.string,
/** the type of notification */
type: PropTypes.oneOf(['standard', 'success', 'destructive', 'warning', 'premium', 'preview', 'dark']),
/** decides if to show the close button */
showCloseButton: PropTypes.bool,
/** close button on click handler */
onClose: PropTypes.func,
/** props to pass to textButton - renders the TextButton when not empty*/
textButtonProps: buttonPropTypes,
/** props to pass to button - renders the Button when not empty */
buttonProps: buttonPropTypes,
/** An icon element to appear before content */
prefixIcon: PropTypes.element,
/** The text content of the notification */
text: PropTypes.node,
/** The width of the content container of the notification */
width: PropTypes.string,
/** Is notification full width (removes left and right border, border radius, more height and bigger paddings) */
fullWidth: PropTypes.bool
});
_defineProperty(FloatingNotification, "defaultProps", {
type: 'standard',
buttonProps: {},
textButtonProps: {},
showCloseButton: true
});
export default FloatingNotification;