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 ListItem, ListItemText, ListItemSecondaryAction,
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 import { SearchBarAdvanceFormData } from '~/store/search-bar/search-bar-actions';
26 type CssRules = 'container' | 'containerSearchViewOpened' | 'input' | 'view';
28 const styles: StyleRulesCallback<CssRules> = (theme: ArvadosTheme) => {
33 borderRadius: theme.spacing.unit / 2
35 containerSearchViewOpened: {
38 borderRadius: `${theme.spacing.unit / 2}px ${theme.spacing.unit / 2}px 0 0`
42 padding: `0px ${theme.spacing.unit}px`
52 type SearchBarDataProps = {
55 isPopoverOpen: boolean;
56 savedQueries: string[];
57 } & SearchBarAutocompleteViewDataProps;
59 interface SearchBarActionProps {
60 onSearch: (value: string) => any;
62 onSetView: (currentView: string) => void;
63 closeView: () => void;
64 saveRecentQuery: (query: string) => void;
65 loadRecentQueries: () => string[];
66 saveQuery: (data: SearchBarAdvanceFormData) => void;
67 deleteSavedQuery: (id: number) => void;
68 openSearchView: () => void;
71 type SearchBarProps = SearchBarDataProps & SearchBarActionProps & WithStyles<CssRules>;
73 interface SearchBarState {
77 interface RenderSavedQueriesProps {
78 text: string | JSX.Element;
80 deleteSavedQuery: (id: number) => void;
83 interface RenderRecentQueriesProps {
84 text: string | JSX.Element;
87 export const RecentQueriesItem = (props: RenderRecentQueriesProps) => {
88 return <ListItem button>
89 <ListItemText secondary={props.text} />
94 export const RenderSavedQueries = (props: RenderSavedQueriesProps) => {
95 return <ListItem button>
96 <ListItemText secondary={props.text} />
97 <ListItemSecondaryAction>
98 <Tooltip title="Remove">
99 <IconButton aria-label="Remove" onClick={() => props.deleteSavedQuery(props.id)}>
103 </ListItemSecondaryAction>
107 export const DEFAULT_SEARCH_DEBOUNCE = 1000;
109 export const SearchBarView = withStyles(styles)(
110 class extends React.Component<SearchBarProps> {
111 state: SearchBarState = {
118 const { classes, currentView, openSearchView, closeView, isPopoverOpen } = this.props;
119 return <ClickAwayListener onClickAway={() => closeView()}>
120 <Paper className={isPopoverOpen ? classes.containerSearchViewOpened : classes.container} >
121 <form onSubmit={this.handleSubmit}>
123 className={classes.input}
124 onChange={this.handleChange}
126 value={this.state.value}
128 disableUnderline={true}
129 onClick={openSearchView}
131 <InputAdornment position="end">
132 <Tooltip title='Search'>
140 <div className={classes.view}>
141 {isPopoverOpen && this.getView(currentView)}
144 </ClickAwayListener>;
147 componentDidMount() {
148 this.setState({ value: this.props.searchValue });
151 componentWillReceiveProps(nextProps: SearchBarProps) {
152 if (nextProps.searchValue !== this.props.searchValue) {
153 this.setState({ value: nextProps.searchValue });
157 componentWillUnmount() {
158 clearTimeout(this.timeout);
161 getView = (currentView: string) => {
162 const { onSetView, loadRecentQueries, savedQueries, deleteSavedQuery, searchValue, searchResults, saveQuery } = this.props;
163 switch (currentView) {
164 case SearchView.BASIC:
165 return <SearchBarBasicView setView={onSetView} recentQueries={loadRecentQueries} savedQueries={savedQueries} deleteSavedQuery={deleteSavedQuery} />;
166 case SearchView.ADVANCED:
167 return <SearchBarAdvancedView setView={onSetView} saveQuery={saveQuery}/>;
168 case SearchView.AUTOCOMPLETE:
169 return <SearchBarAutocompleteView
170 searchResults={searchResults}
171 searchValue={searchValue} />;
173 return <SearchBarBasicView setView={onSetView} recentQueries={loadRecentQueries} savedQueries={savedQueries} deleteSavedQuery={deleteSavedQuery} />;
177 handleSubmit = (event: React.FormEvent<HTMLFormElement>) => {
178 event.preventDefault();
179 clearTimeout(this.timeout);
180 this.props.saveRecentQuery(this.state.value);
181 this.props.onSearch(this.state.value);
182 this.props.loadRecentQueries();
185 handleChange = (event: React.ChangeEvent<HTMLInputElement>) => {
186 clearTimeout(this.timeout);
187 this.setState({ value: event.target.value });
188 this.timeout = window.setTimeout(
189 () => this.props.onSearch(this.state.value),
190 this.props.debounce || DEFAULT_SEARCH_DEBOUNCE
192 if (event.target.value.length > 0) {
193 this.props.onSetView(SearchView.AUTOCOMPLETE);
195 this.props.onSetView(SearchView.BASIC);