UNPKG

@salesforce/design-system-react

Version:

Salesforce Lightning Design System for React

280 lines (248 loc) 9.75 kB
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) { symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); } keys.push.apply(keys, symbols); } return keys; } function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; } 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; } /* Copyright (c) 2015-present, salesforce.com, inc. All rights reserved */ /* Licensed under BSD 3-Clause - see LICENSE.txt or git.io/sfdc-license */ import React from 'react'; import PropTypes from 'prop-types'; import classNames from 'classnames'; import Avatar from '../../avatar'; import Icon from '../../icon'; import Pill from '../../utilities/pill'; import isReactComponent from '../../../utilities/is-react-component'; var propTypes = { /* * The option object within the selection prop that should have focus. */ activeOption: PropTypes.object, /* * The index of the option object within the selection prop that should have focus. */ activeOptionIndex: PropTypes.number, /** * **Assistive text for accessibility** * This object is merged with the default props object on every render. * * `label`: This is used as a visually hidden label if, no `labels.label` is provided. * * `removePill`: Aids in keyboard interaction with Pills. * * `selectedListboxLabel`: Used to identify the listbox */ assistiveText: PropTypes.shape({ label: PropTypes.string, removePill: PropTypes.string, selectedListboxLabel: PropTypes.string }), /** * CSS classes to be added to the top-level `div` tag. */ className: PropTypes.oneOfType([PropTypes.array, PropTypes.object, PropTypes.string]), /* * Callbacks for various pill events such as click, focus, etc */ events: PropTypes.shape({ onClickPill: PropTypes.func, onPillFocus: PropTypes.func.isRequired, onRequestFocus: PropTypes.func.isRequired, onRequestFocusOnNextPill: PropTypes.func.isRequired, onRequestFocusOnPreviousPill: PropTypes.func.isRequired, onRequestRemove: PropTypes.func.isRequired }), /** * HTML id for component main container */ id: PropTypes.string, /** * Determines whether component renders as a bare pill container with associated styling for child pills */ isBare: PropTypes.bool, /** * Adds inline (inside of input) styling */ isInline: PropTypes.bool, /** * Determines whether component renders as a pill container with associated styling and behavior */ isPillContainer: PropTypes.bool, /** * The value of `aria-orientation` to use on the listbox element */ listboxAriaOrientation: PropTypes.string, /** * The value of `role` to use on the listbox element */ listboxRole: PropTypes.string, /** * The value of `aria-orientation` to use on the container element */ containerAriaOrientation: PropTypes.string, /** * The value of `role` to use on the container element */ containerRole: PropTypes.string, /* * Pill Label */ labels: PropTypes.shape({ label: PropTypes.string, remove: PropTypes.string, title: PropTypes.string }), /** * Changes styles of the input. Currently `entity` is not supported. */ renderAtSelectionLength: PropTypes.number, /** * This callback exposes the selected listbox reference / DOM node to parent components. */ selectedListboxRef: PropTypes.func, /** * Accepts an array of item objects. */ selection: PropTypes.array, /** * Custom styles to be passed to the top-level `div` tag */ style: PropTypes.object, /** * Requests that the active option set focus on render */ listboxHasFocus: PropTypes.bool, /** * Changes styles of the input. Currently `entity` is not supported. */ variant: PropTypes.oneOf(['base', 'inline-listbox', 'readonly']) }; var getAvatar = function getAvatar(option) { var avatarObject = option.avatar; var avatar = null; if (avatarObject) { if (isReactComponent(avatarObject) || avatarObject instanceof HTMLElement) { avatar = avatarObject; } else if (avatarObject.imgSrc) { avatar = /*#__PURE__*/React.createElement(Avatar, { imgSrc: avatarObject.imgSrc, title: avatarObject.title || option.label, variant: avatarObject.variant || 'user' }); } } return avatar; }; var getIcon = function getIcon(option) { var iconObject = (option === null || option === void 0 ? void 0 : option.icon) || null; var icon = null; if (iconObject) { if (isReactComponent(iconObject) || iconObject instanceof HTMLElement) { icon = iconObject; } else if (iconObject.category && iconObject.name) { icon = /*#__PURE__*/React.createElement(Icon, { category: iconObject.category, name: iconObject.name, title: iconObject.title || option.label }); } } return icon; }; var SelectedListBox = function SelectedListBox(_ref) { var _ref$listboxAriaOrien = _ref.listboxAriaOrientation, listboxAriaOrientation = _ref$listboxAriaOrien === void 0 ? 'horizontal' : _ref$listboxAriaOrien, _ref$listboxRole = _ref.listboxRole, listboxRole = _ref$listboxRole === void 0 ? 'listbox' : _ref$listboxRole, _ref$renderAtSelectio = _ref.renderAtSelectionLength, renderAtSelectionLength = _ref$renderAtSelectio === void 0 ? 1 : _ref$renderAtSelectio, selection = _ref.selection, isPillContainer = _ref.isPillContainer, className = _ref.className, id = _ref.id, selectedListboxRef = _ref.selectedListboxRef, style = _ref.style, containerRole = _ref.containerRole, containerAriaOrientation = _ref.containerAriaOrientation, isInline = _ref.isInline, assistiveText = _ref.assistiveText, activeOptionIndex = _ref.activeOptionIndex, listboxHasFocus = _ref.listboxHasFocus, isBare = _ref.isBare, events = _ref.events, labels = _ref.labels; return selection.length >= renderAtSelectionLength ? /*#__PURE__*/React.createElement("div", { // eslint-disable-line jsx-a11y/role-supports-aria-props className: classNames({ 'slds-pill_container': isPillContainer }, className) || undefined, id: id, ref: function ref(_ref2) { if (selectedListboxRef) { selectedListboxRef(_ref2); } }, style: style // Remove role and aria-orientation after slds-has-inline-listbox is deprecated in Combobox , role: containerRole, "aria-orientation": containerAriaOrientation }, /*#__PURE__*/React.createElement("ul", { className: classNames('slds-listbox', { 'slds-listbox_inline': isInline, 'slds-listbox_horizontal': !isInline, 'slds-p-top_xxx-small': !isInline }), "aria-label": assistiveText.selectedListboxLabel, role: listboxRole, "aria-orientation": listboxAriaOrientation }, selection.map(function (option, renderIndex) { var _option$title; var hasTabIndex = renderIndex === activeOptionIndex; var icon = getIcon(option); var avatar = !icon ? getAvatar(option) : null; return /*#__PURE__*/React.createElement("li", { role: "presentation", className: "slds-listbox__item", key: "".concat(id, "-list-item-").concat(option.id) }, /*#__PURE__*/React.createElement(Pill, { active: hasTabIndex && listboxHasFocus, assistiveText: { remove: assistiveText.removePill }, avatar: avatar, bare: option.bare || isBare, error: option.error, events: { onBlur: events.onBlurPill, onClick: typeof events.onClickPill === 'function' ? function (event, data) { events.onClickPill(event, _objectSpread(_objectSpread({}, data), {}, { index: renderIndex })); } : null, onFocus: function onFocus(event, data) { events.onPillFocus(event, _objectSpread(_objectSpread({}, data), {}, { index: renderIndex })); }, onRequestFocusOnNextPill: events.onRequestFocusOnNextPill, onRequestFocusOnPreviousPill: events.onRequestFocusOnPreviousPill, onRequestRemove: function onRequestRemove(event, data) { events.onRequestRemove(event, _objectSpread(_objectSpread({}, data), {}, { index: renderIndex })); }, onRequestFocus: events.onRequestFocus }, eventData: { option: option }, hasError: option.error, icon: icon, labels: { label: option.label, title: (_option$title = option.title) !== null && _option$title !== void 0 ? _option$title : option.label, removeTitle: labels.removePillTitle }, requestFocus: listboxHasFocus, tabIndex: hasTabIndex ? 0 : -1 })); }))) : null; }; SelectedListBox.displayName = 'SelectedListBox'; SelectedListBox.propTypes = propTypes; export default SelectedListBox; //# sourceMappingURL=selected-listbox.js.map