lucid-ui
Version:
A UI component library from AppNexus.
169 lines (142 loc) • 7.69 kB
JavaScript
import _map from "lodash/map";
import _keys from "lodash/keys";
import _isEqual from "lodash/isEqual";
import _noop from "lodash/noop";
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 _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); }
function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && Symbol.iterator in Object(iter)) return Array.from(iter); }
function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); }
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }
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 'react-peek/prop-types';
import Icon from '../Icon/Icon';
import RadioButtonLabeled from '../RadioButtonLabeled/RadioButtonLabeled';
import CheckboxLabeled from '../CheckboxLabeled/CheckboxLabeled';
import { lucidClassNames } from '../../util/style-helpers';
import { omitProps } from '../../util/component-types';
var cx = lucidClassNames.bind('&-IconSelect');
var arrayOf = PropTypes.arrayOf,
bool = PropTypes.bool,
func = PropTypes.func,
node = PropTypes.node,
number = PropTypes.number,
oneOf = PropTypes.oneOf,
string = PropTypes.string,
shape = PropTypes.shape;
var getFigureParent = function getFigureParent(domNode) {
if (domNode.classList.contains(cx('&-Item'))) {
return domNode;
}
if (domNode === document.body) {
throw new Error("domNode is not a child of .".concat(cx('&-Item')));
}
if (domNode.parentElement) {
return getFigureParent(domNode.parentElement);
}
return;
};
var defaultProps = {
kind: 'multiple',
isDisabled: false,
onSelect: _noop
};
export var IconSelect = function IconSelect(props) {
var className = props.className,
children = props.children,
kind = props.kind,
items = props.items,
isDisabled = props.isDisabled,
onSelect = props.onSelect,
passThroughs = _objectWithoutProperties(props, ["className", "children", "kind", "items", "isDisabled", "onSelect"]);
var handleClick = function handleClick(event) {
if (!props.isDisabled) {
var domNode = getFigureParent(event.target);
if (domNode) {
var _id = domNode.dataset.id;
domNode.focus();
if (!domNode.hasAttribute('disabled') && _id) {
onSelect(_id, {
event: event,
props: props
});
}
}
}
};
var getChildIcon = function getChildIcon(icon) {
return icon ? icon : /*#__PURE__*/React.createElement(Icon, null, /*#__PURE__*/React.createElement("rect", {
x: "0",
y: "0",
width: "16",
height: "16"
}), /*#__PURE__*/React.createElement("rect", {
x: "1",
y: "1",
width: "14",
height: "14",
fill: "white"
}));
};
var getInputComponent = function getInputComponent(item) {
var kind = props.kind,
className = props.className,
isDisabled = props.isDisabled;
var Label = item.label;
var singleSelect = _isEqual(kind, 'single');
return singleSelect ? /*#__PURE__*/React.createElement(RadioButtonLabeled, {
Label: Label,
className: cx('&-Item-radio', _defineProperty({}, "".concat(className, "-radio"), className)),
isDisabled: isDisabled || item.isDisabled,
isSelected: item.isSelected // tabIndex={item.tabIndex}
}) : /*#__PURE__*/React.createElement(CheckboxLabeled, {
Label: Label,
className: cx('&-Item-checkbox', _defineProperty({}, "".concat(className, "-checkbox"), className)),
isDisabled: isDisabled || item.isDisabled ? true : false,
isIndeterminate: item.isPartial ? true : false,
isSelected: item.isSelected ? true : false // tabIndex={item.tabIndex}
});
};
return /*#__PURE__*/React.createElement("span", _extends({}, omitProps(passThroughs, undefined, [].concat(_toConsumableArray(_keys(IconSelect.propTypes)), ['items'])), {
className: cx('&', className)
}), _map(items, function (childItem, index) {
var _cx3;
var itemDisabled = isDisabled || childItem.isDisabled;
return /*#__PURE__*/React.createElement("figure", {
key: "iconselectitem_".concat(index),
className: cx('&-Item', childItem.className, (_cx3 = {}, _defineProperty(_cx3, "".concat(className, "-Item"), className), _defineProperty(_cx3, '&-Item-is-disabled', itemDisabled), _defineProperty(_cx3, '&-Item-is-partial', childItem.isPartial), _defineProperty(_cx3, '&-Item-is-selected', childItem.isSelected), _defineProperty(_cx3, '&-Item-multi', kind === 'multiple'), _defineProperty(_cx3, '&-Item-single', kind === 'single'), _cx3)),
"data-id": childItem.id,
onClick: itemDisabled ? undefined : handleClick
}, childItem.icon && getChildIcon(childItem.icon), /*#__PURE__*/React.createElement("figcaption", {
className: cx('&-Item-figcaption')
}, getInputComponent(childItem)));
}), children);
};
IconSelect.displayName = 'IconSelect';
IconSelect.defaultProps = defaultProps;
IconSelect.peek = {
description: "\n\t\tIconSelect allow you to pair icons together to form a related cluster.\n\t\tAny props not explicitly called out are spread on to the root\n\t\tcomponent.\n\t",
categories: ['controls', 'selectors']
};
IconSelect.propTypes = {
className: string,
children: node,
items: arrayOf(shape({
id: string.isRequired,
icon: node,
label: node,
isSelected: bool,
isPartial: bool,
tabIndex: number,
isDisabled: bool,
className: string
})).isRequired,
kind: oneOf(['single', 'multiple']),
onSelect: func,
isDisabled: bool
};
export default IconSelect;