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) {
136 if (props.currentView === SearchView.ADVANCED) {
139 props.setAdvancedDataFromSearchValue(props.searchValue);
140 props.onSetView(SearchView.ADVANCED);
143 props.setAdvancedDataFromSearchValue(props.searchValue);
144 props.onSetView(SearchView.ADVANCED);
148 export const SearchBarView = withStyles(styles)(
149 class extends React.Component<SearchBarViewProps> {
151 debouncedSearch = debounce(() => {
152 this.props.onSearch(this.props.searchValue);
155 handleChange = (event: React.ChangeEvent<HTMLInputElement>) => {
156 this.debouncedSearch();
157 this.props.onChange(event);
160 handleSubmit = (event: React.FormEvent<HTMLFormElement>) => {
161 this.debouncedSearch.clear();
162 this.props.onSubmit(event);
165 componentWillUnmount() {
166 this.debouncedSearch.clear();
170 const { children, ...props } = this.props;
171 const { classes, isPopoverOpen } = this.props;
176 <Backdrop onClick={props.closeView} />}
178 <Paper className={isPopoverOpen ? classes.containerSearchViewOpened : classes.container} >
179 <form onSubmit={this.handleSubmit}>
181 className={classes.input}
182 onChange={this.handleChange}
184 value={props.searchValue}
186 disableUnderline={true}
187 onClick={e => handleInputClick(e, props)}
188 onKeyDown={e => handleKeyDown(e, props)}
190 <InputAdornment position="start">
191 <Tooltip title='Search'>
192 <IconButton type="submit">
199 <InputAdornment position="end">
200 <Tooltip title='Advanced search'>
201 <IconButton onClick={e => handleDropdownClick(e, props)}>
202 <ArrowDropDownIcon />
208 <div className={classes.view}>
209 {isPopoverOpen && getView({ ...props })}
217 const getView = (props: SearchBarViewProps) => {
218 switch (props.currentView) {
219 case SearchView.AUTOCOMPLETE:
220 return <SearchBarAutocompleteView
221 navigateTo={props.navigateTo}
222 searchResults={props.searchResults}
223 searchValue={props.searchValue}
224 selectedItem={props.selectedItem} />;
225 case SearchView.ADVANCED:
226 return <SearchBarAdvancedView
227 closeAdvanceView={props.closeAdvanceView}
229 saveQuery={props.saveQuery} />;
231 return <SearchBarBasicView
232 onSetView={props.onSetView}
233 onSearch={props.onSearch}
234 loadRecentQueries={props.loadRecentQueries}
235 savedQueries={props.savedQueries}
236 deleteSavedQuery={props.deleteSavedQuery}
237 editSavedQuery={props.editSavedQuery}
238 selectedItem={props.selectedItem} />;
242 const Backdrop = withStyles<'backdrop'>(theme => ({
249 zIndex: theme.zIndex.modal
252 ({ classes, ...props }: WithStyles<'backdrop'> & React.HTMLProps<HTMLDivElement>) =>
253 <div className={classes.backdrop} {...props} />);