| 1 |
"use strict"; |
| 2 |
Object.defineProperty(exports, "__esModule", { value: true }); |
| 3 |
exports.SearchInput = void 0; |
| 4 |
const tslib_1 = require("tslib"); |
| 5 |
const jsx_runtime_1 = require("react/jsx-runtime"); |
| 6 |
const react_1 = require("react"); |
| 7 |
const react_styles_1 = require("@patternfly/react-styles"); |
| 8 |
const Button_1 = require("../Button"); |
| 9 |
const Badge_1 = require("../Badge"); |
| 10 |
const Icon_1 = require("../Icon"); |
| 11 |
const angle_up_icon_1 = tslib_1.__importDefault(require('@patternfly/react-icons/dist/js/icons/angle-up-icon')); |
| 12 |
const rh_microns_close_icon_1 = tslib_1.__importDefault(require('@patternfly/react-icons/dist/js/icons/rh-microns-close-icon')); |
| 13 |
const search_icon_1 = tslib_1.__importDefault(require('@patternfly/react-icons/dist/js/icons/search-icon')); |
| 14 |
const rh_microns_caret_right_icon_1 = tslib_1.__importDefault(require('@patternfly/react-icons/dist/js/icons/rh-microns-caret-right-icon')); |
| 15 |
const rh_microns_caret_down_fill_icon_1 = tslib_1.__importDefault(require('@patternfly/react-icons/dist/js/icons/rh-microns-caret-down-fill-icon')); |
| 16 |
const AdvancedSearchMenu_1 = require("./AdvancedSearchMenu"); |
| 17 |
const TextInputGroup_1 = require("../TextInputGroup"); |
| 18 |
const InputGroup_1 = require("../InputGroup"); |
| 19 |
const helpers_1 = require("../../helpers"); |
| 20 |
const helpers_2 = require("../../helpers"); |
| 21 |
const text_input_group_1 = tslib_1.__importDefault(require("@patternfly/react-styles/css/components/TextInputGroup/text-input-group")); |
| 22 |
const input_group_1 = tslib_1.__importDefault(require("@patternfly/react-styles/css/components/InputGroup/input-group")); |
| 23 |
const SearchInputBase = (_a) => { |
| 24 |
var { className, searchInputId, value = '', attributes = [], formAdditionalItems, hasWordsAttrLabel = 'Has words', advancedSearchDelimiter, placeholder, hint, onChange, onSearch, onClear, onToggleAdvancedSearch, isAdvancedSearchOpen = false, resultsCount, resultsCountContext = ' results', onNextClick, onPreviousClick, innerRef, expandableInput, 'aria-label': ariaLabel = 'Search input', resetButtonLabel = 'Reset', openMenuButtonAriaLabel = 'Open advanced search', previousNavigationButtonAriaLabel = 'Previous', isPreviousNavigationButtonDisabled = false, isNextNavigationButtonDisabled = false, nextNavigationButtonAriaLabel = 'Next', submitSearchButtonLabel = 'Search', isDisabled = false, appendTo, zIndex = 9999, name, areUtilitiesDisplayed, inputProps } = _a, props = tslib_1.__rest(_a, ["className", "searchInputId", "value", "attributes", "formAdditionalItems", "hasWordsAttrLabel", "advancedSearchDelimiter", "placeholder", "hint", "onChange", "onSearch", "onClear", "onToggleAdvancedSearch", "isAdvancedSearchOpen", "resultsCount", "resultsCountContext", "onNextClick", "onPreviousClick", "innerRef", "expandableInput", 'aria-label', "resetButtonLabel", "openMenuButtonAriaLabel", "previousNavigationButtonAriaLabel", "isPreviousNavigationButtonDisabled", "isNextNavigationButtonDisabled", "nextNavigationButtonAriaLabel", "submitSearchButtonLabel", "isDisabled", "appendTo", "zIndex", "name", "areUtilitiesDisplayed", "inputProps"]); |
| 25 |
const [isSearchMenuOpen, setIsSearchMenuOpen] = (0, react_1.useState)(false); |
| 26 |
const [searchValue, setSearchValue] = (0, react_1.useState)(value); |
| 27 |
const searchInputRef = (0, react_1.useRef)(null); |
| 28 |
const ref = (0, react_1.useRef)(null); |
| 29 |
const searchInputInputRef = innerRef || ref; |
| 30 |
const searchInputExpandableToggleRef = (0, react_1.useRef)(null); |
| 31 |
const triggerRef = (0, react_1.useRef)(null); |
| 32 |
const popperRef = (0, react_1.useRef)(null); |
| 33 |
const [focusAfterExpandChange, setFocusAfterExpandChange] = (0, react_1.useState)(false); |
| 34 |
const { isExpanded, onToggleExpand, toggleAriaLabel, hasAnimations: hasAnimationsProp } = expandableInput || {}; |
| 35 |
const hasAnimations = (0, helpers_2.useHasAnimations)(hasAnimationsProp); |
| 36 |
(0, react_1.useEffect)(() => { |
| 37 |
var _a, _b; |
| 38 |
// this effect and the focusAfterExpandChange variable are needed to focus the input/toggle as needed when the |
| 39 |
// expansion toggle is fired without focusing on mount |
| 40 |
if (!focusAfterExpandChange) { |
| 41 |
return; |
| 42 |
} |
| 43 |
else if (isExpanded) { |
| 44 |
(_a = searchInputInputRef === null || searchInputInputRef === void 0 ? void 0 : searchInputInputRef.current) === null || _a === void 0 ? void 0 : _a.focus(); |
| 45 |
} |
| 46 |
else { |
| 47 |
if (!hasAnimations) { |
| 48 |
(_b = searchInputExpandableToggleRef === null || searchInputExpandableToggleRef === void 0 ? void 0 : searchInputExpandableToggleRef.current) === null || _b === void 0 ? void 0 : _b.focus(); |
| 49 |
} |
| 50 |
} |
| 51 |
if (!hasAnimations) { |
| 52 |
setFocusAfterExpandChange(false); |
| 53 |
} |
| 54 |
}, [focusAfterExpandChange, isExpanded, searchInputInputRef, searchInputExpandableToggleRef]); |
| 55 |
(0, react_1.useEffect)(() => { |
| 56 |
setSearchValue(value); |
| 57 |
}, [value]); |
| 58 |
(0, react_1.useEffect)(() => { |
| 59 |
if (attributes.length > 0 && !advancedSearchDelimiter) { |
| 60 |
// eslint-disable-next-line no-console |
| 61 |
console.error('An advancedSearchDelimiter prop is required when advanced search attributes are provided using the attributes prop'); |
| 62 |
} |
| 63 |
}); |
| 64 |
(0, react_1.useEffect)(() => { |
| 65 |
setIsSearchMenuOpen(isAdvancedSearchOpen); |
| 66 |
}, [isAdvancedSearchOpen]); |
| 67 |
const onChangeHandler = (event, value) => { |
| 68 |
if (onChange) { |
| 69 |
onChange(event, value); |
| 70 |
} |
| 71 |
setSearchValue(value); |
| 72 |
}; |
| 73 |
const onToggle = (e) => { |
| 74 |
const isOpen = !isSearchMenuOpen; |
| 75 |
setIsSearchMenuOpen(isOpen); |
| 76 |
if (onToggleAdvancedSearch) { |
| 77 |
onToggleAdvancedSearch(e, isOpen); |
| 78 |
} |
| 79 |
}; |
| 80 |
const onSearchHandler = (event) => { |
| 81 |
event.preventDefault(); |
| 82 |
if (onSearch) { |
| 83 |
onSearch(event, value, getAttrValueMap()); |
| 84 |
} |
| 85 |
setIsSearchMenuOpen(false); |
| 86 |
}; |
| 87 |
const splitStringExceptInQuotes = (str) => { |
| 88 |
let quoteType; |
| 89 |
return str.match(/\\?.|^$/g).reduce((p, c) => { |
| 90 |
if (c === "'" || c === '"') { |
| 91 |
if (!quoteType) { |
| 92 |
quoteType = c; |
| 93 |
} |
| 94 |
if (c === quoteType) { |
| 95 |
p.quote = !p.quote; |
| 96 |
} |
| 97 |
} |
| 98 |
else if (!p.quote && c === ' ') { |
| 99 |
p.a.push(''); |
| 100 |
} |
| 101 |
else { |
| 102 |
p.a[p.a.length - 1] += c.replace(/\\(.)/, '$1'); |
| 103 |
} |
| 104 |
return p; |
| 105 |
}, { a: [''] }).a; |
| 106 |
}; |
| 107 |
const getAttrValueMap = () => { |
| 108 |
const attrValue = {}; |
| 109 |
const pairs = splitStringExceptInQuotes(searchValue); |
| 110 |
pairs.map((pair) => { |
| 111 |
const splitPair = pair.split(advancedSearchDelimiter); |
| 112 |
if (splitPair.length === 2) { |
| 113 |
attrValue[splitPair[0]] = splitPair[1].replace(/(^'|'$)/g, ''); |
| 114 |
} |
| 115 |
else if (splitPair.length === 1) { |
| 116 |
attrValue.haswords = attrValue.hasOwnProperty('haswords') |
| 117 |
? `${attrValue.haswords} ${splitPair[0]}` |
| 118 |
: splitPair[0]; |
| 119 |
} |
| 120 |
}); |
| 121 |
return attrValue; |
| 122 |
}; |
| 123 |
const onEnter = (event) => { |
| 124 |
if (event.key === 'Enter') { |
| 125 |
onSearchHandler(event); |
| 126 |
} |
| 127 |
}; |
| 128 |
const onClearInput = (e) => { |
| 129 |
if (onClear) { |
| 130 |
onClear(e); |
| 131 |
} |
| 132 |
if (searchInputInputRef && searchInputInputRef.current) { |
| 133 |
searchInputInputRef.current.focus(); |
| 134 |
} |
| 135 |
}; |
| 136 |
const onExpandHandler = (event) => { |
| 137 |
setSearchValue(''); |
| 138 |
onToggleExpand(event, isExpanded); |
| 139 |
setFocusAfterExpandChange(true); |
| 140 |
}; |
| 141 |
const renderUtilities = value && (resultsCount || (!!onNextClick && !!onPreviousClick) || (!!onClear && !expandableInput)); |
| 142 |
const buildTextInputGroup = (_a = {}) => { |
| 143 |
var searchInputProps = tslib_1.__rest(_a, []); |
| 144 |
return ((0, jsx_runtime_1.jsxs)(TextInputGroup_1.TextInputGroup, Object.assign({ isDisabled: isDisabled }, searchInputProps, { children: [(0, jsx_runtime_1.jsx)(TextInputGroup_1.TextInputGroupMain, { hint: hint, icon: (0, jsx_runtime_1.jsx)(search_icon_1.default, {}), innerRef: searchInputInputRef, value: searchValue, placeholder: placeholder, "aria-label": ariaLabel, onKeyDown: onEnter, onChange: onChangeHandler, name: name, inputId: searchInputId, inputProps: inputProps }), (renderUtilities || areUtilitiesDisplayed) && ((0, jsx_runtime_1.jsxs)(TextInputGroup_1.TextInputGroupUtilities, { children: [resultsCount && ((0, jsx_runtime_1.jsx)(Badge_1.Badge, { isRead: true, screenReaderText: resultsCountContext, children: resultsCount })), !!onNextClick && !!onPreviousClick && ((0, jsx_runtime_1.jsxs)("div", { className: text_input_group_1.default.textInputGroupGroup, children: [(0, jsx_runtime_1.jsx)(Button_1.Button, { variant: Button_1.ButtonVariant.plain, "aria-label": previousNavigationButtonAriaLabel, isDisabled: isDisabled || isPreviousNavigationButtonDisabled, onClick: onPreviousClick, icon: (0, jsx_runtime_1.jsx)(angle_up_icon_1.default, {}) }), (0, jsx_runtime_1.jsx)(Button_1.Button, { variant: Button_1.ButtonVariant.plain, "aria-label": nextNavigationButtonAriaLabel, isDisabled: isDisabled || isNextNavigationButtonDisabled, onClick: onNextClick, icon: (0, jsx_runtime_1.jsx)(rh_microns_caret_down_fill_icon_1.default, {}) })] })), !!onClear && !expandableInput && ((0, jsx_runtime_1.jsx)(Button_1.Button, { variant: Button_1.ButtonVariant.plain, isDisabled: isDisabled, "aria-label": resetButtonLabel, onClick: onClearInput, icon: (0, jsx_runtime_1.jsx)(rh_microns_close_icon_1.default, {}) }))] }))] }))); |
| 145 |
}; |
| 146 |
const expandToggleButton = ((0, jsx_runtime_1.jsx)(Button_1.Button, { variant: Button_1.ButtonVariant.plain, "aria-label": toggleAriaLabel, "aria-expanded": isExpanded, icon: (0, jsx_runtime_1.jsx)(search_icon_1.default, {}), onClick: onExpandHandler, ref: searchInputExpandableToggleRef })); |
| 147 |
const collapseToggleButton = ((0, jsx_runtime_1.jsx)(Button_1.Button, { variant: Button_1.ButtonVariant.plain, "aria-label": toggleAriaLabel, "aria-expanded": isExpanded, icon: (0, jsx_runtime_1.jsx)(rh_microns_close_icon_1.default, {}), onClick: onExpandHandler })); |
| 148 |
const singleButtonToggle = ((0, jsx_runtime_1.jsx)(Button_1.Button, { variant: Button_1.ButtonVariant.plain, "aria-label": toggleAriaLabel, "aria-expanded": isExpanded, icon: isExpanded ? (0, jsx_runtime_1.jsx)(rh_microns_close_icon_1.default, {}) : (0, jsx_runtime_1.jsx)(search_icon_1.default, {}), onClick: onExpandHandler, ref: searchInputExpandableToggleRef })); |
| 149 |
const onTransitionEnd = () => { |
| 150 |
var _a; |
| 151 |
!isExpanded && focusAfterExpandChange && ((_a = searchInputExpandableToggleRef === null || searchInputExpandableToggleRef === void 0 ? void 0 : searchInputExpandableToggleRef.current) === null || _a === void 0 ? void 0 : _a.focus()); |
| 152 |
setFocusAfterExpandChange(false); |
| 153 |
}; |
| 154 |
const expandableToggle = ((0, jsx_runtime_1.jsxs)(jsx_runtime_1.Fragment, { children: [!hasAnimations && (0, jsx_runtime_1.jsx)(InputGroup_1.InputGroupItem, { isPlain: true, children: singleButtonToggle }), hasAnimations && ((0, jsx_runtime_1.jsxs)(jsx_runtime_1.Fragment, { children: [(0, jsx_runtime_1.jsx)(InputGroup_1.InputGroupItem, Object.assign({ className: input_group_1.default.modifiers.searchExpand, isPlain: true, onTransitionEnd: onTransitionEnd }, (isExpanded && { inert: '' }), { children: expandToggleButton })), (0, jsx_runtime_1.jsx)(InputGroup_1.InputGroupItem, Object.assign({ className: input_group_1.default.modifiers.searchAction, isPlain: true }, (!isExpanded && { inert: '' }), { children: collapseToggleButton }))] }))] })); |
| 155 |
const buildExpandableSearchInput = (_a = {}) => { |
| 156 |
var searchInputProps = tslib_1.__rest(_a, []); |
| 157 |
return ((0, jsx_runtime_1.jsxs)(InputGroup_1.InputGroup, Object.assign({}, searchInputProps, { children: [(0, jsx_runtime_1.jsx)(InputGroup_1.InputGroupItem, Object.assign({}, (!hasAnimations && { isFill: true }), (hasAnimations && { className: input_group_1.default.modifiers.searchInput }), (!isExpanded && { |
| 158 |
inert: '' |
| 159 |
}), { children: buildTextInputGroup() })), expandableToggle] }))); |
| 160 |
}; |
| 161 |
const buildSearchTextInputGroup = (_a = {}) => { |
| 162 |
var searchInputProps = tslib_1.__rest(_a, []); |
| 163 |
if (expandableInput) { |
| 164 |
return buildExpandableSearchInput(Object.assign({}, searchInputProps)); |
| 165 |
} |
| 166 |
return buildTextInputGroup(Object.assign({}, searchInputProps)); |
| 167 |
}; |
| 168 |
const buildSearchTextInputGroupWithExtraButtons = (_a = {}) => { |
| 169 |
var searchInputProps = tslib_1.__rest(_a, []); |
| 170 |
return ((0, jsx_runtime_1.jsxs)(InputGroup_1.InputGroup, Object.assign({ ref: triggerRef }, searchInputProps, { children: [(0, jsx_runtime_1.jsx)(InputGroup_1.InputGroupItem, Object.assign({}, (!hasAnimations && { isFill: true }), (expandableInput && hasAnimations && { className: input_group_1.default.modifiers.searchInput }), (expandableInput && |
| 171 |
hasAnimations && |
| 172 |
!isExpanded && { |
| 173 |
inert: '' |
| 174 |
}), { children: buildTextInputGroup() })), (attributes.length > 0 || onToggleAdvancedSearch) && ((0, jsx_runtime_1.jsx)(InputGroup_1.InputGroupItem, Object.assign({ isPlain: true }, (hasAnimations && { className: input_group_1.default.modifiers.searchAction }), { children: (0, jsx_runtime_1.jsx)(Button_1.Button, { className: isSearchMenuOpen && 'pf-m-expanded', variant: Button_1.ButtonVariant.control, "aria-label": openMenuButtonAriaLabel, onClick: onToggle, isDisabled: isDisabled, "aria-expanded": isSearchMenuOpen, icon: (0, jsx_runtime_1.jsx)(rh_microns_caret_down_fill_icon_1.default, {}) }) }))), !!onSearch && ((0, jsx_runtime_1.jsx)(InputGroup_1.InputGroupItem, Object.assign({}, (hasAnimations && { className: input_group_1.default.modifiers.searchAction }), { children: (0, jsx_runtime_1.jsx)(Button_1.Button, { type: "submit", variant: Button_1.ButtonVariant.control, "aria-label": submitSearchButtonLabel, onClick: onSearchHandler, isDisabled: isDisabled, icon: (0, jsx_runtime_1.jsx)(Icon_1.Icon, { shouldMirrorRTL: true, children: (0, jsx_runtime_1.jsx)(rh_microns_caret_right_icon_1.default, {}) }) }) }))), expandableInput && (0, jsx_runtime_1.jsx)(InputGroup_1.InputGroupItem, { children: expandableToggle })] }))); |
| 175 |
}; |
| 176 |
const searchInputProps = Object.assign(Object.assign({}, props), { className: (0, react_styles_1.css)(expandableInput && hasAnimations && input_group_1.default.modifiers.searchExpandable, expandableInput && hasAnimations && isExpanded && input_group_1.default.modifiers.expanded, className && (0, react_styles_1.css)(className)), innerRef: searchInputRef }); |
| 177 |
if (!!expandableInput && !isExpanded && !hasAnimations) { |
| 178 |
return ((0, jsx_runtime_1.jsx)(InputGroup_1.InputGroup, Object.assign({}, searchInputProps, { children: (0, jsx_runtime_1.jsx)(InputGroup_1.InputGroupItem, { children: expandableToggle }) }))); |
| 179 |
} |
| 180 |
if (!!onSearch || attributes.length > 0 || !!onToggleAdvancedSearch) { |
| 181 |
if (attributes.length > 0) { |
| 182 |
const AdvancedSearch = ((0, jsx_runtime_1.jsx)("div", { ref: popperRef, children: (0, jsx_runtime_1.jsx)(AdvancedSearchMenu_1.AdvancedSearchMenu, { value: value, parentRef: searchInputRef, parentInputRef: searchInputInputRef, onSearch: onSearch, onClear: onClear, onChange: onChange, onToggleAdvancedMenu: onToggle, resetButtonLabel: resetButtonLabel, submitSearchButtonLabel: submitSearchButtonLabel, attributes: attributes, formAdditionalItems: formAdditionalItems, hasWordsAttrLabel: hasWordsAttrLabel, advancedSearchDelimiter: advancedSearchDelimiter, getAttrValueMap: getAttrValueMap, isSearchMenuOpen: isSearchMenuOpen }) })); |
| 183 |
const advancedSearchInputProps = hasAnimations |
| 184 |
? { |
| 185 |
className: (0, react_styles_1.css)(expandableInput && input_group_1.default.modifiers.searchExpandable, expandableInput && isExpanded && input_group_1.default.modifiers.expanded) |
| 186 |
} |
| 187 |
: {}; |
| 188 |
const AdvancedSearchWithPopper = ((0, jsx_runtime_1.jsx)("div", Object.assign({ className: (0, react_styles_1.css)(className), ref: searchInputRef }, props, { children: (0, jsx_runtime_1.jsx)(helpers_1.Popper, { trigger: buildSearchTextInputGroupWithExtraButtons(advancedSearchInputProps), triggerRef: triggerRef, popper: AdvancedSearch, popperRef: popperRef, isVisible: isSearchMenuOpen, enableFlip: true, appendTo: () => appendTo || searchInputRef.current, zIndex: zIndex }) }))); |
| 189 |
const AdvancedSearchInline = ((0, jsx_runtime_1.jsxs)("div", Object.assign({ className: (0, react_styles_1.css)(className), ref: searchInputRef }, props, { children: [buildSearchTextInputGroupWithExtraButtons(advancedSearchInputProps), AdvancedSearch] }))); |
| 190 |
return appendTo !== 'inline' ? AdvancedSearchWithPopper : AdvancedSearchInline; |
| 191 |
} |
| 192 |
return buildSearchTextInputGroupWithExtraButtons(Object.assign({}, searchInputProps)); |
| 193 |
} |
| 194 |
return buildSearchTextInputGroup(searchInputProps); |
| 195 |
}; |
| 196 |
SearchInputBase.displayName = 'SearchInputBase'; |
| 197 |
exports.SearchInput = (0, react_1.forwardRef)((props, ref) => ((0, jsx_runtime_1.jsx)(SearchInputBase, Object.assign({}, props, { innerRef: ref })))); |
| 198 |
exports.SearchInput.displayName = 'SearchInput'; |
| 199 |
//# sourceMappingURL=SearchInput.js.map |