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';
25 type CssRules = 'container' | 'containerSearchViewOpened' | 'input' | 'searchBar' | 'view';
27 const styles: StyleRulesCallback<CssRules> = (theme: ArvadosTheme) => {
32 borderRadius: theme.spacing.unit / 4
34 containerSearchViewOpened: {
37 borderRadius: `${theme.spacing.unit / 4}px ${theme.spacing.unit / 4}px 0 0`
41 padding: `0px ${theme.spacing.unit}px`
53 type SearchBarDataProps = {
57 } & SearchBarAutocompleteViewDataProps;
59 interface SearchBarActionProps {
60 onSearch: (value: string) => any;
62 onSetView: (currentView: string) => void;
64 closeView: () => void;
65 saveQuery: (query: string) => void;
66 loadQueries: () => string[];
69 type SearchBarProps = SearchBarDataProps & SearchBarActionProps & WithStyles<CssRules>;
71 interface SearchBarState {
75 interface RenderQueriesProps {
76 text: string | JSX.Element;
79 export const RecentQueriesItem = (props: RenderQueriesProps) => {
80 return <ListItem button>
81 <ListItemText secondary={props.text} />
86 export const RenderSavedQueries = (props: RenderQueriesProps) => {
87 return <ListItem button>
88 <ListItemText secondary={props.text} />
89 <ListItemSecondaryAction>
90 <Tooltip title="Remove">
91 <IconButton aria-label="Remove">
95 </ListItemSecondaryAction>
99 export const DEFAULT_SEARCH_DEBOUNCE = 1000;
101 export const SearchBarView = withStyles(styles)(
102 class extends React.Component<SearchBarProps> {
103 state: SearchBarState = {
110 const { classes, currentView, openView, closeView, open } = this.props;
111 return <ClickAwayListener onClickAway={() => closeView()}>
112 <Paper className={open ? classes.containerSearchViewOpened : classes.container} >
113 <form onSubmit={this.handleSubmit} className={classes.searchBar}>
115 className={classes.input}
116 onChange={this.handleChange}
118 value={this.state.value}
120 disableUnderline={true}
121 onClick={() => openView()}
123 <InputAdornment position="end">
124 <Tooltip title='Search'>
132 <div className={classes.view}>
133 {open && this.getView(currentView)}
136 </ClickAwayListener>;
139 componentDidMount() {
140 this.setState({ value: this.props.searchValue });
143 componentWillReceiveProps(nextProps: SearchBarProps) {
144 if (nextProps.searchValue !== this.props.searchValue) {
145 this.setState({ value: nextProps.searchValue });
149 componentWillUnmount() {
150 clearTimeout(this.timeout);
153 getView = (currentView: string) => {
154 switch (currentView) {
155 case SearchView.BASIC:
156 return <SearchBarBasicView setView={this.props.onSetView} recentQueries={this.props.loadQueries} />;
157 case SearchView.ADVANCED:
158 return <SearchBarAdvancedView setView={this.props.onSetView} />;
159 case SearchView.AUTOCOMPLETE:
160 return <SearchBarAutocompleteView
161 searchResults={this.props.searchResults}
162 searchValue={this.props.searchValue} />;
164 return <SearchBarBasicView setView={this.props.onSetView} recentQueries={this.props.loadQueries} />;
168 handleSubmit = (event: React.FormEvent<HTMLFormElement>) => {
169 event.preventDefault();
170 clearTimeout(this.timeout);
171 this.props.saveQuery(this.state.value);
172 this.props.onSearch(this.state.value);
173 this.props.loadQueries();
176 handleChange = (event: React.ChangeEvent<HTMLInputElement>) => {
177 clearTimeout(this.timeout);
178 this.setState({ value: event.target.value });
179 this.timeout = window.setTimeout(
180 () => this.props.onSearch(this.state.value),
181 this.props.debounce || DEFAULT_SEARCH_DEBOUNCE
183 if (event.target.value.length > 0) {
184 this.props.onSetView(SearchView.AUTOCOMPLETE);
186 this.props.onSetView(SearchView.BASIC);