Debian Codegraph @

node/@patternfly/react-core/dist/esm/components/SearchInput/AdvancedSearchMenu.js

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