-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Expand file tree
/
Copy pathTagSearch.tsx
More file actions
221 lines (192 loc) · 5.57 KB
/
TagSearch.tsx
File metadata and controls
221 lines (192 loc) · 5.57 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
import { EuiTitle, EuiInputPopover, EuiSelectable } from "@elastic/eui";
import React, { useEffect, useRef, useState } from "react";
import {
SuggestionModes,
TagSuggestionInstance,
} from "../hooks/useSearchInputWithTags";
interface TagSearchInterface {
currentTag: string;
tagsString: string;
setTagsString: (tagsString: string) => void;
acceptSuggestion: (suggestion: TagSuggestionInstance) => void;
tagSuggestions: TagSuggestionInstance[];
suggestionMode: SuggestionModes;
setCursorPosition: (position: number | undefined) => void;
}
interface SelectableOption {
label: string;
checked?: "on" | "off" | undefined;
suggestion: TagSuggestionInstance;
}
// Helper Functions
const suggestionFormatter = (item: TagSuggestionInstance) => {
return {
label: item.suggestion,
suggestion: item,
showIcons: false,
append: <span>{item.description}</span>,
};
};
const getCursorPosition = (
inputNode: React.MutableRefObject<HTMLInputElement | null>
) => {
return inputNode.current?.selectionStart || undefined;
};
const computePlaceholderText = (
tagSuggestions: TagSuggestionInstance[] | undefined
) => {
return !tagSuggestions
? ""
: "e.g. " +
tagSuggestions
.slice(0, 2)
.map((s) => `"${s.suggestion}"`)
.join(" or ");
};
const generateResultsCount = (
currentTag: string,
suggestionMode: SuggestionModes,
tagSuggestions: TagSuggestionInstance[]
) => {
let resultsCount = undefined;
const currentTagIsEmpty = currentTag.length <= 0;
const currentTagHasNoValue = currentTag.split(":")[1] === "";
const operatingWord =
currentTagIsEmpty || currentTagHasNoValue ? "possible" : "matching";
const counterWord = suggestionMode === "KEY" ? `key` : `value`;
if (tagSuggestions.length > 0) {
const isPlural = tagSuggestions.length > 1 ? "s" : "";
resultsCount = (
<span>{`${tagSuggestions.length} ${operatingWord} ${counterWord}${isPlural}`}</span>
);
}
return resultsCount;
};
// Hooks
const useInputHack = (
setTagsString: (s: string) => void,
setCursorPosition: (n: number | undefined) => void
) => {
// HACK --- route around the lack of onChange
// See: https://github.com/elastic/eui/issues/5651
const inputNode = useRef<HTMLInputElement | null>(null);
useEffect(() => {
const cb = () => {
const s: string = inputNode.current?.value || "";
setTagsString(s);
setCursorPosition(getCursorPosition(inputNode));
};
const copiedNode = inputNode.current;
if (copiedNode) {
copiedNode.addEventListener("input", cb);
}
return () => {
if (copiedNode) {
copiedNode.removeEventListener("input", cb);
}
};
}, [inputNode, setTagsString, setCursorPosition]);
return inputNode;
};
const useSelectableOptions = (
tagSuggestions: TagSuggestionInstance[],
acceptSuggestion: (suggestion: TagSuggestionInstance) => void
) => {
const [options, setOptions] = useState<SelectableOption[]>(
tagSuggestions ? tagSuggestions.map(suggestionFormatter) : []
);
const onSelectableChange = (options: SelectableOption[]) => {
// Get the thing that just got "checked"
const clickedItem = options.find((option) => option.checked === "on");
if (clickedItem) {
acceptSuggestion(clickedItem.suggestion);
}
setOptions(options);
};
useEffect(() => {
// Update options when new set of suggestions are passed down
setOptions(tagSuggestions.map(suggestionFormatter));
}, [tagSuggestions, setOptions]);
return {
options,
onSelectableChange,
};
};
const TagSearch = ({
currentTag,
tagsString,
setTagsString,
acceptSuggestion,
tagSuggestions,
suggestionMode,
setCursorPosition,
}: TagSearchInterface) => {
// HACK --- route around the lack of onChange
const inputNode = useInputHack(setTagsString, setCursorPosition);
// Handling Suggestion Options
const { options, onSelectableChange } = useSelectableOptions(
tagSuggestions, // Gets turned into options
acceptSuggestion // Get triggered when an option is selected
);
// Using EuiInputPopover: https://elastic.github.io/eui/#/layout/popover
const [hasFocus, setHasFocus] = useState<boolean>(false);
// Props for EuiFieldSearch
const searchProps = {
value: tagsString,
inputRef: (node: HTMLInputElement | null) => {
// HTMLInputElement is hooked into useInputHack
inputNode.current = node;
},
onFocus: () => {
setHasFocus(true);
},
fullWidth: true,
placeholder: computePlaceholderText(tagSuggestions),
};
const resultsCount = generateResultsCount(
currentTag,
suggestionMode,
tagSuggestions
);
return (
<>
<EuiTitle size="xs">
<h2>Filter by Tags</h2>
</EuiTitle>
<EuiSelectable
onFocus={() => {
setHasFocus(true);
}}
onBlur={() => {
setHasFocus(false);
}}
searchable={true}
isPreFiltered={true}
searchProps={searchProps}
aria-label="Filter by "
onChange={onSelectableChange}
options={options}
singleSelection={true}
listProps={{ bordered: true }}
>
{(list, search) => {
return (
<EuiInputPopover
fullWidth
disableFocusTrap={true}
input={<>{search}</>}
isOpen={hasFocus}
closePopover={() => {
setHasFocus(false);
}}
>
{resultsCount}
{list}
</EuiInputPopover>
);
}}
</EuiSelectable>
</>
);
};
export default TagSearch;