init all fields, create form fields file, modify collection tag form
[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, SearchBarAutocompleteViewDataProps } from '~/views-components/search-bar/search-bar-autocomplete-view';
23 import { ArvadosTheme } from '~/common/custom-theme';
24
25 type CssRules = 'container' | 'input' | 'searchBar' | 'view';
26
27 const styles: StyleRulesCallback<CssRules> = (theme: ArvadosTheme) => {
28     return {
29         container: {
30             position: 'relative',
31             width: '100%',
32             borderRadius: '0px'
33         },
34         input: {
35             border: 'none',
36             padding: `0px ${theme.spacing.unit}px`
37         },
38         searchBar: {
39             height: '30px'
40         },
41         view: {
42             position: 'absolute',
43             width: '100%'
44         }
45     };
46 };
47
48 type SearchBarDataProps = {
49     searchValue: string;
50     currentView: string;
51     open: boolean;
52 } & SearchBarAutocompleteViewDataProps;
53
54 interface SearchBarActionProps {
55     onSearch: (value: string) => any;
56     debounce?: number;
57     onSetView: (currentView: string) => void;
58     openView: () => void;
59     closeView: () => void;
60 }
61
62 type SearchBarProps = SearchBarDataProps & SearchBarActionProps & WithStyles<CssRules>;
63
64 interface SearchBarState {
65     value: string;
66 }
67
68 interface RenderQueriesProps {
69     text: string | JSX.Element;
70 }
71
72 export const RecentQueriesItem = (props: RenderQueriesProps) => {
73     return <ListItem button>
74         <ListItemText secondary={props.text} />
75     </ListItem>;
76 };
77
78
79 export const RenderSavedQueries = (props: RenderQueriesProps) => {
80     return <ListItem button>
81         <ListItemText secondary={props.text} />
82         <ListItemSecondaryAction>
83             <Tooltip title="Remove">
84                 <IconButton aria-label="Remove">
85                     <RemoveIcon />
86                 </IconButton>
87             </Tooltip>
88         </ListItemSecondaryAction>
89     </ListItem>;
90 };
91
92 export const DEFAULT_SEARCH_DEBOUNCE = 1000;
93
94 export const SearchBarView = withStyles(styles)(
95     class extends React.Component<SearchBarProps> {
96         state: SearchBarState = {
97             value: ""
98         };
99
100         timeout: number;
101
102         render() {
103             const { classes, currentView, openView, closeView, open } = this.props;
104             return <ClickAwayListener onClickAway={() => closeView()}>
105                 <Paper className={classes.container} >
106                     <form onSubmit={this.handleSubmit} className={classes.searchBar}>
107                         <Input
108                             className={classes.input}
109                             onChange={this.handleChange}
110                             placeholder="Search"
111                             value={this.state.value}
112                             fullWidth={true}
113                             disableUnderline={true}
114                             onClick={() => openView()}
115                             endAdornment={
116                                 <InputAdornment position="end">
117                                     <Tooltip title='Search'>
118                                         <IconButton>
119                                             <SearchIcon />
120                                         </IconButton>
121                                     </Tooltip>
122                                 </InputAdornment>
123                             } />
124                     </form>
125                     <div className={classes.view}>
126                         {open && this.getView(currentView)}
127                     </div>
128                 </Paper>
129             </ClickAwayListener>;
130         }
131
132         componentDidMount() {
133             this.setState({ value: this.props.searchValue });
134         }
135
136         componentWillReceiveProps(nextProps: SearchBarProps) {
137             if (nextProps.searchValue !== this.props.searchValue) {
138                 this.setState({ value: nextProps.searchValue });
139             }
140         }
141
142         componentWillUnmount() {
143             clearTimeout(this.timeout);
144         }
145
146         getView = (currentView: string) => {
147             switch (currentView) {
148                 case SearchView.BASIC:
149                     return <SearchBarBasicView setView={this.props.onSetView} />;
150                 case SearchView.ADVANCED:
151                     return <SearchBarAdvancedView setView={this.props.onSetView} />;
152                 case SearchView.AUTOCOMPLETE:
153                     return <SearchBarAutocompleteView 
154                                 searchResults={this.props.searchResults} 
155                                 searchValue={this.props.searchValue} />;
156                 default:
157                     return <SearchBarBasicView setView={this.props.onSetView} />;
158             }
159         }
160
161         handleSubmit = (event: React.FormEvent<HTMLFormElement>) => {
162             event.preventDefault();
163             clearTimeout(this.timeout);
164             this.props.onSearch(this.state.value);
165         }
166
167         handleChange = (event: React.ChangeEvent<HTMLInputElement>) => {
168             clearTimeout(this.timeout);
169             this.setState({ value: event.target.value });
170             this.timeout = window.setTimeout(
171                 () => this.props.onSearch(this.state.value),
172                 this.props.debounce || DEFAULT_SEARCH_DEBOUNCE
173             );
174             if (event.target.value.length > 0) {
175                 this.props.onSetView(SearchView.AUTOCOMPLETE);
176             } else {
177                 this.props.onSetView(SearchView.BASIC);
178             }
179         }
180     }
181 );