1
0
mirror of https://github.com/kremalicious/blog.git synced 2024-06-10 20:00:09 +02:00
blog/src/components/molecules/Search.jsx

95 lines
2.1 KiB
React
Raw Normal View History

2018-08-25 11:15:40 +02:00
import React, { PureComponent, Fragment } from 'react'
2018-08-28 23:28:42 +02:00
import PropTypes from 'prop-types'
2018-08-25 11:15:40 +02:00
import Helmet from 'react-helmet'
2018-08-27 19:42:09 +02:00
import { CSSTransition } from 'react-transition-group'
2018-08-28 23:28:42 +02:00
import SearchInput from '../atoms/SearchInput'
import SearchButton from '../atoms/SearchButton'
import SearchResults from '../atoms/SearchResults'
2018-08-25 11:15:40 +02:00
import styles from './Search.module.scss'
class Search extends PureComponent {
constructor(props) {
super(props)
this.state = {
2018-08-28 23:28:42 +02:00
searchOpen: false,
query: '',
results: []
2018-08-25 11:15:40 +02:00
}
}
toggleSearch = () => {
this.setState(prevState => ({
searchOpen: !prevState.searchOpen
}))
}
2018-08-28 23:28:42 +02:00
closeSearch = () => {
this.setState({
searchOpen: false,
query: '',
results: []
})
}
2018-08-25 11:15:40 +02:00
isSearchOpen = () => this.state.searchOpen === true
2018-08-28 23:28:42 +02:00
getSearchResults(query) {
if (!query || !window.__LUNR__) return []
const lunrIndex = window.__LUNR__[this.props.lng]
const results = lunrIndex.index.search(query)
return results.map(({ ref }) => lunrIndex.store[ref])
}
search = event => {
const query = event.target.value
const results = this.getSearchResults(query)
this.setState({
results,
query
})
}
2018-08-25 11:15:40 +02:00
render() {
2018-08-28 23:28:42 +02:00
const { searchOpen, query, results } = this.state
2018-08-25 11:15:40 +02:00
return (
<Fragment>
<Helmet>
<body className={this.isSearchOpen() ? 'has-search-open' : null} />
</Helmet>
2018-08-28 23:28:42 +02:00
<SearchButton onClick={this.toggleSearch} />
2018-08-25 11:15:40 +02:00
2018-08-28 23:28:42 +02:00
{searchOpen && (
2018-08-27 19:42:09 +02:00
<CSSTransition
2018-08-28 23:28:42 +02:00
appear={searchOpen}
in={searchOpen}
2018-08-27 19:42:09 +02:00
timeout={200}
classNames={styles}
>
<section className={styles.search}>
2018-08-28 23:28:42 +02:00
<SearchInput
value={query}
onChange={this.search}
onToggle={this.closeSearch}
2018-08-27 19:42:09 +02:00
/>
</section>
</CSSTransition>
2018-08-25 11:15:40 +02:00
)}
2018-08-28 23:28:42 +02:00
{query && (
<SearchResults results={results} onClose={this.closeSearch} />
)}
2018-08-25 11:15:40 +02:00
</Fragment>
)
}
}
2018-08-28 23:28:42 +02:00
Search.propTypes = {
lng: PropTypes.string.isRequired
}
2018-08-25 11:15:40 +02:00
export default Search