UNPKG

lucid-ui

Version:

A UI component library from AppNexus.

107 lines (93 loc) 5.71 kB
import _keys from "lodash/keys"; import _isEmpty from "lodash/isEmpty"; 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 _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 { lucidClassNames } from '../../util/style-helpers'; import CloseIcon from '../Icon/CloseIcon/CloseIcon'; import { filterTypes, rejectTypes, omitProps } from '../../util/component-types'; var cx = lucidClassNames.bind('&-Tag'); var bool = PropTypes.bool, func = PropTypes.func, node = PropTypes.node, string = PropTypes.string, oneOf = PropTypes.oneOf; var defaultProps = { isTop: false, hasLightBackground: true, isRemovable: false, onRemove: _noop, kind: 'default' }; export var Tag = function Tag(props) { var isTop = props.isTop, isRemovable = props.isRemovable, children = props.children, className = props.className, onRemove = props.onRemove, hasLightBackground = props.hasLightBackground, kind = props.kind, passThroughs = _objectWithoutProperties(props, ["isTop", "isRemovable", "children", "className", "onRemove", "hasLightBackground", "kind"]); var handleRemove = function handleRemove(_ref) { var event = _ref.event; onRemove({ props: props, event: event }); }; var subTags = filterTypes(children, Tag); var otherChildren = rejectTypes(children, Tag); var hasOtherChildren = !_isEmpty(otherChildren); var isLeaf = _isEmpty(subTags); return /*#__PURE__*/React.createElement("div", _extends({}, omitProps(passThroughs, undefined, [].concat(_toConsumableArray(_keys(Tag.propTypes)), ['callbackId'])), { className: cx('&', { '&-is-top': isTop, '&-is-leaf': isLeaf, '&-is-removable': isRemovable, '&-has-light-background': hasLightBackground, '&-default': kind === 'default', '&-primary': kind === 'primary', '&-success': kind === 'success', '&-warning': kind === 'warning', '&-danger': kind === 'danger', '&-info': kind === 'info' }, className) }), hasOtherChildren && /*#__PURE__*/React.createElement("span", { className: cx('&-other-children') }, otherChildren, isRemovable && /*#__PURE__*/React.createElement(CloseIcon, { onClick: handleRemove, className: cx('&-remove-button'), size: 7, isClickable: true })), subTags); }; Tag.defaultProps = defaultProps; Tag.displayName = 'Tag'; Tag.peek = { description: "\n\t\t\t`Tag` is a visualization for selected settings. \n\t\t\t\t", notes: { overview: "\n\t\t\t\t\tA visualization for items. Tags can be removable and can be nested into groups.\n\t\t\t\t\t", intendedUse: "\n\t\t\t\t\tTags are typically used to display filter selections. Tags can be interactive or display-only. They can also be grouped into a parent container.\n\t\t\t\t\t\t\t\t\t\t\t\n\t\t\t\t\t**Styling notes**\n\t\t\t\t\t\n\t\t\t\t\t- The default style is not interactive, it does not have a `CloseIcon`.\n\t\t\t\t\t- Use `isRemovable='true'` for interactive tags.\n\t\t\t\t\t- Styling is optimized for 40 or fewer characters.\n\t\t\t\t\t", technicalRecommendations: "\n\t\t\t\t\tNone\n\t\t\t" }, categories: ['communication'] }; Tag.propTypes = { isTop: bool, hasLightBackground: bool, isRemovable: bool, kind: oneOf(['primary', 'success', 'warning', 'danger', 'info', 'default']), onRemove: func, children: node, className: string }; export default Tag;