forked from deephaven/web-client-ui
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSearchInput.tsx
More file actions
173 lines (157 loc) · 4.33 KB
/
SearchInput.tsx
File metadata and controls
173 lines (157 loc) · 4.33 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
import React, { PureComponent } from 'react';
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
import { vsArrowLeft, vsArrowRight, vsSearch } from '@deephaven/icons';
import classNames from 'classnames';
import Button from './Button';
import './SearchInput.scss';
import { GLOBAL_SHORTCUTS } from './shortcuts';
import { ContextActions } from './context-actions';
interface SearchInputProps {
value: string;
placeholder: string;
onBlur?: React.FocusEventHandler<HTMLInputElement>;
onChange: React.ChangeEventHandler<HTMLInputElement>;
onKeyDown: React.KeyboardEventHandler<HTMLInputElement>;
className: string;
disabled?: boolean;
matchCount: number;
id: string;
'data-testid'?: string;
cursor?: {
index: number | undefined;
next: (direction: 'forward' | 'back') => void;
};
}
class SearchInput extends PureComponent<SearchInputProps> {
static defaultProps = {
placeholder: 'Search',
className: '',
matchCount: null,
onKeyDown(): void {
// no-op
},
id: '',
'data-testid': undefined,
cursor: undefined,
};
constructor(props: SearchInputProps) {
super(props);
this.inputField = React.createRef();
this.searchChangeSelection = React.createRef();
}
componentDidMount(): void {
this.setInputPaddingRight();
}
componentDidUpdate(): void {
this.setInputPaddingRight();
}
focus(): void {
this.inputField.current?.focus();
}
select(): void {
this.inputField.current?.select();
}
inputField: React.RefObject<HTMLInputElement>;
searchChangeSelection: React.RefObject<HTMLDivElement>;
setInputPaddingRight(): void {
const inputField = this.inputField.current;
const searchChangeSelection = this.searchChangeSelection.current;
if (inputField && searchChangeSelection) {
const paddingRight = searchChangeSelection.getBoundingClientRect().width;
inputField.style.paddingRight = `${paddingRight}px`;
}
}
render(): JSX.Element {
const {
value,
placeholder,
onBlur,
onChange,
className,
disabled,
matchCount,
id,
onKeyDown,
'data-testid': dataTestId,
cursor,
} = this.props;
let matchCountSection;
const contextActions = [
{
action: () => cursor?.next('forward'),
shortcut: GLOBAL_SHORTCUTS.NEXT,
},
{
action: () => cursor?.next('back'),
shortcut: GLOBAL_SHORTCUTS.PREVIOUS,
},
];
if (cursor && matchCount > 1) {
matchCountSection = (
<>
<Button
kind="ghost"
className="search-change-button"
type="button"
onClick={() => {
cursor.next('back');
}}
icon={vsArrowLeft}
tooltip={`Previous match (${GLOBAL_SHORTCUTS.PREVIOUS.getDisplayText()})`}
/>
<span className="search-change-text">
{cursor.index !== undefined && `${cursor.index + 1} of `}
{matchCount}
</span>
<Button
kind="ghost"
className="search-change-button"
type="button"
onClick={() => {
cursor.next('forward');
}}
icon={vsArrowRight}
tooltip={`Next match (${GLOBAL_SHORTCUTS.NEXT.getDisplayText()})`}
/>
</>
);
} else {
matchCountSection = matchCount > 0 && (
<span className="search-match">{matchCount}</span>
);
}
return (
<div className={classNames('search-group', className)}>
<input
type="search"
value={value}
onBlur={onBlur}
onChange={onChange}
onKeyDown={onKeyDown}
className="form-control"
disabled={disabled}
placeholder={placeholder}
ref={this.inputField}
id={id}
data-testid={dataTestId}
/>
{matchCount != null ? (
<>
<div
className="search-change-selection"
ref={this.searchChangeSelection}
>
{matchCountSection}
</div>
<ContextActions actions={contextActions} />
</>
) : (
<span className="search-icon">
<FontAwesomeIcon icon={vsSearch} />
</span>
)}
</div>
);
}
}
export default SearchInput;