-
Notifications
You must be signed in to change notification settings - Fork 381
Expand file tree
/
Copy pathSearchInput.tsx
More file actions
577 lines (536 loc) · 20.7 KB
/
SearchInput.tsx
File metadata and controls
577 lines (536 loc) · 20.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
import { forwardRef, useEffect, useRef, useState } from 'react';
import { css } from '@patternfly/react-styles';
import { Button, ButtonVariant } from '../Button';
import { Badge } from '../Badge';
import { Icon } from '../Icon';
import AngleDownIcon from '@patternfly/react-icons/dist/esm/icons/angle-down-icon';
import AngleUpIcon from '@patternfly/react-icons/dist/esm/icons/angle-up-icon';
import RhMicronsCloseIcon from '@patternfly/react-icons/dist/esm/icons/rh-microns-close-icon';
import SearchIcon from '@patternfly/react-icons/dist/esm/icons/search-icon';
import CaretDownIcon from '@patternfly/react-icons/dist/esm/icons/caret-down-icon';
import ArrowRightIcon from '@patternfly/react-icons/dist/esm/icons/arrow-right-icon';
import { AdvancedSearchMenu } from './AdvancedSearchMenu';
import { TextInputGroup, TextInputGroupMain, TextInputGroupUtilities } from '../TextInputGroup';
import { InputGroup, InputGroupItem } from '../InputGroup';
import { Popper } from '../../helpers';
import { useHasAnimations } from '../../helpers';
import textInputGroupStyles from '@patternfly/react-styles/css/components/TextInputGroup/text-input-group';
import inputGroupStyles from '@patternfly/react-styles/css/components/InputGroup/input-group';
import { IS_INERT } from '../../helpers/inert';
/** Properties for adding search attributes to an advanced search input. These properties must
* be passed in as an object within an array to the search input component's attribute property.
*/
export interface SearchInputSearchAttribute {
/** The search attribute's value to be provided in the search input's query string.
* It should have no spaces and be unique for every attribute.
*/
attr: string;
/** The search attribute's display name. It is used to label the field in the advanced
* search menu.
*/
display: React.ReactNode;
}
/** Properties for creating an expandable search input. These properties should be passed into
* the search input component's expandableInput property.
*/
export interface SearchInputExpandable {
/** Flag to indicate if the search input is expanded. */
isExpanded: boolean;
/** Callback function to toggle the expandable search input. */
onToggleExpand: (event: React.SyntheticEvent<HTMLButtonElement>, isExpanded: boolean) => void;
/** An accessible label for the expandable search input toggle. */
toggleAriaLabel: string;
/** Flag indicating animations should be enabled when the search input expands and collapses. Note: this will change the component's DOM structure. In a future breaking change release, this will become the default behavior and will no longer be needed. */
hasAnimations?: boolean;
}
/** The main search input component. */
export interface SearchInputProps extends Omit<React.HTMLProps<HTMLDivElement>, 'onChange' | 'results' | 'ref'> {
/** Delimiter in the query string for pairing attributes with search values.
* Required whenever attributes are passed as props.
*/
advancedSearchDelimiter?: string;
/** The container to append the menu to.
* If your menu is being cut off you can append it to an element higher up the DOM tree.
* Some examples:
* appendTo={() => document.body}
* appendTo={document.getElementById('target')}
*/
appendTo?: HTMLElement | (() => HTMLElement) | 'inline';
/** An accessible label for the search input. */
'aria-label'?: string;
/** Flag to indicate utilities should be displayed. By default if this prop is undefined or false, utilities will only be displayed when the search input has a value. */
areUtilitiesDisplayed?: boolean;
/** Array of attribute values used for dynamically generated advanced search. */
attributes?: string[] | SearchInputSearchAttribute[];
/** Additional classes added to the search input. */
className?: string;
/** Object that makes the search input expandable/collapsible. */
expandableInput?: SearchInputExpandable;
/* Additional elements added after the attributes in the form.
* The new form elements can be wrapped in a form group component for automatic formatting. */
formAdditionalItems?: React.ReactNode;
/** Attribute label for strings unassociated with one of the provided listed attributes. */
hasWordsAttrLabel?: React.ReactNode;
/** A suggestion for autocompleting. */
hint?: string;
/** Id for the search input */
searchInputId?: string;
/** @hide A reference object to attach to the input box. */
innerRef?: React.RefObject<any>;
/** A flag for controlling the open state of a custom advanced search implementation. */
isAdvancedSearchOpen?: boolean;
/** Flag indicating if search input is disabled. */
isDisabled?: boolean;
/** Flag indicating if the next navigation button is disabled. */
isNextNavigationButtonDisabled?: boolean;
/** Flag indicating if the previous navigation button is disabled. */
isPreviousNavigationButtonDisabled?: boolean;
/** Accessible label for the button to navigate to next result. */
nextNavigationButtonAriaLabel?: string;
/** A callback for when the input value changes. */
onChange?: (event: React.FormEvent<HTMLInputElement>, value: string) => void;
/** A callback for when the user clicks the clear button. */
onClear?: (event: React.SyntheticEvent<HTMLButtonElement>) => void;
/** A callback for when the user clicks to navigate to next result. */
onNextClick?: (event: React.SyntheticEvent<HTMLButtonElement>) => void;
/** A callback for when the user clicks to navigate to previous result. */
onPreviousClick?: (event: React.SyntheticEvent<HTMLButtonElement>) => void;
/** A callback for when the search button is clicked. */
onSearch?: (
event: React.SyntheticEvent<HTMLButtonElement>,
value: string,
attrValueMap: { [key: string]: string }
) => void;
/** A callback for when the open advanced search button is clicked. */
onToggleAdvancedSearch?: (event: React.SyntheticEvent<HTMLButtonElement>, isOpen?: boolean) => void;
/** Accessible label for the button which opens the advanced search form menu. */
openMenuButtonAriaLabel?: string;
/** Placeholder text of the search input. */
placeholder?: string;
/** Accessible label for the button to navigate to previous result. */
previousNavigationButtonAriaLabel?: string;
/** z-index of the advanced search form when appendTo is not inline. */
zIndex?: number;
/** Label for the button which resets the advanced search form and clears the search input. */
resetButtonLabel?: string;
/** The number of search results returned. Either a total number of results,
* or a string representing the current result over the total number of results. i.e. "1 / 5". */
resultsCount?: number | string;
/** Screenreader text that will appear after resultsCount to give context for what that value represents to assistive technologies. */
resultsCountContext?: string;
/** Label for the button which calls the onSearch event handler. */
submitSearchButtonLabel?: string;
/** Value of the search input. */
value?: string;
/** Name attribute for the search input */
name?: string;
/** Additional props to spread to the search input element. */
inputProps?: any;
}
const SearchInputBase: React.FunctionComponent<SearchInputProps> = ({
className,
searchInputId,
value = '',
attributes = [] as string[],
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,
...props
}: SearchInputProps) => {
const [isSearchMenuOpen, setIsSearchMenuOpen] = useState(false);
const [searchValue, setSearchValue] = useState(value);
const searchInputRef = useRef(null);
const ref = useRef(null);
const searchInputInputRef = innerRef || ref;
const searchInputExpandableToggleRef = useRef(null);
const triggerRef = useRef(null);
const popperRef = useRef(null);
const [focusAfterExpandChange, setFocusAfterExpandChange] = useState(false);
const { isExpanded, onToggleExpand, toggleAriaLabel, hasAnimations: hasAnimationsProp } = expandableInput || {};
const hasAnimations = useHasAnimations(hasAnimationsProp);
useEffect(() => {
// this effect and the focusAfterExpandChange variable are needed to focus the input/toggle as needed when the
// expansion toggle is fired without focusing on mount
if (!focusAfterExpandChange) {
return;
} else if (isExpanded) {
searchInputInputRef?.current?.focus();
} else {
if (!hasAnimations) {
searchInputExpandableToggleRef?.current?.focus();
}
}
if (!hasAnimations) {
setFocusAfterExpandChange(false);
}
}, [focusAfterExpandChange, isExpanded, searchInputInputRef, searchInputExpandableToggleRef]);
useEffect(() => {
setSearchValue(value);
}, [value]);
useEffect(() => {
if (attributes.length > 0 && !advancedSearchDelimiter) {
// eslint-disable-next-line no-console
console.error(
'An advancedSearchDelimiter prop is required when advanced search attributes are provided using the attributes prop'
);
}
});
useEffect(() => {
setIsSearchMenuOpen(isAdvancedSearchOpen);
}, [isAdvancedSearchOpen]);
const onChangeHandler = (event: React.FormEvent<HTMLInputElement>, value: string) => {
if (onChange) {
onChange(event, value);
}
setSearchValue(value);
};
const onToggle = (e: React.SyntheticEvent<HTMLButtonElement>) => {
const isOpen = !isSearchMenuOpen;
setIsSearchMenuOpen(isOpen);
if (onToggleAdvancedSearch) {
onToggleAdvancedSearch(e, isOpen);
}
};
const onSearchHandler = (event: React.SyntheticEvent<HTMLButtonElement>) => {
event.preventDefault();
if (onSearch) {
onSearch(event, value, getAttrValueMap());
}
setIsSearchMenuOpen(false);
};
const splitStringExceptInQuotes = (str: string) => {
let quoteType: string;
return str.match(/\\?.|^$/g).reduce(
(p: any, c: string) => {
if (c === "'" || c === '"') {
if (!quoteType) {
quoteType = c;
}
if (c === quoteType) {
p.quote = !p.quote;
}
} else if (!p.quote && c === ' ') {
p.a.push('');
} else {
p.a[p.a.length - 1] += c.replace(/\\(.)/, '$1');
}
return p;
},
{ a: [''] }
).a;
};
const getAttrValueMap = () => {
const attrValue: { [key: string]: string } = {};
const pairs = splitStringExceptInQuotes(searchValue);
pairs.map((pair: string) => {
const splitPair = pair.split(advancedSearchDelimiter);
if (splitPair.length === 2) {
attrValue[splitPair[0]] = splitPair[1].replace(/(^'|'$)/g, '');
} else if (splitPair.length === 1) {
attrValue.haswords = attrValue.hasOwnProperty('haswords')
? `${attrValue.haswords} ${splitPair[0]}`
: splitPair[0];
}
});
return attrValue;
};
const onEnter = (event: React.KeyboardEvent<any>) => {
if (event.key === 'Enter') {
onSearchHandler(event);
}
};
const onClearInput = (e: React.SyntheticEvent<HTMLButtonElement>) => {
if (onClear) {
onClear(e);
}
if (searchInputInputRef && searchInputInputRef.current) {
searchInputInputRef.current.focus();
}
};
const onExpandHandler = (event: React.SyntheticEvent<HTMLButtonElement>) => {
setSearchValue('');
onToggleExpand(event, isExpanded);
setFocusAfterExpandChange(true);
};
const renderUtilities =
value && (resultsCount || (!!onNextClick && !!onPreviousClick) || (!!onClear && !expandableInput));
const buildTextInputGroup = ({ ...searchInputProps } = {}) => (
<TextInputGroup isDisabled={isDisabled} {...searchInputProps}>
<TextInputGroupMain
hint={hint}
icon={<SearchIcon />}
innerRef={searchInputInputRef}
value={searchValue}
placeholder={placeholder}
aria-label={ariaLabel}
onKeyDown={onEnter}
onChange={onChangeHandler}
name={name}
inputId={searchInputId}
inputProps={inputProps}
/>
{(renderUtilities || areUtilitiesDisplayed) && (
<TextInputGroupUtilities>
{resultsCount && (
<Badge isRead screenReaderText={resultsCountContext}>
{resultsCount}
</Badge>
)}
{!!onNextClick && !!onPreviousClick && (
<div className={textInputGroupStyles.textInputGroupGroup}>
<Button
variant={ButtonVariant.plain}
aria-label={previousNavigationButtonAriaLabel}
isDisabled={isDisabled || isPreviousNavigationButtonDisabled}
onClick={onPreviousClick}
icon={<AngleUpIcon />}
/>
<Button
variant={ButtonVariant.plain}
aria-label={nextNavigationButtonAriaLabel}
isDisabled={isDisabled || isNextNavigationButtonDisabled}
onClick={onNextClick}
icon={<AngleDownIcon />}
/>
</div>
)}
{!!onClear && !expandableInput && (
<Button
variant={ButtonVariant.plain}
isDisabled={isDisabled}
aria-label={resetButtonLabel}
onClick={onClearInput}
icon={<RhMicronsCloseIcon />}
/>
)}
</TextInputGroupUtilities>
)}
</TextInputGroup>
);
const expandToggleButton = (
<Button
variant={ButtonVariant.plain}
aria-label={toggleAriaLabel}
aria-expanded={isExpanded}
icon={<SearchIcon />}
onClick={onExpandHandler}
ref={searchInputExpandableToggleRef}
/>
);
const collapseToggleButton = (
<Button
variant={ButtonVariant.plain}
aria-label={toggleAriaLabel}
aria-expanded={isExpanded}
icon={<RhMicronsCloseIcon />}
onClick={onExpandHandler}
/>
);
const singleButtonToggle = (
<Button
variant={ButtonVariant.plain}
aria-label={toggleAriaLabel}
aria-expanded={isExpanded}
icon={isExpanded ? <RhMicronsCloseIcon /> : <SearchIcon />}
onClick={onExpandHandler}
ref={searchInputExpandableToggleRef}
/>
);
const onTransitionEnd = () => {
!isExpanded && focusAfterExpandChange && searchInputExpandableToggleRef?.current?.focus();
setFocusAfterExpandChange(false);
};
const expandableToggle = (
<>
{!hasAnimations && <InputGroupItem isPlain>{singleButtonToggle}</InputGroupItem>}
{hasAnimations && (
<>
<InputGroupItem
className={inputGroupStyles.modifiers.searchExpand}
isPlain
onTransitionEnd={onTransitionEnd}
{...(isExpanded && { inert: IS_INERT })}
>
{expandToggleButton}
</InputGroupItem>
<InputGroupItem
className={inputGroupStyles.modifiers.searchAction}
isPlain
{...(!isExpanded && { inert: IS_INERT })}
>
{collapseToggleButton}
</InputGroupItem>
</>
)}
</>
);
const buildExpandableSearchInput = ({ ...searchInputProps }: any = {}) => (
<InputGroup {...searchInputProps}>
<InputGroupItem
{...(!hasAnimations && { isFill: true })}
{...(hasAnimations && { className: inputGroupStyles.modifiers.searchInput })}
{...(!isExpanded && {
inert: IS_INERT
})}
>
{buildTextInputGroup()}
</InputGroupItem>
{expandableToggle}
</InputGroup>
);
const buildSearchTextInputGroup = ({ ...searchInputProps } = {}) => {
if (expandableInput) {
return buildExpandableSearchInput({ ...searchInputProps });
}
return buildTextInputGroup({ ...searchInputProps });
};
const buildSearchTextInputGroupWithExtraButtons = ({ ...searchInputProps } = {}) => (
<InputGroup ref={triggerRef} {...searchInputProps}>
<InputGroupItem
{...(!hasAnimations && { isFill: true })}
{...(expandableInput && hasAnimations && { className: inputGroupStyles.modifiers.searchInput })}
{...(expandableInput &&
hasAnimations &&
!isExpanded && {
inert: IS_INERT
})}
>
{buildTextInputGroup()}
</InputGroupItem>
{(attributes.length > 0 || onToggleAdvancedSearch) && (
<InputGroupItem isPlain {...(hasAnimations && { className: inputGroupStyles.modifiers.searchAction })}>
<Button
className={isSearchMenuOpen && 'pf-m-expanded'}
variant={ButtonVariant.control}
aria-label={openMenuButtonAriaLabel}
onClick={onToggle}
isDisabled={isDisabled}
aria-expanded={isSearchMenuOpen}
icon={<CaretDownIcon />}
/>
</InputGroupItem>
)}
{!!onSearch && (
<InputGroupItem {...(hasAnimations && { className: inputGroupStyles.modifiers.searchAction })}>
<Button
type="submit"
variant={ButtonVariant.control}
aria-label={submitSearchButtonLabel}
onClick={onSearchHandler}
isDisabled={isDisabled}
icon={
<Icon shouldMirrorRTL>
<ArrowRightIcon />
</Icon>
}
/>
</InputGroupItem>
)}
{expandableInput && <InputGroupItem>{expandableToggle}</InputGroupItem>}
</InputGroup>
);
const searchInputProps = {
...props,
className: css(
expandableInput && hasAnimations && inputGroupStyles.modifiers.searchExpandable,
expandableInput && hasAnimations && isExpanded && inputGroupStyles.modifiers.expanded,
className && css(className)
),
innerRef: searchInputRef
};
if (!!expandableInput && !isExpanded && !hasAnimations) {
return (
<InputGroup {...searchInputProps}>
<InputGroupItem>{expandableToggle}</InputGroupItem>
</InputGroup>
);
}
if (!!onSearch || attributes.length > 0 || !!onToggleAdvancedSearch) {
if (attributes.length > 0) {
const AdvancedSearch = (
<div ref={popperRef}>
<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}
/>
</div>
);
const advancedSearchInputProps = hasAnimations
? {
className: css(
expandableInput && inputGroupStyles.modifiers.searchExpandable,
expandableInput && isExpanded && inputGroupStyles.modifiers.expanded
)
}
: {};
const AdvancedSearchWithPopper = (
<div className={css(className)} ref={searchInputRef} {...props}>
<Popper
trigger={buildSearchTextInputGroupWithExtraButtons(advancedSearchInputProps)}
triggerRef={triggerRef}
popper={AdvancedSearch}
popperRef={popperRef}
isVisible={isSearchMenuOpen}
enableFlip={true}
appendTo={() => appendTo || searchInputRef.current}
zIndex={zIndex}
/>
</div>
);
const AdvancedSearchInline = (
<div className={css(className)} ref={searchInputRef} {...props}>
{buildSearchTextInputGroupWithExtraButtons(advancedSearchInputProps)}
{AdvancedSearch}
</div>
);
return appendTo !== 'inline' ? AdvancedSearchWithPopper : AdvancedSearchInline;
}
return buildSearchTextInputGroupWithExtraButtons({ ...searchInputProps });
}
return buildSearchTextInputGroup(searchInputProps);
};
SearchInputBase.displayName = 'SearchInputBase';
export const SearchInput = forwardRef((props: SearchInputProps, ref: React.Ref<HTMLInputElement>) => (
<SearchInputBase {...props} innerRef={ref as React.MutableRefObject<any>} />
));
SearchInput.displayName = 'SearchInput';