UNPKG

@carbon/ibm-security

Version:

Carbon for Cloud & Cognitive IBM Security UI components

218 lines (216 loc) 10.5 kB
import _classCallCheck from "@babel/runtime/helpers/classCallCheck"; import _createClass from "@babel/runtime/helpers/createClass"; import _possibleConstructorReturn from "@babel/runtime/helpers/possibleConstructorReturn"; import _getPrototypeOf from "@babel/runtime/helpers/getPrototypeOf"; import _inherits from "@babel/runtime/helpers/inherits"; import _defineProperty from "@babel/runtime/helpers/defineProperty"; function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; } function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; } function _callSuper(t, o, e) { return o = _getPrototypeOf(o), _possibleConstructorReturn(t, _isNativeReflectConstruct() ? Reflect.construct(o, e || [], _getPrototypeOf(t).constructor) : o.apply(t, e)); } function _isNativeReflectConstruct() { try { var t = !Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); } catch (t) {} return (_isNativeReflectConstruct = function _isNativeReflectConstruct() { return !!t; })(); } /** * @file Small tearsheet. * @copyright IBM Security 2019 - 2021 */ import { Close20 } from '@carbon/icons-react'; import classnames from 'classnames'; import PropTypes from 'prop-types'; import React, { PureComponent } from 'react'; import { getComponentNamespace } from '../../../globals/namespace'; import * as defaultLabels from '../../../globals/nls'; import theme from '../../../globals/theme'; import { isNode } from '../../../globals/utils/capabilities'; import Button from '../../Button'; import IconButton from '../../IconButton'; import { LoadingMessage } from '../../Loading'; import Portal, { PORTAL_EVENTS } from '../../Portal'; import ScrollGradient from '../../ScrollGradient/ScrollGradient'; import Transition from '../../Transition'; var namespace = getComponentNamespace('tearsheet--small'); /** * Small tearsheet component. */ var TearsheetSmall = /*#__PURE__*/function (_PureComponent) { function TearsheetSmall() { var _this; _classCallCheck(this, TearsheetSmall); for (var _len = arguments.length, _args = new Array(_len), _key = 0; _key < _len; _key++) { _args[_key] = arguments[_key]; } _this = _callSuper(this, TearsheetSmall, [].concat(_args)); _defineProperty(_this, "state", { isOpen: _this.props.isOpen, loading: _this.props.loading, loadingMessage: _this.props.loadingMessage }); _defineProperty(_this, "renderProp", function (prop) { for (var _len2 = arguments.length, args = new Array(_len2 > 1 ? _len2 - 1 : 0), _key2 = 1; _key2 < _len2; _key2++) { args[_key2 - 1] = arguments[_key2]; } return typeof prop === 'function' ? prop.apply(void 0, args) : prop; }); _defineProperty(_this, "renderBody", function () { var _this$props = _this.props, body = _this$props.body, children = _this$props.children; var loading = _this.state.loading; var elementToRender = body || children; return _this.renderProp(elementToRender, { isLoading: loading }); }); return _this; } _inherits(TearsheetSmall, _PureComponent); return _createClass(TearsheetSmall, [{ key: "render", value: function render() { var _this$props2 = this.props, flush = _this$props2.flush, focusTrap = _this$props2.focusTrap, heading = _this$props2.heading, description = _this$props2.description, closeButton = _this$props2.closeButton, secondaryButton = _this$props2.secondaryButton, primaryButton = _this$props2.primaryButton, rootNode = _this$props2.rootNode, className = _this$props2.className, labels = _this$props2.labels, stopPropagation = _this$props2.stopPropagation, stopPropagationEvents = _this$props2.stopPropagationEvents; var componentLabels = _objectSpread(_objectSpread(_objectSpread({}, defaultLabels.labels), labels), defaultLabels.filterFalsey({ TEARSHEET_SMALL_PRIMARY_BUTTON: primaryButton && primaryButton.label || '', TEARSHEET_SMALL_SECONDARY_BUTTON: secondaryButton && secondaryButton.label || '', TEARSHEET_SMALL_CLOSE_BUTTON: closeButton && closeButton.label || '' })); return /*#__PURE__*/React.createElement(Transition, { className: namespace, component: "span", timeout: 300 }, this.state.isOpen && /*#__PURE__*/React.createElement(Portal, { focusTrap: focusTrap, rootNode: rootNode, stopPropagation: stopPropagation, stopPropagationEvents: stopPropagationEvents }, /*#__PURE__*/React.createElement("section", { className: classnames(namespace, className), "aria-hidden": false }, this.state.loading && /*#__PURE__*/React.createElement(LoadingMessage, { className: "".concat(namespace, "__loading") }, this.state.loadingMessage), /*#__PURE__*/React.createElement("header", { className: "".concat(namespace, "__header") }, /*#__PURE__*/React.createElement("h1", { className: "".concat(namespace, "__title") }, heading), /*#__PURE__*/React.createElement("div", { className: "".concat(namespace, "__description") }, this.renderProp(description))), /*#__PURE__*/React.createElement("section", { className: "".concat(namespace, "__body") }, /*#__PURE__*/React.createElement(ScrollGradient, { className: "".concat(namespace, "__content"), color: theme.ui01 }, /*#__PURE__*/React.createElement("div", { className: classnames(_defineProperty({}, "".concat(namespace, "__scroll-gradient__content"), !flush)) }, this.renderBody()))), /*#__PURE__*/React.createElement("footer", { className: "".concat(namespace, "__footer") }, !secondaryButton.hide && /*#__PURE__*/React.createElement(Button, { className: "".concat(namespace, "__footer__button ").concat(namespace, "__footer__button--secondary"), disabled: secondaryButton.isDisabled || this.state.loading, kind: "secondary", onClick: secondaryButton.onClick, size: "xl" }, componentLabels.TEARSHEET_SMALL_SECONDARY_BUTTON), !primaryButton.hide && /*#__PURE__*/React.createElement(Button, { className: "".concat(namespace, "__footer__button"), disabled: primaryButton.isDisabled || this.state.loading, onClick: primaryButton.onClick, size: "xl" }, componentLabels.TEARSHEET_SMALL_PRIMARY_BUTTON)), !closeButton.isDisabled && /*#__PURE__*/React.createElement(IconButton, { className: "".concat(namespace, "__button--close"), label: componentLabels.TEARSHEET_SMALL_CLOSE_BUTTON, onClick: closeButton.onClick, renderIcon: Close20, size: "lg", tooltip: false, disabled: this.state.loading })))); } }], [{ key: "getDerivedStateFromProps", value: function getDerivedStateFromProps(nextProps, prevState) { if (prevState.isOpen !== nextProps.isOpen) { return { isOpen: nextProps.isOpen }; } if (prevState.loading !== nextProps.loading || prevState.loadingMessage !== nextProps.loadingMessage) { return { loading: nextProps.loading, loadingMessage: nextProps.loadingMessage }; } return null; } }]); }(PureComponent); export var buttonType = PropTypes.shape({ /** @type {func} onClick callback */ onClick: PropTypes.func.isRequired, /** @type {string} The button label. */ label: PropTypes.string, /** @type {bool} Whether the button is disabled. */ isDisabled: PropTypes.bool, /** @type {bool} Whether the button is hidden. */ hide: PropTypes.bool }); TearsheetSmall.propTypes = { /** @type {func|string|element} The function|string|element to render your content. */ body: PropTypes.oneOfType([PropTypes.string, PropTypes.func, PropTypes.element]), /** Provide the content for the `TearsheetSmall` */ children: PropTypes.element, /** @type {string} Extra classes to add. */ className: PropTypes.string, /** @type {Record<object, object>} An object list of close button props. */ closeButton: buttonType.isRequired, /** @type {element|Function|string} The element, function, or string for the description. */ description: PropTypes.oneOfType([PropTypes.element, PropTypes.func, PropTypes.string]), /** Specify whether the content's padding is flush */ flush: PropTypes.bool, /** @type {boolean} Focus trap. */ focusTrap: PropTypes.bool, /** @type {string} The view title. */ heading: PropTypes.string, /** @type {boolean} The open state. */ isOpen: PropTypes.bool, /** @type {object} Labels for the TearsheetSmall buttons */ labels: defaultLabels.propType, /** @type {bool} The toggle to determine whether or not to show the loading. */ loading: PropTypes.bool, /** @type {string} The message to be displayed during loading. */ loadingMessage: PropTypes.string, /** @type {Record<object, object>} An object list of primary button props. */ primaryButton: buttonType.isRequired, /** @type {Node} The root node for rendering the modal */ rootNode: isNode() ? PropTypes.instanceOf(Node) : PropTypes.any, /** @type {Record<object, object>} An object list of secondary button props. */ secondaryButton: buttonType.isRequired, /** @type {boolean} Stop event propagation for events that can bubble. */ stopPropagation: PropTypes.bool, /** @type {Array} Array of event types to stop propagation. */ stopPropagationEvents: PropTypes.arrayOf(PropTypes.oneOf(PORTAL_EVENTS)) }; TearsheetSmall.defaultProps = { body: '', children: undefined, flush: false, focusTrap: true, heading: '', description: '', rootNode: isNode() ? document.body : undefined, loading: false, loadingMessage: '', className: '', isOpen: true, labels: {}, stopPropagation: false, stopPropagationEvents: undefined }; export default TearsheetSmall;