@carbon/ibm-security
Version:
Carbon for Cloud & Cognitive IBM Security UI components
263 lines (262 loc) • 11.7 kB
JavaScript
import _extends from "@babel/runtime/helpers/extends";
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 Data decorator
* @copyright IBM Security 2019 - 2021
*/
import deprecate from 'carbon-components-react/es/prop-types/deprecate';
import PropTypes from 'prop-types';
import React, { Component } from 'react';
import * as defaultLabels from '../../globals/nls';
import { isNode } from '../../globals/utils/capabilities';
import Decorator from './Decorator';
import PanelV2 from '../PanelV2';
import Portal, { PORTAL_EVENTS } from '../Portal';
var defaultProps = Decorator.defaultProps,
propTypes = Decorator.propTypes;
var DataDecorator = /*#__PURE__*/function (_Component) {
function DataDecorator() {
var _this;
_classCallCheck(this, DataDecorator);
for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
args[_key] = arguments[_key];
}
_this = _callSuper(this, DataDecorator, [].concat(args));
_defineProperty(_this, "state", {
isOpen: false
});
_defineProperty(_this, "toggleOpen", function () {
return _this.state.isOpen ? _this.close() : _this.open();
});
_defineProperty(_this, "open", function () {
_this.setState({
isOpen: true
});
_this.props.onOpen();
});
_defineProperty(_this, "close", function () {
_this.setState({
isOpen: false
});
_this.props.onClose();
});
return _this;
}
_inherits(DataDecorator, _Component);
return _createClass(DataDecorator, [{
key: "render",
value: function render() {
var _this2 = this;
var _this$props = this.props,
children = _this$props.children,
className = _this$props.className,
closeButton = _this$props.closeButton,
focusTrap = _this$props.focusTrap,
focusTrapOptions = _this$props.focusTrapOptions,
inline = _this$props.inline,
labels = _this$props.labels,
noIcon = _this$props.noIcon,
primaryButton = _this$props.primaryButton,
renderFooter = _this$props.renderFooter,
rootNode = _this$props.rootNode,
score = _this$props.score,
secondaryButton = _this$props.secondaryButton,
stopPropagation = _this$props.stopPropagation,
stopPropagationEvents = _this$props.stopPropagationEvents,
subtitle = _this$props.subtitle,
type = _this$props.type,
value = _this$props.value,
title = _this$props.title,
scoreThresholds = _this$props.scoreThresholds,
scoreDescription = _this$props.scoreDescription,
propOnContextMenu = _this$props.onContextMenu,
midLineTruncation = _this$props.midLineTruncation;
var onContextMenu = propOnContextMenu ? function (event) {
event.preventDefault();
propOnContextMenu(event);
} : undefined;
var decoratorProps = {
className: className,
inline: inline,
noIcon: noIcon,
score: score,
type: type,
value: value,
title: title,
scoreThresholds: scoreThresholds,
scoreDescription: scoreDescription,
onContextMenu: onContextMenu,
midLineTruncation: midLineTruncation
};
var componentLabels = _objectSpread(_objectSpread(_objectSpread({}, defaultLabels.labels), labels), defaultLabels.filterFalsey({
DATA_DECORATOR_PRIMARY_BUTTON: primaryButton && primaryButton.label || '',
DATA_DECORATOR_SECONDARY_BUTTON: secondaryButton && secondaryButton.label || '',
DATA_DECORATOR_CLOSE_BUTTON: closeButton && closeButton.label || ''
}));
return /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement(Decorator, _extends({}, decoratorProps, {
active: this.state.isOpen,
onClick: function onClick(event) {
event.stopPropagation();
_this2.toggleOpen();
}
})), /*#__PURE__*/React.createElement(PanelV2, {
closeButton: {
onClick: function onClick(event) {
_this2.close(event, type, value);
if (closeButton && closeButton.onClick) {
closeButton.onClick(event, type, value);
}
}
},
focusTrap: focusTrap,
focusTrapOptions: focusTrapOptions,
isOpen: this.state.isOpen,
labels: _objectSpread(_objectSpread({}, componentLabels), defaultLabels.filterFalsey({
PANEL_CONTAINER_PRIMARY_BUTTON: componentLabels.DATA_DECORATOR_PRIMARY_BUTTON,
PANEL_CONTAINER_SECONDARY_BUTTON: componentLabels.DATA_DECORATOR_SECONDARY_BUTTON,
PANEL_CONTAINER_CLOSE_BUTTON: componentLabels.DATA_DECORATOR_CLOSE_BUTTON
})),
onClose: this.close,
primaryButton: primaryButton && _objectSpread(_objectSpread({}, primaryButton), {}, {
onClick: function onClick(event) {
if (primaryButton.closePanel === undefined || primaryButton.closePanel) {
_this2.close(event, type, value);
}
if (primaryButton.onClick) {
primaryButton.onClick(event, type, value);
}
}
}),
renderFooter: renderFooter,
rootNode: rootNode,
secondaryButton: secondaryButton && _objectSpread(_objectSpread({}, secondaryButton), {}, {
onClick: function onClick(event) {
if (secondaryButton.closePanel === undefined || secondaryButton.closePanel) {
_this2.close(event, type, value);
}
if (secondaryButton.onClick) {
secondaryButton.onClick(event, type, value);
}
}
}),
stopPropagation: stopPropagation,
stopPropagationEvents: stopPropagationEvents,
subtitle: subtitle,
title: value
}, children));
}
}]);
}(Component);
var buttonType = PropTypes.shape({
closePanel: PropTypes.bool,
onClick: PropTypes.func,
label: PropTypes.string,
isDisabled: PropTypes.bool,
icon: PropTypes.object,
iconDescription: PropTypes.string
});
DataDecorator.propTypes = {
/** @type {ReactNode} The children of the DataDecorator. */
children: PropTypes.node,
/** @type {string} class name for rendered content. */
className: PropTypes.string,
/** @type {Record<string, any>} An object list of close button props. */
closeButton: buttonType,
/** @type {boolean} Focus trap. */
focusTrap: PropTypes.bool,
focusTrapOptions: Portal.propTypes.focusTrapOptions,
/** @type {boolean} Determines if this is inline or not. */
inline: propTypes.inline,
/** @type {object} Labels for DataDecorator and children */
labels: defaultLabels.propType,
/** @type {object} Mid-line truncation options applied to value of decorator if applicable. */
midLineTruncation: PropTypes.shape({
enabled: PropTypes.bool,
maxLength: PropTypes.number,
front: PropTypes.number,
back: PropTypes.number
}),
/** @type {boolean} Whether the rendered Decorator includes an icon */
noIcon: PropTypes.bool,
/** @type {Function} The function to call when the DataDecorator Panel closes. */
onClose: PropTypes.func,
/** @type {Function} The function to call when the DataDecorator is secondary-clicked */
onContextMenu: PropTypes.func,
/** @type {Function} The function to call when the DataDecorator Panel opens. */
onOpen: PropTypes.func,
/** @type {Record<string, any>} An object list of primary button props. */
primaryButton: deprecate(buttonType, "\nThe prop `primaryButton` for DataDecorator has been deprecated in favor of `renderFooter`."),
/** @type {Function} Panel footer render prop. */
renderFooter: PropTypes.func,
/** @type {ReactNode|any} The root node for rendering the panel */
rootNode: isNode() ? PropTypes.instanceOf(Node) : PropTypes.any,
/** @type {number} The score of the data. */
score: PropTypes.number,
/** @type {func} Descriptive text for screen readers that details the severity of a score. */
scoreDescription: PropTypes.func,
/** @type {number} The external URL. */
scoreThresholds: function scoreThresholds(props, propName) {
if (!Array.isArray(props.scoreThresholds) || props.scoreThresholds.length !== 4 || !props.scoreThresholds.every(function (number) {
return typeof number === 'number';
})) {
return new Error("".concat(propName, " is required to be an array of four numbers."));
}
return null;
},
/** @type {Record<string, any>} An object list of secondary button props. */
secondaryButton: deprecate(buttonType, "\nThe prop `secondaryButton` for DataDecorator has been deprecated in favor of `renderFooter`."),
/** @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)),
/** @type {ReactNode} Child elements for the panel's subtitle. */
subtitle: PropTypes.node,
/** @type {string} decorator's title. */
title: PropTypes.string,
/** @type {string} The type of data. */
type: PropTypes.string.isRequired,
/** @type {string} The value of the data. */
value: PropTypes.string.isRequired
};
DataDecorator.defaultProps = {
children: undefined,
className: undefined,
closeButton: undefined,
focusTrap: true,
focusTrapOptions: Portal.defaultProps.focusTrapOptions,
inline: defaultProps.inline,
labels: {},
noIcon: false,
onContextMenu: undefined,
onClose: function onClose() {},
onOpen: function onOpen() {},
primaryButton: undefined,
renderFooter: null,
rootNode: undefined,
score: undefined,
scoreThresholds: [0, 4, 7, 10],
secondaryButton: undefined,
subtitle: undefined,
title: undefined,
stopPropagation: false,
stopPropagationEvents: undefined,
scoreDescription: function scoreDescription(score, scoreThresholds) {
return "Score ".concat(score, " out of ").concat(scoreThresholds.slice(-1)[0]);
},
midLineTruncation: {
enabled: false,
maxLength: 20,
front: 12,
back: 4
}
};
export default DataDecorator;