1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
5 import * as React from 'react';
13 InputAdornment, Input,
14 } from '@material-ui/core';
15 import SearchIcon from '@material-ui/icons/Search';
16 import ArrowDropDownIcon from '@material-ui/icons/ArrowDropDown';
17 import { ArvadosTheme } from '~/common/custom-theme';
18 import { SearchView } from '~/store/search-bar/search-bar-reducer';
21 SearchBarBasicViewDataProps,
22 SearchBarBasicViewActionProps
23 } from '~/views-components/search-bar/search-bar-basic-view';
25 SearchBarAutocompleteView,
26 SearchBarAutocompleteViewDataProps,
27 SearchBarAutocompleteViewActionProps
28 } from '~/views-components/search-bar/search-bar-autocomplete-view';
30 SearchBarAdvancedView,
31 SearchBarAdvancedViewDataProps,
32 SearchBarAdvancedViewActionProps
33 } from '~/views-components/search-bar/search-bar-advanced-view';
34 import { KEY_CODE_DOWN, KEY_CODE_ESC, KEY_CODE_UP, KEY_ENTER } from "~/common/codes";
35 import { debounce } from 'debounce';
37 type CssRules = 'container' | 'containerSearchViewOpened' | 'input' | 'view';
39 const styles: StyleRulesCallback<CssRules> = (theme: ArvadosTheme) => {
44 borderRadius: theme.spacing.unit / 2,
45 zIndex: theme.zIndex.modal,
47 containerSearchViewOpened: {
50 borderRadius: `${theme.spacing.unit / 2}px ${theme.spacing.unit / 2}px 0 0`,
51 zIndex: theme.zIndex.modal,
65 export type SearchBarDataProps = SearchBarViewDataProps
66 & SearchBarAutocompleteViewDataProps
67 & SearchBarAdvancedViewDataProps
68 & SearchBarBasicViewDataProps;
70 interface SearchBarViewDataProps {
73 isPopoverOpen: boolean;
77 export type SearchBarActionProps = SearchBarViewActionProps
78 & SearchBarAutocompleteViewActionProps
79 & SearchBarAdvancedViewActionProps
80 & SearchBarBasicViewActionProps;
82 interface SearchBarViewActionProps {
83 onChange: (event: React.ChangeEvent<HTMLInputElement>) => void;
84 onSubmit: (event: React.FormEvent<HTMLFormElement>) => void;
85 onSetView: (currentView: string) => void;
86 closeView: () => void;
87 openSearchView: () => void;
88 loadRecentQueries: () => string[];
91 setAdvancedDataFromSearchValue: (search: string) => void;
94 type SearchBarViewProps = SearchBarDataProps & SearchBarActionProps & WithStyles<CssRules>;
96 const handleKeyDown = (e: React.KeyboardEvent, props: SearchBarViewProps) => {
97 if (e.keyCode === KEY_CODE_DOWN) {
99 if (!props.isPopoverOpen) {
100 props.onSetView(SearchView.AUTOCOMPLETE);
101 props.openSearchView();
105 } else if (e.keyCode === KEY_CODE_UP) {
108 } else if (e.keyCode === KEY_CODE_ESC) {
111 } else if (e.keyCode === KEY_ENTER) {
112 if (props.currentView === SearchView.BASIC) {
114 props.onSearch(props.selectedItem.query);
115 } else if (props.currentView === SearchView.AUTOCOMPLETE) {
116 if (props.selectedItem.id !== props.searchValue) {
118 props.navigateTo(props.selectedItem.id);
124 const handleInputClick = (e: React.MouseEvent, props: SearchBarViewProps) => {
125 if (props.searchValue) {
126 props.onSetView(SearchView.AUTOCOMPLETE);
127 props.openSearchView();
133 const handleDropdownClick = (e: React.MouseEvent, props: SearchBarViewProps) => {
135 if (props.isPopoverOpen && props.currentView === SearchView.ADVANCED) {
138 props.setAdvancedDataFromSearchValue(props.searchValue);
139 props.onSetView(SearchView.ADVANCED);
143 export const SearchBarView = withStyles(styles)(
144 class extends React.Component<SearchBarViewProps> {
146 debouncedSearch = debounce(() => {
147 this.props.onSearch(this.props.searchValue);
150 handleChange = (event: React.ChangeEvent<HTMLInputElement>) => {
151 this.debouncedSearch();
152 this.props.onChange(event);
155 handleSubmit = (event: React.FormEvent<HTMLFormElement>) => {
156 this.debouncedSearch.clear();
157 this.props.onSubmit(event);
160 componentWillUnmount() {
161 this.debouncedSearch.clear();
165 const { children, ...props } = this.props;
166 const { classes, isPopoverOpen } = this.props;
171 <Backdrop onClick={props.closeView} />}
173 <Paper className={isPopoverOpen ? classes.containerSearchViewOpened : classes.container} >
174 <form onSubmit={this.handleSubmit}>
176 className={classes.input}
177 onChange={this.handleChange}
179 value={props.searchValue}
181 disableUnderline={true}
182 onClick={e => handleInputClick(e, props)}
183 onKeyDown={e => handleKeyDown(e, props)}
185 <InputAdornment position="start">
186 <Tooltip title='Search'>
187 <IconButton type="submit">
194 <InputAdornment position="end">
195 <Tooltip title='Advanced search'>
196 <IconButton onClick={e => handleDropdownClick(e, props)}>
197 <ArrowDropDownIcon />
203 <div className={classes.view}>
204 {isPopoverOpen && getView({ ...props })}
212 const getView = (props: SearchBarViewProps) => {
213 switch (props.currentView) {
214 case SearchView.AUTOCOMPLETE:
215 return <SearchBarAutocompleteView
216 navigateTo={props.navigateTo}
217 searchResults={props.searchResults}
218 searchValue={props.searchValue}
219 selectedItem={props.selectedItem} />;
220 case SearchView.ADVANCED:
221 return <SearchBarAdvancedView
222 closeAdvanceView={props.closeAdvanceView}
224 saveQuery={props.saveQuery} />;
226 return <SearchBarBasicView
227 onSetView={props.onSetView}
228 onSearch={props.onSearch}
229 loadRecentQueries={props.loadRecentQueries}
230 savedQueries={props.savedQueries}
231 deleteSavedQuery={props.deleteSavedQuery}
232 editSavedQuery={props.editSavedQuery}
233 selectedItem={props.selectedItem} />;
237 const Backdrop = withStyles<'backdrop'>(theme => ({
244 zIndex: theme.zIndex.modal
247 ({ classes, ...props }: WithStyles<'backdrop'> & React.HTMLProps<HTMLDivElement>) =>
248 <div className={classes.backdrop} {...props} />);