forked from jupytercalpoly/jupyterlab-code-snippets
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFilterTools.tsx
236 lines (215 loc) · 6.97 KB
/
FilterTools.tsx
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
// Copyright (c) 2020, jupytercalpoly
// Distributed under the terms of the BSD-3 Clause License.
import { InputGroup, checkIcon } from '@jupyterlab/ui-components';
import React from 'react';
//import { filter } from '@lumino/algorithm';
interface IFilterSnippetProps {
languages: string[];
allTags: string[][]; // change to [[snip], [lang]]
onFilter: (
searchValue: string,
filterTags: string[],
selectedLangTags: string[]
) => void;
}
interface IFilterSnippetState {
show: boolean;
selectedTags: string[];
searchValue: string;
}
const FILTER_ARROW_UP = 'jp-codeSnippet-filter-arrow-up';
const FILTER_OPTION = 'jp-codeSnippet-filter-option';
const FILTER_TAGS = 'jp-codeSnippet-filter-tags';
const FILTER_TAG = 'jp-codeSnippet-filter-tag';
const FILTER_CHECK = 'jp-codeSnippet-filter-check';
const FILTER_TITLE = 'jp-codeSnippet-filter-title';
const FILTER_TOOLS = 'jp-codeSnippet-filterTools';
const FILTER_SEARCHBAR = 'jp-codeSnippet-searchbar';
const FILTER_SEARCHWRAPPER = 'jp-codeSnippet-searchwrapper';
const FILTER_CLASS = 'jp-codeSnippet-filter';
const FILTER_BUTTON = 'jp-codeSnippet-filter-btn';
export class FilterTools extends React.Component<
IFilterSnippetProps,
IFilterSnippetState
> {
constructor(props: IFilterSnippetProps) {
super(props);
this.state = { show: false, selectedTags: [], searchValue: '' }; //--> selectedTags & selectedLangTags
this.createFilterBox = this.createFilterBox.bind(this);
this.renderFilterOption = this.renderFilterOption.bind(this);
this.renderTags = this.renderTags.bind(this);
this.renderAppliedTag = this.renderAppliedTag.bind(this);
this.renderUnappliedTag = this.renderUnappliedTag.bind(this);
this.handleClick = this.handleClick.bind(this);
this.filterSnippets = this.filterSnippets.bind(this);
}
componentDidMount(): void {
this.setState({
show: false,
selectedTags: [],
searchValue: '',
});
}
componentDidUpdate(prevProps: IFilterSnippetProps): void {
if (prevProps !== this.props) {
// get all the tags together in one list
const flattenTags = this.props.allTags.reduce(
(accumulator, value) => accumulator.concat(value),
[]
);
this.setState((state) => ({
selectedTags: state.selectedTags
.filter((tag) => flattenTags.includes(tag))
.sort(),
}));
}
}
createFilterBox(): void {
const filterArrow = document.querySelector(`.${FILTER_ARROW_UP}`);
const filterOption = document.querySelector(`.${FILTER_OPTION}`);
filterArrow.classList.toggle('idle');
filterOption.classList.toggle('idle');
}
renderTags(tags: string[][], type: string): JSX.Element {
// get all the tags together in one list
const flattenTags = tags.reduce(
(accumulator, value) => accumulator.concat(value),
[]
);
return (
<div className={FILTER_TAGS}>
{flattenTags.sort().map((tag: string, index: number) => {
// language tags
if (type === 'language' && this.props.languages.includes(tag)) {
if (this.state.selectedTags.includes(tag)) {
return this.renderAppliedTag(tag, index.toString());
} else {
return this.renderUnappliedTag(tag, index.toString());
}
} else if (
// snippet tags
type === 'snippet' &&
!this.props.languages.includes(tag)
) {
if (this.state.selectedTags.includes(tag)) {
return this.renderAppliedTag(tag, index.toString());
} else {
return this.renderUnappliedTag(tag, index.toString());
}
}
})}
</div>
);
}
renderAppliedTag(tag: string, index: string): JSX.Element {
return (
<div
className={`${FILTER_TAG} tag applied-tag`}
id={'filter' + '-' + tag + '-' + index}
key={'filter' + '-' + tag + '-' + index}
>
<button onClick={this.handleClick}>{tag}</button>
<checkIcon.react
className={FILTER_CHECK}
tag="span"
elementPosition="center"
height="18px"
width="18px"
marginLeft="5px"
marginRight="-3px"
/>
</div>
);
}
renderUnappliedTag(tag: string, index: string): JSX.Element {
return (
<div
className={`${FILTER_TAG} tag unapplied-tag`}
id={'filter' + '-' + tag + '-' + index}
key={'filter' + '-' + tag + '-' + index}
>
<button onClick={this.handleClick}>{tag}</button>
</div>
);
}
handleClick(event: React.MouseEvent<HTMLButtonElement, MouseEvent>): void {
const target = event.target as HTMLElement;
const clickedTag = target.innerText;
const parent = target.parentElement;
this.setState(
(state) => ({
selectedTags: this.handleClickHelper(
parent,
state.selectedTags,
clickedTag
),
}),
this.filterSnippets
);
}
handleClickHelper(
parent: HTMLElement,
currentTags: string[],
clickedTag: string
): string[] {
if (parent.classList.contains('unapplied-tag')) {
parent.classList.replace('unapplied-tag', 'applied-tag');
currentTags.splice(-1, 0, clickedTag);
} else if (parent.classList.contains('applied-tag')) {
parent.classList.replace('applied-tag', 'unapplied-tag');
const idx = currentTags.indexOf(clickedTag);
currentTags.splice(idx, 1);
}
return currentTags.sort();
}
handleSearch = (event: React.ChangeEvent<HTMLInputElement>): void => {
this.setState({ searchValue: event.target.value }, this.filterSnippets);
};
filterSnippets(): void {
this.props.onFilter(
this.state.searchValue,
this.state.selectedTags,
this.state.selectedTags.filter((tag) =>
this.props.languages.includes(tag)
)
);
}
renderFilterOption(): JSX.Element {
//TODO: make lang tags/cell tags a dropdown
return (
<div className={`${FILTER_OPTION} idle`}>
<div className={FILTER_TITLE}>
<span>language tags</span>
</div>
{this.renderTags(this.props.allTags, 'language')}
<div className={FILTER_TITLE}>
<span>snippet tags</span>
</div>
{this.renderTags(this.props.allTags, 'snippet')}
</div>
);
}
render(): JSX.Element {
return (
<div className={FILTER_TOOLS}>
<div className={FILTER_SEARCHBAR}>
<InputGroup
className={FILTER_SEARCHWRAPPER}
type="text"
placeholder="SEARCH SNIPPETS"
onChange={this.handleSearch}
rightIcon="ui-components:search"
value={this.state.searchValue}
/>
</div>
<div className={FILTER_CLASS}>
<button className={FILTER_BUTTON} onClick={this.createFilterBox}>
Filter By Tags
</button>
<div className={`${FILTER_ARROW_UP} idle`}></div>
{this.renderFilterOption()}
</div>
</div>
);
}
}