Merge branch 'master'
[arvados-workbench2.git] / src / views-components / search-bar / search-bar-view.tsx
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 import * as React from 'react';
6 import {
7     IconButton,
8     Paper,
9     StyleRulesCallback,
10     withStyles,
11     WithStyles,
12     Tooltip,
13     InputAdornment, Input,
14     ListItem, ListItemText, ListItemSecondaryAction,
15     ClickAwayListener
16 } from '@material-ui/core';
17 import SearchIcon from '@material-ui/icons/Search';
18 import { RemoveIcon } from '~/components/icon/icon';
19 import { SearchView } from '~/store/search-bar/search-bar-reducer';
20 import { SearchBarBasicView } from '~/views-components/search-bar/search-bar-basic-view';
21 import { SearchBarAdvancedView } from '~/views-components/search-bar/search-bar-advanced-view';
22 import { SearchBarAutocompleteView } from '~/views-components/search-bar/search-bar-autocomplete-view';
23
24 type CssRules = 'container' | 'input' | 'searchBar';
25
26 const styles: StyleRulesCallback<CssRules> = theme => {
27     return {
28         container: {
29             position: 'relative',
30             width: '100%',
31             borderRadius: '0px'
32         },
33         input: {
34             border: 'none',
35             padding: `0px ${theme.spacing.unit}px`
36         },
37         searchBar: {
38             height: '30px'
39         }
40     };
41 };
42
43 interface SearchBarDataProps {
44     value: string;
45     currentView: string;
46     open: boolean;
47 }
48
49 interface SearchBarActionProps {
50     onSearch: (value: string) => any;
51     debounce?: number;
52     onSetView: (currentView: string) => void;
53     openView: () => void;
54     closeView: () => void;
55 }
56
57 type SearchBarProps = SearchBarDataProps & SearchBarActionProps & WithStyles<CssRules>;
58
59 interface SearchBarState {
60     value: string;
61 }
62
63 interface RenderQueriesProps {
64     text: string;
65 }
66
67 export const RenderRecentQueries = (props: RenderQueriesProps) => {
68     return <ListItem button>
69         <ListItemText secondary={props.text} />
70     </ListItem>;
71 };
72
73
74 export const RenderSavedQueries = (props: RenderQueriesProps) => {
75     return <ListItem button>
76         <ListItemText secondary={props.text} />
77         <ListItemSecondaryAction>
78             <Tooltip title="Remove">
79                 <IconButton aria-label="Remove">
80                     <RemoveIcon />
81                 </IconButton>
82             </Tooltip>
83         </ListItemSecondaryAction>
84     </ListItem>;
85 };
86
87 export const DEFAULT_SEARCH_DEBOUNCE = 1000;
88
89 export const SearchBarView = withStyles(styles)(
90     class extends React.Component<SearchBarProps> {
91         state: SearchBarState = {
92             value: ""
93         };
94
95         timeout: number;
96
97         render() {
98             const { classes, currentView, openView, closeView, open } = this.props;
99             return <ClickAwayListener onClickAway={() => closeView()}>
100                 <Paper className={classes.container} >
101                     <form onSubmit={this.handleSubmit} className={classes.searchBar}>
102                         <Input
103                             className={classes.input}
104                             onChange={this.handleChange}
105                             placeholder="Search"
106                             value={this.state.value}
107                             fullWidth={true}
108                             disableUnderline={true}
109                             onClick={() => openView()}
110                             endAdornment={
111                                 <InputAdornment position="end">
112                                     <Tooltip title='Search'>
113                                         <IconButton>
114                                             <SearchIcon />
115                                         </IconButton>
116                                     </Tooltip>
117                                 </InputAdornment>
118                             } />
119                         {open && this.getView(currentView)}
120                     </form>
121                 </Paper >
122             </ClickAwayListener>;
123         }
124
125         componentDidMount() {
126             this.setState({ value: this.props.value });
127         }
128
129         componentWillReceiveProps(nextProps: SearchBarProps) {
130             if (nextProps.value !== this.props.value) {
131                 this.setState({ value: nextProps.value });
132             }
133         }
134
135         componentWillUnmount() {
136             clearTimeout(this.timeout);
137         }
138
139         getView = (currentView: string) => {
140             switch (currentView) {
141                 case SearchView.BASIC:
142                     return <SearchBarBasicView setView={this.props.onSetView} />;
143                 case SearchView.ADVANCED:
144                     return <SearchBarAdvancedView setView={this.props.onSetView} />;
145                 case SearchView.AUTOCOMPLETE:
146                     return <SearchBarAutocompleteView />;
147                 default:
148                     return <SearchBarBasicView setView={this.props.onSetView} />;
149             }
150         }
151
152         handleSubmit = (event: React.FormEvent<HTMLFormElement>) => {
153             event.preventDefault();
154             clearTimeout(this.timeout);
155             this.props.onSearch(this.state.value);
156         }
157
158         handleChange = (event: React.ChangeEvent<HTMLInputElement>) => {
159             clearTimeout(this.timeout);
160             this.setState({ value: event.target.value });
161             this.timeout = window.setTimeout(
162                 () => this.props.onSearch(this.state.value),
163                 this.props.debounce || DEFAULT_SEARCH_DEBOUNCE
164             );
165             if (event.target.value.length > 0) {
166                 this.props.onSetView(SearchView.AUTOCOMPLETE);
167             } else {
168                 this.props.onSetView(SearchView.BASIC);
169             }
170         }
171     }
172 );