forked from facebook/react
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSearchInput.js
More file actions
140 lines (129 loc) · 3.99 KB
/
SearchInput.js
File metadata and controls
140 lines (129 loc) · 3.99 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
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow
*/
import * as React from 'react';
import {useEffect, useRef} from 'react';
import Button from './Button';
import ButtonIcon from './ButtonIcon';
import Icon from './Icon';
import styles from './SearchInput.css';
type Props = {
goToNextResult: () => void,
goToPreviousResult: () => void,
placeholder: string,
search: (text: string) => void,
searchIndex: number,
searchResultsCount: number,
searchText: string,
testName?: ?string,
};
export default function SearchInput({
goToNextResult,
goToPreviousResult,
placeholder,
search,
searchIndex,
searchResultsCount,
searchText,
testName,
}: Props): React.Node {
const inputRef = useRef<HTMLInputElement | null>(null);
const resetSearch = () => search('');
const handleChange = ({currentTarget}) => {
search(currentTarget.value);
};
const handleKeyPress = ({key, shiftKey}) => {
if (key === 'Enter') {
if (shiftKey) {
goToPreviousResult();
} else {
goToNextResult();
}
}
};
// Auto-focus search input
useEffect(() => {
if (inputRef.current === null) {
return () => {};
}
const handleKeyDown = (event: KeyboardEvent) => {
const {key, metaKey} = event;
if (key === 'f' && metaKey) {
if (inputRef.current !== null) {
inputRef.current.focus();
event.preventDefault();
event.stopPropagation();
}
}
};
// It's important to listen to the ownerDocument to support the browser extension.
// Here we use portals to render individual tabs (e.g. Profiler),
// and the root document might belong to a different window.
const ownerDocument = inputRef.current.ownerDocument;
ownerDocument.addEventListener('keydown', handleKeyDown);
return () => ownerDocument.removeEventListener('keydown', handleKeyDown);
}, []);
return (
<div className={styles.SearchInput} data-testname={testName}>
<Icon className={styles.InputIcon} type="search" />
<input
data-testname={testName ? `${testName}-Input` : undefined}
className={styles.Input}
onChange={handleChange}
onKeyPress={handleKeyPress}
placeholder={placeholder}
ref={inputRef}
value={searchText}
/>
{!!searchText && (
<React.Fragment>
<span
className={styles.IndexLabel}
data-testname={testName ? `${testName}-ResultsCount` : undefined}>
{Math.min(searchIndex + 1, searchResultsCount)} |{' '}
{searchResultsCount}
</span>
<div className={styles.LeftVRule} />
<Button
data-testname={testName ? `${testName}-PreviousButton` : undefined}
className={styles.IconButton}
disabled={!searchText}
onClick={goToPreviousResult}
title={
<React.Fragment>
Scroll to previous search result (<kbd>Shift</kbd> +{' '}
<kbd>Enter</kbd>)
</React.Fragment>
}>
<ButtonIcon type="up" />
</Button>
<Button
data-testname={testName ? `${testName}-NextButton` : undefined}
className={styles.IconButton}
disabled={!searchText}
onClick={goToNextResult}
title={
<React.Fragment>
Scroll to next search result (<kbd>Enter</kbd>)
</React.Fragment>
}>
<ButtonIcon type="down" />
</Button>
<Button
data-testname={testName ? `${testName}-ResetButton` : undefined}
className={styles.IconButton}
disabled={!searchText}
onClick={resetSearch}
title="Reset search">
<ButtonIcon type="close" />
</Button>
</React.Fragment>
)}
</div>
);
}