UNPKG

optimizely-oui

Version:

Optimizely's Component Library.

112 lines (94 loc) 3.27 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); } import React, { useEffect, useRef } from "react"; import classNames from "classnames"; import PropTypes from "prop-types"; import Checkbox from "../../Checkbox"; export var DropdownBlockLink = function DropdownBlockLink(props) { var el = useRef(null); 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.createElement("div", _extends({ className: classNames({ 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.createElement(Checkbox, { onChange: onClick, checked: props.isItemSelected, label: props.children, isDisabled: false }) : props.children); }; 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.node.isRequired, /** Whether or not this element should be highlighted. */ hasFauxFocus: PropTypes.bool, /** Whether or not this item's checkbox is checked' */ isItemSelected: PropTypes.bool, /** * Should this be a link or non-clickable text? * To indicate item is selected (in single select), * isLink should be false. */ isLink: PropTypes.bool.isRequired, /** Whether or not this item should include a checkbox */ isMultiSelect: PropTypes.bool, /** Minimum width of the list item, useful if * you need to have a block of description text */ minWidth: PropTypes.oneOfType([PropTypes.string, PropTypes.number]), /** Click handler for the menu option */ onClick: PropTypes.func.isRequired, /** MouseEnter handler for the menu option */ onMouseEnter: PropTypes.func, /** MouseLeave handler for the menu option */ onMouseLeave: PropTypes.func, /** Used for data-test-section attribute on the link */ testSection: PropTypes.string, /** Used for data-track-id attribute on the link */ trackId: PropTypes.string, /** An optional value to invoke the onClick callback with */ value: PropTypes.string }; export default DropdownBlockLink;