UNPKG

optimizely-oui

Version:

Optimizely's Component Library.

184 lines (147 loc) 8.45 kB
function _typeof(obj) { if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); } 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 _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; } function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; } function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); } function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); } function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; } function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); if (superClass) _setPrototypeOf(subClass, superClass); } function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); } 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; } import React from "react"; import PropTypes from "prop-types"; import classNames from "classnames"; var Item = /*#__PURE__*/ function (_React$Component) { _inherits(Item, _React$Component); function Item(props) { var _this; _classCallCheck(this, Item); _this = _possibleConstructorReturn(this, _getPrototypeOf(Item).call(this, props)); _defineProperty(_assertThisInitialized(_this), "ref", React.createRef()); _this._handleOnKeyDown = _this._handleOnKeyDown.bind(_assertThisInitialized(_this)); return _this; } _createClass(Item, [{ key: "componentDidUpdate", value: function componentDidUpdate() { if (this.props.hasFauxFocus) { this.ref.current.scrollIntoView({ behavior: "smooth", block: "center" }); } } }, { key: "_handleOnKeyDown", value: function _handleOnKeyDown(event) { if (event && event.keyCode === 32 && event.target === this._itemWithOnClick) { // Space key was pressed on the item, not a child of the item. // Run the `onClick` and prevent the page from scrolling. event.preventDefault(); this.props.onClick(); } } }, { key: "render", value: function render() { var _this2 = this; var item; var _this$props = this.props, children = _this$props.children, className = _this$props.className, gutters = _this$props.gutters, hasFauxFocus = _this$props.hasFauxFocus, href = _this$props.href, hrefTarget = _this$props.hrefTarget, hrefTitle = _this$props.hrefTitle, isDisabled = _this$props.isDisabled, onClick = _this$props.onClick, onMouseDown = _this$props.onMouseDown, testSection = _this$props.testSection, props = _objectWithoutProperties(_this$props, ["children", "className", "gutters", "hasFauxFocus", "href", "hrefTarget", "hrefTitle", "isDisabled", "onClick", "onMouseDown", "testSection"]); var commonClasses = classNames({ "oui-block-list__item": true, "oui-block-list__item--has-focus": hasFauxFocus, "hard--sides": gutters === "tight" }); if (isDisabled) { item = React.createElement("div", { className: "".concat(commonClasses, " pointer-events--none background--faint muted") }, children); } else if (onClick) { // The element is rendered as a `div` instead of a `button` because the // user may want to pass in a `button` within `children`. // `tabIndex`, `role`, and `onKeyDown` are provided to make the `div` // behave like a button: https://mzl.la/1mRMvQj item = React.createElement("div", { className: "".concat(commonClasses, " link oui-block-list__link"), onClick: onClick, onKeyDown: this._handleOnKeyDown, tabIndex: "0", role: "button", ref: function ref(el) { _this2._itemWithOnClick = el; } }, children); } else if (href) { item = React.createElement("a", { href: href, className: "".concat(commonClasses, " oui-block-list__link display--block"), target: hrefTarget, title: hrefTitle, rel: hrefTarget === "_blank" && "noopener noreferrer" }, children); } else { item = React.createElement("div", _extends({ className: commonClasses, onMouseDown: onMouseDown }, testSection && { "data-test-section": testSection + "-item" }), children); } return React.createElement("li", _extends({ ref: this.ref, style: { wordBreak: "break-word" }, className: className, "data-test-section": testSection }, props), item); } }]); return Item; }(React.Component); Item.propTypes = { /** String or JSX that appears within the component */ children: PropTypes.node.isRequired, /** CSS class names. */ className: PropTypes.string, /** Determines level of padding of item */ gutters: PropTypes.oneOf(["loose", "tight"]), /** Boolean to set faux focus of a list item */ hasFauxFocus: PropTypes.bool, /** URL to navigate to when clicking on the item */ href: PropTypes.string, /** Target that the link, if provided, should open in */ hrefTarget: PropTypes.oneOf(["_self", "_blank"]), /** Sets the `title` attribute on an `href` */ hrefTitle: PropTypes.string, /** Disable the item */ isDisabled: PropTypes.bool, /** Function that is run when clicking on the item */ onClick: PropTypes.func, /** Function that is run when mouse-down event is fired on the item */ onMouseDown: PropTypes.func, /** Hook for automated JavaScript tests */ testSection: PropTypes.string }; Item.defaultProps = { gutters: "loose" }; Item.displayName = "BlockList.Item"; export default Item;