summaryrefslogtreecommitdiff
path: root/src/components/SearchForm.js
blob: 6f626c213853381c063a2d1772f0f3f9776d7a1c (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
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
import React from "react";
import PropTypes from "prop-types";

import Button from "@mui/material/Button";
import ClearIcon from "@mui/icons-material/Clear";
import FormControl from "@mui/material/FormControl";
import IconButton from "@mui/material/IconButton";
import InputAdornment from "@mui/material/InputAdornment";
import MenuItem from "@mui/material/MenuItem";
import Select from "@mui/material/Select";
import TextField from "@mui/material/TextField";

class SearchForm extends React.Component {
    static propTypes = {
        filter: PropTypes.func.isRequired
    };

    constructor(props) {
        super(props);
        // NOTE: This state is for UI only, List state's "filter" is used for requests.
        this.state = {
            searchField: "port",
            searchValue: ""
        };

        this.clearSearch = this.clearSearch.bind(this);
        this.submitSearch = this.submitSearch.bind(this);
    }

    clearSearch(_) {
        this.setState({ searchValue: "" });
        this.props.filter(null, null);
    }

    submitSearch() {
        // e.preventDefault();
        if (this.state.searchValue === "") this.clearSearch();
        else this.props.filter(this.state.searchField, this.state.searchValue);
    }

    render() {
        return (
            <div id="search">
                <TextField
                    fullWidth
                    size="small"
                    sx={{ width: 400 }}
                    value={this.state.searchValue}
                    placeholder="Search..."
                    onChange={event => {
                        this.setState({
                            searchValue: event.target.value
                        });
                    }}
                    onKeyDown={event => {
                        if (event.key === "Enter") this.submitSearch();
                        if (event.key === "Escape") this.clearSearch();
                    }}
                    InputProps={{
                        endAdornment: (
                            <InputAdornment position="end">
                                {this.state.searchValue !== "" && (
                                    <ClearIcon
                                        variant="clickable"
                                        onClick={this.clearSearch}
                                    />
                                )}
                            </InputAdornment>
                        )
                    }}
                />
                <Select
                    size="small"
                    sx={{ width: 200, marginLeft: "1em" }}
                    value={this.state.searchField}
                    onChange={event => {
                        this.setState({
                            searchField: event.target.value
                        });
                    }}
                >
                    <MenuItem value="port">Port</MenuItem>
                    <MenuItem value="domain">Domain</MenuItem>
                    <MenuItem value="ip">IP</MenuItem>
                    <MenuItem value="asn">ASN</MenuItem>
                    <MenuItem value="asn_country_code">
                        ASN Country Code
                    </MenuItem>
                </Select>
                <Button
                    variant="contained"
                    sx={{ marginLeft: "1em" }}
                    onClick={this.submitSearch}
                >
                    Search
                </Button>
            </div>
        );
    }
}

export default SearchForm;