1
0
Fork 0
mirror of https://github.com/betagouv/mon-entreprise synced 2025-02-08 18:45:01 +00:00
mon-entreprise/source/components/SearchButton.js
Laurent Bossavit 977df3f8a5 👽 SearchBar
2018-04-09 11:40:29 +02:00

58 lines
1.5 KiB
JavaScript

import React, { Component } from 'react'
import { Trans, translate } from 'react-i18next'
import { connect } from 'react-redux'
import './SearchButton.css'
import Overlay from './Overlay'
import { SearchBar } from './pages/RulesList'
import withColours from 'Components/withColours'
@connect(
state => ({
flatRules: state.flatRules
})
)
@withColours
@translate()
export default class SearchButton extends Component {
componentDidMount() {
// removeEventListener will need the exact same function instance
this.boundHandleKeyDown = this.handleKeyDown.bind(this)
window.addEventListener('keydown', this.boundHandleKeyDown)
}
handleKeyDown(e) {
if (!(e.ctrlKey && e.key === 'p')) return
this.setState({ visible: true })
e.preventDefault()
e.stopPropagation()
return false
}
componentWillUnmount() {
window.removeEventListener('keydown', this.boundHandleKeyDown)
}
state = {
visible: false
}
close = () => this.setState({ visible: false })
render() {
let { flatRules } = this.props
return (
<div id="searchButton">
{this.state.visible ? (
<Overlay onOuterClick={this.close}>
<h2><Trans>Chercher une règle</Trans></h2>
<SearchBar showDefaultList={false} finally={this.close} rules={flatRules} />
</Overlay>
) : (
<button onClick={() => this.setState({ visible: true })}>
<i
style={{ color: this.props.colours.colour }}
className="fa fa-search"
aria-hidden="true"
/>
</button>
)}
</div>
)
}
}