summaryrefslogtreecommitdiffstats
path: root/packages/gitbook-plugin-search/src/components/Input.js
blob: c4d128858fe5006d818b4377b7b504c89431feb5 (plain)
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
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);