UNPKG

optimizely-oui

Version:

Optimizely's Component Library.

115 lines (94 loc) 5.04 kB
function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); } function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; } function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; } import React from "react"; import PropTypes from "prop-types"; import classNames from "classnames"; import Icon from "react-oui-icons"; /** * @param {Object} props - Properties passed to component * @returns {ReactElement} */ var Link = function Link(_ref) { var _classNames; var ariaLabel = _ref.ariaLabel, children = _ref.children, className = _ref.className, href = _ref.href, leftIcon = _ref.leftIcon, onClick = _ref.onClick, _ref$isDisabled = _ref.isDisabled, isDisabled = _ref$isDisabled === void 0 ? false : _ref$isDisabled, _ref$isFullWidth = _ref.isFullWidth, isFullWidth = _ref$isFullWidth === void 0 ? false : _ref$isFullWidth, _ref$newWindow = _ref.newWindow, newWindow = _ref$newWindow === void 0 ? false : _ref$newWindow, rightIcon = _ref.rightIcon, style = _ref.style, testSection = _ref.testSection, props = _objectWithoutProperties(_ref, ["ariaLabel", "children", "className", "href", "leftIcon", "onClick", "isDisabled", "isFullWidth", "newWindow", "rightIcon", "style", "testSection"]); var classes = classNames((_classNames = {}, _defineProperty(_classNames, "link", true), _defineProperty(_classNames, "link--".concat(style), style), _defineProperty(_classNames, "link--disabled", isDisabled), _defineProperty(_classNames, "pointer-events--none", isDisabled), _defineProperty(_classNames, "width--1-1", isFullWidth), _defineProperty(_classNames, "display--inline-block ", isFullWidth), _defineProperty(_classNames, "flex--inline flex-justified--center flex-align--center", leftIcon || rightIcon), _classNames), className); var leftIconComp = leftIcon ? React.createElement("div", { className: "flex flex-self--center push-half--right" }, React.createElement(Icon, { name: leftIcon, size: "small" })) : ""; var rightIconComp = rightIcon ? React.createElement("div", { className: "flex flex-self--center push-half--left" }, React.createElement(Icon, { name: rightIcon, size: "small" })) : ""; if (!href && !onClick) { return React.createElement("span", _extends({ "data-oui-component": true, className: classes, "data-test-section": testSection, "data-track-id": testSection, disabled: isDisabled }, props), leftIcon && leftIconComp, children, rightIcon && rightIconComp); } return React.createElement("a", _extends({ "aria-label": ariaLabel, "data-oui-component": true, href: href, className: classes, "data-test-section": testSection, "data-track-id": testSection, onClick: onClick }, newWindow ? { target: "_blank", rel: "noopener noreferrer" } : {}, { disabled: isDisabled }, props), leftIcon && leftIconComp, children, rightIcon && rightIconComp); }; Link.propTypes = { /** Label to use for screen readers */ ariaLabel: PropTypes.string, /** Text/node that is linked */ children: PropTypes.node.isRequired, /** CSS class names. */ className: PropTypes.string, /** Href for the anchor element */ href: PropTypes.string, /** Disable link and interactivity */ isDisabled: PropTypes.bool, /** Whether or not this link is full width */ isFullWidth: PropTypes.bool, /** Icon to display on the left */ leftIcon: PropTypes.node, /** Open link in new window */ newWindow: PropTypes.bool, /** Click handler function */ onClick: PropTypes.func, /** Icon to display on the right */ rightIcon: PropTypes.node, /** Link style options */ style: PropTypes.oneOf(["default", "dark", "muted", "bad-news", "reverse"]), /** Hook for automated JavaScript tests */ testSection: PropTypes.string }; export default Link;