search-basic-view
[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 export const renderRecentQueries = (text: string) => {
64     return <ListItem button>
65         <ListItemText secondary={text} />
66     </ListItem>;
67 };
68
69
70 export const renderSavedQueries = (text: string) => {
71     return <ListItem button>
72         <ListItemText secondary={text} />
73         <ListItemSecondaryAction>
74             <Tooltip title="Remove">
75                 <IconButton aria-label="Remove">
76                     <RemoveIcon />
77                 </IconButton>
78             </Tooltip>
79         </ListItemSecondaryAction>
80     </ListItem>;
81 };
82
83 export const DEFAULT_SEARCH_DEBOUNCE = 1000;
84
85 export const SearchBarView = withStyles(styles)(
86     class extends React.Component<SearchBarProps> {
87         state: SearchBarState = {
88             value: ""
89         };
90
91         timeout: number;
92
93         render() {
94             const { classes, currentView, openView, closeView, open } = this.props;
95             return <ClickAwayListener onClickAway={() => closeView()}>
96                 <Paper className={classes.container} >
97                     <form onSubmit={this.handleSubmit} className={classes.searchBar}>
98                         <Input
99                             className={classes.input}
100                             onChange={this.handleChange}
101                             placeholder="Search"
102                             value={this.state.value}
103                             fullWidth={true}
104                             disableUnderline={true}
105                             onClick={() => openView()}
106                             endAdornment={
107                                 <InputAdornment position="end">
108                                     <Tooltip title='Search'>
109                                         <IconButton>
110                                             <SearchIcon />
111                                         </IconButton>
112                                     </Tooltip>
113                                 </InputAdornment>
114                             } />
115                         {open && this.getView(currentView)}
116                     </form>
117                 </Paper >
118             </ClickAwayListener>;
119         }
120
121         componentDidMount() {
122             this.setState({ value: this.props.value });
123         }
124
125         componentWillReceiveProps(nextProps: SearchBarProps) {
126             if (nextProps.value !== this.props.value) {
127                 this.setState({ value: nextProps.value });
128             }
129         }
130
131         componentWillUnmount() {
132             clearTimeout(this.timeout);
133         }
134
135         getView = (currentView: string) => {
136             switch (currentView) {
137                 case SearchView.BASIC:
138                     return <SearchBarBasicView setView={this.props.onSetView} />;
139                 case SearchView.ADVANCED:
140                     return <SearchBarAdvancedView setView={this.props.onSetView} />;
141                 case SearchView.AUTOCOMPLETE:
142                     return <SearchBarAutocompleteView />;
143                 default:
144                     return <SearchBarBasicView setView={this.props.onSetView} />;
145             }
146         }
147
148         handleSubmit = (event: React.FormEvent<HTMLFormElement>) => {
149             event.preventDefault();
150             clearTimeout(this.timeout);
151             this.props.onSearch(this.state.value);
152         }
153
154         handleChange = (event: React.ChangeEvent<HTMLInputElement>) => {
155             clearTimeout(this.timeout);
156             this.setState({ value: event.target.value });
157             this.timeout = window.setTimeout(
158                 () => this.props.onSearch(this.state.value),
159                 this.props.debounce || DEFAULT_SEARCH_DEBOUNCE
160             );
161             if (event.target.value.length > 0) {
162                 this.props.onSetView(SearchView.AUTOCOMPLETE);
163             } else {
164                 this.props.onSetView(SearchView.BASIC);
165             }
166         }
167     }
168 );