| 1 |
"use strict"; |
| 2 |
Object.defineProperty(exports, "__esModule", { value: true }); |
| 3 |
exports.AdvancedSearchMenu = void 0; |
| 4 |
const jsx_runtime_1 = require("react/jsx-runtime"); |
| 5 |
const react_1 = require("react"); |
| 6 |
const Button_1 = require("../Button"); |
| 7 |
const Form_1 = require("../Form"); |
| 8 |
const TextInput_1 = require("../TextInput"); |
| 9 |
const helpers_1 = require("../../helpers"); |
| 10 |
const Panel_1 = require("../Panel"); |
| 11 |
const react_styles_1 = require("@patternfly/react-styles"); |
| 12 |
const AdvancedSearchMenu = ({ className, parentRef, parentInputRef, value = '', attributes = [], formAdditionalItems, hasWordsAttrLabel = 'Has words', advancedSearchDelimiter, getAttrValueMap, onChange, onSearch, onClear, resetButtonLabel = 'Reset', submitSearchButtonLabel = 'Search', isSearchMenuOpen, onToggleAdvancedMenu }) => { |
| 13 |
const hasWordsId = (0, helpers_1.useSSRSafeId)(); |
| 14 |
const firstAttrRef = (0, react_1.useRef)(null); |
| 15 |
const [putFocusBackOnInput, setPutFocusBackOnInput] = (0, react_1.useState)(false); |
| 16 |
(0, react_1.useEffect)(() => { |
| 17 |
if (attributes.length > 0 && !advancedSearchDelimiter) { |
| 18 |
// eslint-disable-next-line no-console |
| 19 |
console.error('AdvancedSearchMenu: An advancedSearchDelimiter prop is required when advanced search attributes are provided using the attributes prop'); |
| 20 |
} |
| 21 |
}); |
| 22 |
(0, react_1.useEffect)(() => { |
| 23 |
if (isSearchMenuOpen && firstAttrRef && firstAttrRef.current) { |
| 24 |
firstAttrRef.current.focus(); |
| 25 |
setPutFocusBackOnInput(true); |
| 26 |
} |
| 27 |
else if (!isSearchMenuOpen && putFocusBackOnInput && parentInputRef && parentInputRef.current) { |
| 28 |
parentInputRef.current.focus(); |
| 29 |
} |
| 30 |
}, [isSearchMenuOpen]); |
| 31 |
(0, react_1.useEffect)(() => { |
| 32 |
const onDocClick = (event) => { |
| 33 |
const clickedWithinSearchInput = parentRef && parentRef.current.contains(event.target); |
| 34 |
if (isSearchMenuOpen && !clickedWithinSearchInput) { |
| 35 |
onToggleAdvancedMenu(event); |
| 36 |
} |
| 37 |
}; |
| 38 |
const onEscPress = (event) => { |
| 39 |
if (event.key === 'Escape' && isSearchMenuOpen) { |
| 40 |
onToggleAdvancedMenu(event); |
| 41 |
} |
| 42 |
}; |
| 43 |
document.addEventListener('mousedown', onDocClick); |
| 44 |
document.addEventListener('touchstart', onDocClick); |
| 45 |
document.addEventListener('keydown', onEscPress); |
| 46 |
return function cleanup() { |
| 47 |
document.removeEventListener('mousedown', onDocClick); |
| 48 |
document.removeEventListener('touchstart', onDocClick); |
| 49 |
document.removeEventListener('keydown', onEscPress); |
| 50 |
}; |
| 51 |
}, [isSearchMenuOpen, parentRef, onToggleAdvancedMenu]); |
| 52 |
const onSearchHandler = (event) => { |
| 53 |
event.preventDefault(); |
| 54 |
if (onSearch) { |
| 55 |
onSearch(event, value, getAttrValueMap()); |
| 56 |
} |
| 57 |
if (isSearchMenuOpen) { |
| 58 |
onToggleAdvancedMenu(event); |
| 59 |
} |
| 60 |
}; |
| 61 |
const handleValueChange = (attribute, newValue, event) => { |
| 62 |
const newMap = getAttrValueMap(); |
| 63 |
newMap[attribute] = newValue; |
| 64 |
let updatedValue = ''; |
| 65 |
Object.entries(newMap).forEach(([k, v]) => { |
| 66 |
if (v.trim() !== '') { |
| 67 |
/* Wrap the value in quotes if it contains spaces */ |
| 68 |
const quoteWrappedValue = v.includes(' ') ? `'${v.replace(/(^'|'$)/g, '')}'` : v; |
| 69 |
if (k !== 'haswords') { |
| 70 |
updatedValue = `${updatedValue} ${k}${advancedSearchDelimiter}${quoteWrappedValue}`; |
| 71 |
} |
| 72 |
else { |
| 73 |
updatedValue = `${updatedValue} ${quoteWrappedValue}`; |
| 74 |
} |
| 75 |
} |
| 76 |
}); |
| 77 |
if (onChange) { |
| 78 |
onChange(event, updatedValue.replace(/^\s+/g, '')); |
| 79 |
} |
| 80 |
}; |
| 81 |
const getValue = (attribute) => { |
| 82 |
const map = getAttrValueMap(); |
| 83 |
return map.hasOwnProperty(attribute) ? map[attribute] : ''; |
| 84 |
}; |
| 85 |
const buildFormGroups = () => { |
| 86 |
const formGroups = []; |
| 87 |
attributes.forEach((attribute, index) => { |
| 88 |
const display = typeof attribute === 'string' ? attribute : attribute.display; |
| 89 |
const queryAttr = typeof attribute === 'string' ? attribute : attribute.attr; |
| 90 |
if (index === 0) { |
| 91 |
formGroups.push((0, jsx_runtime_1.jsx)(Form_1.FormGroup, { label: display, fieldId: `${queryAttr}_${index}`, children: (0, jsx_runtime_1.jsx)(TextInput_1.TextInput, { ref: firstAttrRef, type: "text", id: `${queryAttr}_${index}`, value: getValue(queryAttr), onChange: (evt, value) => handleValueChange(queryAttr, value, evt) }) }, `${attribute}_${index}`)); |
| 92 |
} |
| 93 |
else { |
| 94 |
formGroups.push((0, jsx_runtime_1.jsx)(Form_1.FormGroup, { label: display, fieldId: `${queryAttr}_${index}`, children: (0, jsx_runtime_1.jsx)(TextInput_1.TextInput, { type: "text", id: `${queryAttr}_${index}`, value: getValue(queryAttr), onChange: (evt, value) => handleValueChange(queryAttr, value, evt) }) }, `${attribute}_${index}`)); |
| 95 |
} |
| 96 |
}); |
| 97 |
formGroups.push((0, jsx_runtime_1.jsx)(Form_1.FormGroup, { label: hasWordsAttrLabel, fieldId: hasWordsId, children: (0, jsx_runtime_1.jsx)(TextInput_1.TextInput, { type: "text", id: hasWordsId, value: getValue('haswords'), onChange: (evt, value) => handleValueChange('haswords', value, evt) }) }, "hasWords")); |
| 98 |
return formGroups; |
| 99 |
}; |
| 100 |
return isSearchMenuOpen ? ((0, jsx_runtime_1.jsx)(Panel_1.Panel, { variant: "raised", className: (0, react_styles_1.css)(className), children: (0, jsx_runtime_1.jsx)(Panel_1.PanelMain, { children: (0, jsx_runtime_1.jsx)(Panel_1.PanelMainBody, { children: (0, jsx_runtime_1.jsxs)(Form_1.Form, { children: [buildFormGroups(), formAdditionalItems ? formAdditionalItems : null, (0, jsx_runtime_1.jsxs)(Form_1.ActionGroup, { children: [(0, jsx_runtime_1.jsx)(Button_1.Button, { variant: "primary", type: "submit", onClick: onSearchHandler, isDisabled: !value, children: submitSearchButtonLabel }), !!onClear && ((0, jsx_runtime_1.jsx)(Button_1.Button, { variant: "link", type: "reset", onClick: onClear, children: resetButtonLabel }))] })] }) }) }) })) : null; |
| 101 |
}; |
| 102 |
exports.AdvancedSearchMenu = AdvancedSearchMenu; |
| 103 |
exports.AdvancedSearchMenu.displayName = 'SearchInput'; |
| 104 |
//# sourceMappingURL=AdvancedSearchMenu.js.map |