forked from cerebral/overmind
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.tsx
More file actions
132 lines (123 loc) · 2.82 KB
/
index.tsx
File metadata and controls
132 lines (123 loc) · 2.82 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
import * as React from 'react'
import {
InputElement,
SearchResult,
SearchResultItem,
Wrapper,
SearchText,
} from './elements'
function debounce(func, wait, immediate?) {
var timeout
return function() {
var context = this
var args = arguments
var later = function() {
timeout = null
if (!immediate) func.apply(context, args)
}
var callNow = immediate && !timeout
clearTimeout(timeout)
timeout = setTimeout(later, wait)
if (callNow) func.apply(context, args)
}
}
type TSearchResult = {
type: string
title: string
count: number
fileName: string
}
type State = {
query: string
isLoading: boolean
searchResult: TSearchResult[]
showSearchResult: boolean
}
class Search extends React.Component<{}, State> {
state = {
query: '',
isLoading: false,
searchResult: [],
showSearchResult: false,
}
componentDidMount() {
document.addEventListener('click', () => {
this.setState({
showSearchResult: false,
})
})
}
onChange = (event: React.ChangeEvent<HTMLInputElement>) => {
const query = event.target.value
this.setState({
query,
showSearchResult: query.length > 2,
isLoading: query.length > 2,
})
if (query.length < 3) {
return
}
this.search()
}
search = debounce(function() {
this.setState({
isLoading: true,
})
fetch('/backend/search?query=' + this.state.query)
.then((response) => response.json())
.then((searchResult) =>
this.setState({
searchResult,
isLoading: false,
})
)
}, 200)
renderSearchResultItem = (item: TSearchResult) => {
return (
<SearchResultItem
key={item.fileName}
href={`/${
item.type === 'guide' ? 'guides' : 'api'
}/${item.fileName.replace('.md', '')}`}
>
<span>{item.type}</span>
{item.title}
</SearchResultItem>
)
}
renderSearchResult() {
if (this.state.isLoading) {
return <SearchText>Searching...</SearchText>
}
if (this.state.searchResult.length === 0) {
return <SearchText>Sorry, no result :(</SearchText>
}
return this.state.searchResult.map(this.renderSearchResultItem)
}
onClick = () => {
if (this.state.searchResult.length) {
// Let closing click event go through first
setTimeout(() => {
this.setState({
showSearchResult: true,
})
}, 0)
}
}
render() {
return (
<Wrapper>
<InputElement
onClick={this.onClick}
placeholder="Search..."
onChange={this.onChange}
value={this.state.query}
/>
{this.state.showSearchResult ? (
<SearchResult>{this.renderSearchResult()}</SearchResult>
) : null}
</Wrapper>
)
}
}
export default Search