chayns-components
Version:
A set of beautiful React components for developing chayns® applications.
130 lines (125 loc) • 5.2 kB
JavaScript
;
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
exports.__esModule = true;
exports.default = void 0;
var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
var _clsx = _interopRequireDefault(require("clsx"));
var _propTypes = _interopRequireDefault(require("prop-types"));
var _react = _interopRequireWildcard(require("react"));
var _is = require("../../utils/is");
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
/**
* @component
*/
let displayedIconWarning = false;
/**
* Displays a FontAwesome icon.
*/
class Icon extends _react.Component {
constructor(props) {
super(props);
this.onClick = this.onClick.bind(this);
}
componentDidMount() {
const {
icon
} = this.props;
if (!displayedIconWarning && icon && !(0, _is.isString)(icon)) {
displayedIconWarning = true;
// eslint-disable-next-line no-console,max-len
console.warn('[chayns components] Icon: You are still using fortawesome SVG-icons. Consider changing to fontawesome-font-icons. https://github.com/TobitSoftware/chayns-components/blob/master/src/react-chayns-icon/README.md#deprecated');
}
}
onClick(e) {
const {
onClick,
disabled,
stopPropagation
} = this.props;
if (onClick && !disabled) onClick(e);
if (stopPropagation) e.stopPropagation();
}
render() {
const {
icon,
className,
onClick,
disabled,
stopPropagation,
style,
...other
} = this.props;
if (Array.isArray(icon)) {
return /*#__PURE__*/_react.default.createElement("span", {
className: `fa-stack ${className}`,
style: {
height: '1em',
width: '1.4em',
lineHeight: '1em',
...style
}
}, icon.map(s => /*#__PURE__*/_react.default.createElement("i", (0, _extends2.default)({
key: s,
className: `${s} fa-stack-1x`,
onClick: this.onClick
}, other))));
}
let iconName = icon;
if (typeof icon === 'object') {
iconName = `${icon.prefix} fa-${icon.iconName}`;
}
if (!(0, _is.isString)(iconName)) return null;
const classes = (0, _clsx.default)('react-chayns-icon', iconName, className, onClick && 'react-chayns-icon--clickable', disabled && 'react-chayns-icon--disabled');
return /*#__PURE__*/_react.default.createElement("i", (0, _extends2.default)({
className: classes,
style: style,
onClick: this.onClick
}, other));
}
}
exports.default = Icon;
Icon.propTypes = {
/**
* The icon to display. Supply a string or an array of strings like this:
* `fa fa-plane`. Search for icons and their strings on
* https://fontawesome.com/icons/. For backwards compatibility you can also
* specify an icon object from the `@fortawesome`-packages, but this should
* not be used going forward.
*/
icon: _propTypes.default.oneOfType([_propTypes.default.string.isRequired, _propTypes.default.shape({
iconName: _propTypes.default.string.isRequired,
prefix: _propTypes.default.string.isRequired
}).isRequired, _propTypes.default.arrayOf(_propTypes.default.string.isRequired)]).isRequired,
/**
* A classname string that will be applied to the HTML element of the icon.
*/
className: _propTypes.default.string,
/**
* A React style object that will be applied ot the `<i>`-element of the
* icon.
*/
style: _propTypes.default.objectOf(_propTypes.default.oneOfType([_propTypes.default.number, _propTypes.default.string])),
/**
* A callback that is called for the `onclick`-event on the icon.
*/
onClick: _propTypes.default.func,
/**
* Disables any user interaction on the icon and renders it in a disabled
* style.
*/
disabled: _propTypes.default.bool,
/**
* Wether to stop propagation of click events to parent elements.
*/
stopPropagation: _propTypes.default.bool
};
Icon.defaultProps = {
className: '',
style: undefined,
onClick: undefined,
disabled: false,
stopPropagation: false
};
Icon.displayName = 'Icon';
//# sourceMappingURL=Icon.js.map