UNPKG

@carbon/ibm-cloud-cognitive-cdai

Version:
292 lines (287 loc) 10.7 kB
import _extends from "@babel/runtime/helpers/extends"; import _defineProperty from "@babel/runtime/helpers/defineProperty"; function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; } function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; } // // Copyright IBM Corp. 2020, 2020 // // This source code is licensed under the Apache-2.0 license found in the // LICENSE file in the root directory of this source tree. // import React, { Fragment } from 'react'; import PropTypes from 'prop-types'; import pluralize from 'pluralize'; import CreatableSelect from 'react-select/creatable'; import { Tag } from 'carbon-components-react'; import { Close16, Search16 } from '@carbon/icons-react'; import { idePrefix } from '../../globals/js/settings'; var Highlight = /*#__PURE__*/React.memo(function (_ref) { var children = _ref.children, search = _ref.search; if ( // We accept arbitrary React nodes but we can't highlight them typeof children !== 'string' || !search) { return children; } var searchRegExp = new RegExp(search.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'), 'ig'); var newChildren = []; var result; var index = 0; while ((result = searchRegExp.exec(children)) !== null) { newChildren.push( // Non-matching substring (could be empty) children.substring(index, result.index), /*#__PURE__*/ // Matching substring (can't be empty) React.createElement("mark", { key: newChildren.length }, children.substring(result.index, searchRegExp.lastIndex))); index = searchRegExp.lastIndex; } // Remainder of string did not match (could be empty) newChildren.push(children.substring(index)); return newChildren; }); Highlight.displayName = 'Highlight'; Highlight.propTypes = { children: PropTypes.oneOfType([PropTypes.string, PropTypes.node]), search: PropTypes.string }; // custom styling var customStyles = { multiValueLabel: function multiValueLabel(provided) { return _objectSpread(_objectSpread({}, provided), {}, { color: 'inherit' }); }, control: function control(provided, state) { var style = _objectSpread(_objectSpread({}, provided), {}, { borderRadius: 0, paddingLeft: '26px', minHeight: '3rem', outline: '2px solid transparent', // important overrides borderTop: 'none', borderRight: 'none', borderLeft: 'none', backgroundColor: undefined }); if (state.isFocused || state.isHovered) { style = _objectSpread(_objectSpread({}, style), {}, { borderBottom: 'solid 1px transparent', paddingLeft: '27px' }); } return style; }, menu: function menu(provided) { return _objectSpread(_objectSpread({}, provided), {}, { borderRadius: 0, padding: '1rem 26px', margin: '2px 0', // important overrides backgroundColor: undefined }); }, placeholder: function placeholder(provided) { return _objectSpread({}, provided); }, groupHeading: function groupHeading(provided) { return _objectSpread(_objectSpread({}, provided), {}, { textTransform: 'none', fontSize: '0.875rem' }); } }; // custom behaviour var getNewOptionData = function getNewOptionData() { var allowPlaintext = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : true; return function (inputValue, optionLabel) { if (allowPlaintext) { return { label: optionLabel, value: inputValue, __plaintext__: true }; } }; }; var formatCreateLabel = function formatCreateLabel(searchForText) { return function (inputValue) { return "".concat(searchForText, " \"").concat(inputValue, "\""); }; }; var findCategoryForOption = function findCategoryForOption(option, allOptions) { var category = allOptions.find(function (category) { return category.options.find(function (thisOption) { return thisOption.value === option.value; }); }); return pluralize.singular(category.label); }; var _formatOptionLabel = function formatOptionLabel(option, context, allOptions) { // highlight search text if we"re rendering an existing option in the menu, // not in the tags already selected, and not in the "seach for..." option var shouldHighlight = !context.selectValue.includes(option) && !option.__plaintext__; // when rendering in the input, rather than the list, we want to include // the category name, but not for plain text searches var includeCategory = context.selectValue.includes(option) && !option.__plaintext__; var valueSection = shouldHighlight ? /*#__PURE__*/React.createElement(Highlight, { search: context.inputValue }, option.label) : option.label; return includeCategory ? /*#__PURE__*/React.createElement(Fragment, null, /*#__PURE__*/React.createElement("span", { className: "".concat(idePrefix, "-filter--category") }, findCategoryForOption(option, allOptions), ":"), valueSection) : valueSection; }; // custom components var MultiValueContainer = function MultiValueContainer(props) { return /*#__PURE__*/React.createElement(Tag, { type: props.data.type || 'filter', className: "".concat(idePrefix, "-filter--tag") }, /*#__PURE__*/React.createElement("div", { className: "".concat(idePrefix, "-filter--tag-container") }, props.children)); }; var MultiValueRemove = function MultiValueRemove(props) { var type = props.data.type || 'filter'; return /*#__PURE__*/React.createElement("button", _extends({ type: "button" }, props.innerProps, { className: "".concat(idePrefix, "-filter--close").concat(type === 'filter' ? " ".concat(idePrefix, "-filter--tag-filter") : /* istanbul ignore next */'') }), /*#__PURE__*/React.createElement(Close16, { description: "Close" })); }; var DropdownIndicator = function DropdownIndicator() { return null; }; var IndicatorSeparator = function IndicatorSeparator() { return null; }; var ClearIndicator = function ClearIndicator(props) { return /*#__PURE__*/React.createElement("div", _extends({}, props.innerProps, { className: "".concat(idePrefix, "-filter--clear") }), /*#__PURE__*/React.createElement(Close16, { description: "Clear" })); }; var IdeFilter = function IdeFilter(_ref2) { var ariaLabel = _ref2.ariaLabel, onChange = _ref2.onChange, onInputChange = _ref2.onInputChange, onBlur = _ref2.onBlur, onFocus = _ref2.onFocus, options = _ref2.options, value = _ref2.value, inputValue = _ref2.inputValue, placeholderText = _ref2.placeholderText, menuIsOpen = _ref2.menuIsOpen, loadingMessage = _ref2.loadingMessage, allowCreateWhileLoading = _ref2.allowCreateWhileLoading, light = _ref2.light, isLoading = _ref2.isLoading, searchForText = _ref2.searchForText, SearchIcon = _ref2.searchIcon, autoFocus = _ref2.autoFocus, allowPlaintext = _ref2.allowPlaintext; return /*#__PURE__*/React.createElement("div", { className: "".concat(idePrefix, "-filter").concat(light ? " ".concat(idePrefix, "-filter--light") : '') }, SearchIcon ? /*#__PURE__*/React.createElement(SearchIcon, { className: "".concat(idePrefix, "-filter--search-icon") }) : /*#__PURE__*/React.createElement(Search16, { className: "".concat(idePrefix, "-filter--search-icon"), description: placeholderText }), /*#__PURE__*/React.createElement(CreatableSelect, { components: { MultiValueContainer: MultiValueContainer, MultiValueRemove: MultiValueRemove, DropdownIndicator: DropdownIndicator, IndicatorSeparator: IndicatorSeparator, ClearIndicator: ClearIndicator }, "aria-label": ariaLabel, styles: customStyles, menuIsOpen: menuIsOpen, isMulti: true, value: value, inputValue: inputValue, onChange: onChange, onInputChange: onInputChange, onBlur: onBlur, onFocus: onFocus, options: options, placeholder: placeholderText, className: "".concat(idePrefix, "-filter--select"), classNamePrefix: "".concat(idePrefix, "-filter"), formatOptionLabel: function formatOptionLabel(option, context) { return _formatOptionLabel(option, context, options); }, formatCreateLabel: formatCreateLabel(searchForText), getNewOptionData: getNewOptionData(allowPlaintext), loadingMessage: loadingMessage, allowCreateWhileLoading: allowCreateWhileLoading, isLoading: isLoading // eslint-disable-next-line jsx-a11y/no-autofocus , autoFocus: autoFocus })); }; IdeFilter.displayName = 'IdeFilter'; IdeFilter.defaultProps = { placeholderText: 'Search', ariaLabel: 'Search', searchForText: 'Search for', closeText: 'Close', clearText: 'Clear', allowCreateWhileLoading: true, isLoading: false, autoFocus: false, allowPlaintext: true }; IdeFilter.propTypes = { allowCreateWhileLoading: PropTypes.bool, allowPlaintext: PropTypes.bool, ariaLabel: PropTypes.string, autoFocus: PropTypes.bool, inputValue: PropTypes.string, isLoading: PropTypes.bool, light: PropTypes.bool, loadingMessage: PropTypes.func, menuIsOpen: PropTypes.bool, onBlur: PropTypes.func, onChange: PropTypes.func.isRequired, onFocus: PropTypes.func, onInputChange: PropTypes.func, options: PropTypes.arrayOf(PropTypes.shape({ label: PropTypes.string.isRequired, options: PropTypes.arrayOf(PropTypes.shape({ label: PropTypes.string.isRequired, value: PropTypes.string.isRequired, type: PropTypes.string })).isRequired })).isRequired, placeholderText: PropTypes.string, searchForText: PropTypes.string, searchIcon: PropTypes.oneOfType([PropTypes.element, PropTypes.func]), value: PropTypes.arrayOf(PropTypes.shape({ label: PropTypes.string.isRequired, value: PropTypes.string.isRequired })) }; MultiValueContainer.propTypes = { children: PropTypes.node, data: PropTypes.shape({ type: PropTypes.string }) }; MultiValueRemove.propTypes = { children: PropTypes.node, data: PropTypes.shape({ type: PropTypes.string }), innerProps: PropTypes.object }; ClearIndicator.propTypes = { innerProps: PropTypes.object }; export default IdeFilter;