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,
15 } from '@material-ui/core';
16 import SearchIcon from '@material-ui/icons/Search';
17 import ArrowDropDownIcon from '@material-ui/icons/ArrowDropDown';
18 import { ArvadosTheme } from '~/common/custom-theme';
19 import { SearchView } from '~/store/search-bar/search-bar-reducer';
22 SearchBarBasicViewDataProps,
23 SearchBarBasicViewActionProps
24 } from '~/views-components/search-bar/search-bar-basic-view';
26 SearchBarAutocompleteView,
27 SearchBarAutocompleteViewDataProps,
28 SearchBarAutocompleteViewActionProps
29 } from '~/views-components/search-bar/search-bar-autocomplete-view';
31 SearchBarAdvancedView,
32 SearchBarAdvancedViewDataProps,
33 SearchBarAdvancedViewActionProps
34 } from '~/views-components/search-bar/search-bar-advanced-view';
35 import { KEY_CODE_DOWN, KEY_CODE_ESC, KEY_CODE_UP, KEY_ENTER } from "~/common/codes";
37 type CssRules = 'container' | 'containerSearchViewOpened' | 'input' | 'view';
39 const styles: StyleRulesCallback<CssRules> = (theme: ArvadosTheme) => {
44 borderRadius: theme.spacing.unit / 2
46 containerSearchViewOpened: {
49 borderRadius: `${theme.spacing.unit / 2}px ${theme.spacing.unit / 2}px 0 0`
63 export type SearchBarDataProps = SearchBarViewDataProps
64 & SearchBarAutocompleteViewDataProps
65 & SearchBarAdvancedViewDataProps
66 & SearchBarBasicViewDataProps;
68 interface SearchBarViewDataProps {
71 isPopoverOpen: boolean;
75 export type SearchBarActionProps = SearchBarViewActionProps
76 & SearchBarAutocompleteViewActionProps
77 & SearchBarAdvancedViewActionProps
78 & SearchBarBasicViewActionProps;
80 interface SearchBarViewActionProps {
81 onChange: (event: React.ChangeEvent<HTMLInputElement>) => void;
82 onSubmit: (event: React.FormEvent<HTMLFormElement>) => void;
83 onSetView: (currentView: string) => void;
84 closeView: () => void;
85 openSearchView: () => void;
86 loadRecentQueries: () => string[];
89 setAdvancedDataFromSearchValue: (search: string) => void;
92 type SearchBarViewProps = SearchBarDataProps & SearchBarActionProps & WithStyles<CssRules>;
94 const handleKeyDown = (e: React.KeyboardEvent, props: SearchBarViewProps) => {
95 if (e.keyCode === KEY_CODE_DOWN) {
97 if (!props.isPopoverOpen) {
98 props.onSetView(SearchView.AUTOCOMPLETE);
99 props.openSearchView();
103 } else if (e.keyCode === KEY_CODE_UP) {
106 } else if (e.keyCode === KEY_CODE_ESC) {
109 } else if (e.keyCode === KEY_ENTER) {
110 if (props.currentView === SearchView.BASIC) {
112 props.onSearch(props.selectedItem.query);
113 } else if (props.currentView === SearchView.AUTOCOMPLETE) {
114 if (props.selectedItem.id !== props.searchValue) {
116 props.navigateTo(props.selectedItem.id);
122 const handleInputClick = (e: React.MouseEvent, props: SearchBarViewProps) => {
123 if (props.searchValue) {
124 props.onSetView(SearchView.AUTOCOMPLETE);
125 props.openSearchView();
131 const handleDropdownClick = (e: React.MouseEvent, props: SearchBarViewProps) => {
133 if (props.isPopoverOpen) {
134 if (props.currentView === SearchView.ADVANCED) {
137 props.setAdvancedDataFromSearchValue(props.searchValue);
138 props.onSetView(SearchView.ADVANCED);
141 props.setAdvancedDataFromSearchValue(props.searchValue);
142 props.onSetView(SearchView.ADVANCED);
146 export const SearchBarView = withStyles(styles)(
147 (props : SearchBarViewProps) => {
148 const { classes, isPopoverOpen } = props;
150 <ClickAwayListener onClickAway={props.closeView}>
151 <Paper className={isPopoverOpen ? classes.containerSearchViewOpened : classes.container} >
152 <form onSubmit={props.onSubmit}>
154 className={classes.input}
155 onChange={props.onChange}
157 value={props.searchValue}
159 disableUnderline={true}
160 onClick={e => handleInputClick(e, props)}
161 onKeyDown={e => handleKeyDown(e, props)}
163 <InputAdornment position="start">
164 <Tooltip title='Search'>
165 <IconButton type="submit">
172 <InputAdornment position="end">
173 <Tooltip title='Advanced search'>
174 <IconButton onClick={e => handleDropdownClick(e, props)}>
175 <ArrowDropDownIcon />
181 <div className={classes.view}>
182 {isPopoverOpen && getView({...props})}
190 const getView = (props: SearchBarViewProps) => {
191 switch (props.currentView) {
192 case SearchView.AUTOCOMPLETE:
193 return <SearchBarAutocompleteView
194 navigateTo={props.navigateTo}
195 searchResults={props.searchResults}
196 searchValue={props.searchValue}
197 selectedItem={props.selectedItem} />;
198 case SearchView.ADVANCED:
199 return <SearchBarAdvancedView
200 closeAdvanceView={props.closeAdvanceView}
202 saveQuery={props.saveQuery} />;
204 return <SearchBarBasicView
205 onSetView={props.onSetView}
206 onSearch={props.onSearch}
207 loadRecentQueries={props.loadRecentQueries}
208 savedQueries={props.savedQueries}
209 deleteSavedQuery={props.deleteSavedQuery}
210 editSavedQuery={props.editSavedQuery}
211 selectedItem={props.selectedItem} />;