@momentum-ui/react-collaboration
Version:
Cisco Momentum UI Framework for React Collaboration Applications
170 lines • 7.96 kB
JavaScript
/** @component icon */
var __extends = (this && this.__extends) || (function () {
var extendStatics = function (d, b) {
extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
return extendStatics(d, b);
};
return function (d, b) {
if (typeof b !== "function" && b !== null)
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
var __assign = (this && this.__assign) || function () {
__assign = Object.assign || function(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
t[p] = s[p];
}
return t;
};
return __assign.apply(this, arguments);
};
var __rest = (this && this.__rest) || function (s, e) {
var t = {};
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
t[p] = s[p];
if (s != null && typeof Object.getOwnPropertySymbols === "function")
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
t[p[i]] = s[p[i]];
}
return t;
};
import React from 'react';
import PropTypes from 'prop-types';
import iconNames from '@momentum-ui/icons/data/momentumUiIconsNames.json';
import { Button } from '@momentum-ui/react-collaboration';
import { getColorValue } from '../utils';
/**
* @deprecated - Components in the legacy folder (/src/legacy) are deprecated. Please use a component from the components folder (/src/components) instead. Legacy components may not follow accessibility standards.
**/
var Icon = /** @class */ (function (_super) {
__extends(Icon, _super);
function Icon() {
return _super !== null && _super.apply(this, arguments) || this;
}
Icon.prototype.render = function () {
var _a = this.props, append = _a.append, ariaLabel = _a.ariaLabel, buttonClassName = _a.buttonClassName, buttonProps = _a.buttonProps, color = _a.color, className = _a.className, description = _a.description, onClick = _a.onClick, name = _a.name, prepend = _a.prepend, size = _a.size, sizeOverride = _a.sizeOverride, style = _a.style, title = _a.title, type = _a.type, icon = _a.icon, otherProps = __rest(_a, ["append", "ariaLabel", "buttonClassName", "buttonProps", "color", "className", "description", "onClick", "name", "prepend", "size", "sizeOverride", "style", "title", "type", "icon"]);
var consoleHandler = function (message, data) {
/* eslint-disable no-console */
switch (message) {
case 'color-warn':
console.warn("[@momentum-ui/react-collaboration] Icon: ".concat(data, " may not exist in the design system,") +
" please use a color name from https://momentum.design/styles/color/style");
break;
case 'name-error':
console.warn("[@momentum-ui/react-collaboration] Icon: Icon ".concat(data, " does not exist in the design system.") +
" Visit https://momentum.design/styles/icons/library for a list of available icons or to request a new icon.");
break;
}
/* eslint-enable no-console */
};
var getSize = function () {
var defaultSize = 16;
var sizeFromName = Number(name.split('_')[1]);
return size || sizeFromName || defaultSize;
};
var getColor = function () {
if (color.startsWith('#')) {
consoleHandler('color-warn', color);
return color;
}
var value = getColorValue(color);
return value;
};
var getNameClass = function () {
var iconName = name.startsWith('icon-') ? name.replace(/^(icon-)/, '') : name;
if (sizeOverride) {
iconName = name.split('_')[0] + "_".concat(getSize());
}
return iconNames.includes(iconName)
? "icon-".concat(iconName)
: consoleHandler('name-error', iconName);
};
var getAriaLabel = function () {
if (ariaLabel) {
return ariaLabel;
}
if (!ariaLabel) {
if (title && description)
return "".concat(title, " ").concat(description);
if (title)
return title;
if (description)
return description;
}
return null;
};
var getIcon = function () {
var styles = __assign(__assign({ fontSize: getSize() }, (color && { color: getColor() })), style);
return (React.createElement("i", __assign({ className: "md-icon icon" +
" ".concat(getNameClass()) +
"".concat((className && " ".concat(className)) || '') +
"".concat((prepend && " md-prepend") || '') +
"".concat((append && " md-append") || ''), "aria-label": !onClick ? getAriaLabel() : null, style: styles }, (title && !onClick && { title: title }), (!onClick && __assign({}, otherProps)))));
};
return onClick ? (React.createElement(Button, __assign({ className: 'md-button--icon' +
"".concat((type && " md-button--icon-".concat(type)) || '') +
"".concat((buttonClassName && " ".concat(buttonClassName)) || ''), ariaLabel: getAriaLabel(), onClick: onClick }, buttonProps, (title && { title: title }), otherProps), icon ? icon : getIcon())) : icon ? (icon) : (getIcon());
};
return Icon;
}(React.PureComponent));
Icon.propTypes = {
/** @prop Add margin to the left of Icon | null */
append: PropTypes.bool,
/** @prop Text to display for blindness accessibility features | null */
ariaLabel: PropTypes.string,
/** @prop Optional Button class name string | '' */
buttonClassName: PropTypes.string,
/** @prop Optional props to pass to underlying button component | null */
buttonProps: PropTypes.object,
/** @prop Optional color css styling | '' */
color: PropTypes.string,
/** @prop Optional class name string | '' */
className: PropTypes.string,
/** @prop Icon description text | '' */
description: PropTypes.string,
/** @prop Icon node to draw instead of supplying name */
icon: PropTypes.node,
/** @prop Legacy icon name */
name: PropTypes.string,
/** @prop Handler invoked by click of the user | null */
onClick: PropTypes.func,
/** @prop Add margin to the right of Icon | null */
prepend: PropTypes.bool,
/** @prop Sets Icon size | null */
size: PropTypes.number,
// Internal prop to override iconName with size prop */
sizeOverride: PropTypes.bool,
/** @prop Additional style properties to be applied | null */
style: PropTypes.object,
/** @prop Sets Icon Title prop | '' */
title: PropTypes.string,
/** @prop Sets Icon Type | '' */
type: PropTypes.oneOf(['', 'white']),
};
Icon.defaultProps = {
append: false,
ariaLabel: null,
buttonClassName: '',
buttonProps: null,
color: '',
className: '',
description: '',
onClick: null,
prepend: false,
size: null,
sizeOverride: false,
style: null,
title: '',
type: '',
};
Icon.displayName = 'Icon';
export default Icon;
//# sourceMappingURL=index.js.map