diff options
author | Samy Pesse <samypesse@gmail.com> | 2016-09-28 18:14:35 +0200 |
---|---|---|
committer | Samy Pesse <samypesse@gmail.com> | 2016-09-28 18:14:35 +0200 |
commit | 287c80b2e116572669f2a7073731ba7350e3619a (patch) | |
tree | 1572bab423b927e16bca4d56020aeec6442fe1a9 /packages/gitbook-plugin-search/src/components/Input.js | |
parent | 26fc021b900f6b28a180fa8360313b3193e6bf5e (diff) | |
download | gitbook-287c80b2e116572669f2a7073731ba7350e3619a.zip gitbook-287c80b2e116572669f2a7073731ba7350e3619a.tar.gz gitbook-287c80b2e116572669f2a7073731ba7350e3619a.tar.bz2 |
Start search plugin
Diffstat (limited to 'packages/gitbook-plugin-search/src/components/Input.js')
-rw-r--r-- | packages/gitbook-plugin-search/src/components/Input.js | 47 |
1 files changed, 47 insertions, 0 deletions
diff --git a/packages/gitbook-plugin-search/src/components/Input.js b/packages/gitbook-plugin-search/src/components/Input.js new file mode 100644 index 0000000..c4d1288 --- /dev/null +++ b/packages/gitbook-plugin-search/src/components/Input.js @@ -0,0 +1,47 @@ +const GitBook = require('gitbook-core'); +const { React } = GitBook; + +const search = require('../actions/search'); + +const DEBOUNCE_MS = 600; + +const SearchInput = React.createClass({ + propTypes: { + dispatch: React.PropTypes.func + }, + + onUpdateQuery(q) { + const { dispatch } = this.props; + + dispatch(search.query(q)); + }, + + onKeyDown(event) { + const { value } = event.currentTarget; + + if (this.debouncedSearch) { + clearTimeout(this.debouncedSearch); + } + + this.debouncedSearch = setTimeout(() => { + this.debouncedSearch = null; + this.onUpdateQuery(value); + }, DEBOUNCE_MS); + }, + + componentWillUnmount() { + if (this.debouncedSearch) { + clearTimeout(this.debouncedSearch); + } + }, + + render() { + return ( + <div className="Search/Input"> + <input type="text" placeholder="" onKeyDown={this.onKeyDown} /> + </div> + ); + } +}); + +module.exports = GitBook.connect(SearchInput); |