UNPKG

optimizely-oui

Version:

Optimizely's Component Library.

134 lines (105 loc) 5.24 kB
"use strict"; 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); } Object.defineProperty(exports, "__esModule", { value: true }); exports["default"] = exports.DropdownBlockLink = void 0; var _react = _interopRequireWildcard(require("react")); var _classnames = _interopRequireDefault(require("classnames")); var _propTypes = _interopRequireDefault(require("prop-types")); var _Checkbox = _interopRequireDefault(require("../../Checkbox")); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; } function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function _getRequireWildcardCache() { return cache; }; return cache; } function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { "default": obj }; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (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; } 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); } var DropdownBlockLink = function DropdownBlockLink(props) { var el = (0, _react.useRef)(null); (0, _react.useEffect)(function () { if (props.hasFauxFocus && el.current) { el.current.scrollIntoView({ behavior: "smooth", block: "center" }); } }); function onClick() { props.onClick(props.value); } function onMouseEnter() { props.onMouseEnter(props.value); } function onMouseLeave() { props.onMouseLeave(props.value); } var styleProps = {}; if (props.minWidth) { styleProps.minWidth = props.minWidth; } return _react["default"].createElement("div", _extends({ className: (0, _classnames["default"])({ link: props.isLink, isSelected: !props.isLink, "oui-dropdown__block-link": props.isLink, "oui-dropdown__block-link--has-focus": props.hasFauxFocus }), ref: el, style: styleProps }, props.testSection ? { "data-test-section": props.testSection } : {}, props.trackId ? { "data-track-id": props.trackId } : {}, !props.isMultiSelect ? { onClick: onClick } : {}, { onMouseEnter: onMouseEnter, onMouseLeave: onMouseLeave }), props.isMultiSelect ? _react["default"].createElement(_Checkbox["default"], { onChange: onClick, checked: props.isItemSelected, label: props.children, isDisabled: false }) : props.children); }; exports.DropdownBlockLink = DropdownBlockLink; DropdownBlockLink.defaultProps = { isLink: true, onMouseEnter: function onMouseEnter() { return null; }, onMouseLeave: function onMouseLeave() { return null; } }; DropdownBlockLink.propTypes = { /** Content to be shown in the menu option */ children: _propTypes["default"].node.isRequired, /** Whether or not this element should be highlighted. */ hasFauxFocus: _propTypes["default"].bool, /** Whether or not this item's checkbox is checked' */ isItemSelected: _propTypes["default"].bool, /** * Should this be a link or non-clickable text? * To indicate item is selected (in single select), * isLink should be false. */ isLink: _propTypes["default"].bool.isRequired, /** Whether or not this item should include a checkbox */ isMultiSelect: _propTypes["default"].bool, /** Minimum width of the list item, useful if * you need to have a block of description text */ minWidth: _propTypes["default"].oneOfType([_propTypes["default"].string, _propTypes["default"].number]), /** Click handler for the menu option */ onClick: _propTypes["default"].func.isRequired, /** MouseEnter handler for the menu option */ onMouseEnter: _propTypes["default"].func, /** MouseLeave handler for the menu option */ onMouseLeave: _propTypes["default"].func, /** Used for data-test-section attribute on the link */ testSection: _propTypes["default"].string, /** Used for data-track-id attribute on the link */ trackId: _propTypes["default"].string, /** An optional value to invoke the onClick callback with */ value: _propTypes["default"].string }; var _default = DropdownBlockLink; exports["default"] = _default;