Debian Codegraph @

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

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