-
Notifications
You must be signed in to change notification settings - Fork 182
/
search.js
129 lines (115 loc) · 3.3 KB
/
search.js
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
import React, {PureComponent} from 'react';
import PropTypes from 'prop-types';
import classNames from 'classnames';
import {SearchIcon} from '@jetbrains/ring-ui/components/icon';
import Input, {Size, Theme} from '@jetbrains/ring-ui/components/input/input';
import List from '@jetbrains/ring-ui/components/list/list';
import PopupMenu from '@jetbrains/ring-ui/components/popup-menu/popup-menu';
import Markdown from '@jetbrains/ring-ui/components/markdown/markdown';
import fuzzyHighlight from '@jetbrains/ring-ui/components/global/fuzzy-highlight';
import styles from './index.css';
function makeFilter(filter) {
const needle = filter.trim();
if (needle === '') {
return null;
}
return haystack => fuzzyHighlight(needle, haystack);
}
const filterItems = (items, filterFn) =>
items.
map(({title, ...rest}) => ({
filtered: filterFn(title),
...rest
})).
filter(({filtered}) => filtered.matched).
map(({filtered, ...rest}) => ({
title: (
<Markdown
inline={true}
source={filtered.highlight}
/>
),
...rest
}));
const groupListItem = name => ({
rgItemType: List.ListProps.Type.TITLE,
label: name === 'Docs' ? 'Ring UI' : name,
key: name
});
const linkListItem = ({url, title}) => {
return {
rgItemType: List.ListProps.Type.LINK,
href: url,
label: title,
key: url
};
};
class Search extends PureComponent {
state = {
filter: '',
open: false,
};
setFilter = e => this.setState({
filter: e.target.value,
open: true,
});
close = () => this.setState({open: false});
render() {
const {categories} = this.props;
const {filter} = this.state;
const filterFn = makeFilter(filter);
const filteredCategories = filterFn
? categories.
map(({items, ...rest}) => ({
items: filterItems(items, filterFn),
...rest
})).
filter(({items}) => items.length > 0)
: categories;
// [].concat(...arrs) === arrs.flatten()
const data = filteredCategories.length > 0 ? [].concat(
...filteredCategories.map(({name, items}) => [
groupListItem(name),
...items.map(linkListItem)
])
) : [groupListItem('Nothing found')];
return (
<div className={styles.search}>
<SearchIcon className={styles.searchIcon}/>
<Input
borderless
theme={Theme.DARK}
className={classNames(styles.searchInput, 'ring-js-shortcuts')}
autoFocus={true}
placeholder="Search"
value={filter}
onChange={this.setFilter}
/>
<PopupMenu
hidden={!this.state.open}
onCloseAttempt={this.close}
dontCloseOnAnchorClick
top={-8}
minWidth={PopupMenu.PopupProps.MinWidth.TARGET}
maxHeight={PopupMenu.PopupProps.MaxHeight.SCREEN}
directions={[PopupMenu.PopupProps.Directions.BOTTOM_RIGHT]}
className={styles.list}
shortcuts={true}
activateFirstItem
compact={true}
{...{data}}
/>
</div>
);
}
}
Search.propTypes = {
categories: PropTypes.arrayOf(PropTypes.shape({
name: PropTypes.string,
items: PropTypes.arrayOf(PropTypes.shape({
url: PropTypes.string,
title: PropTypes.string
}))
}))
};
export default Search;