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' | 'input' | 'searchBar' | 'view';
27 const styles: StyleRulesCallback<CssRules> = (theme: ArvadosTheme) => {
36 padding: `0px ${theme.spacing.unit}px`
48 type SearchBarDataProps = {
52 } & SearchBarAutocompleteViewDataProps;
54 interface SearchBarActionProps {
55 onSearch: (value: string) => any;
57 onSetView: (currentView: string) => void;
59 closeView: () => void;
62 type SearchBarProps = SearchBarDataProps & SearchBarActionProps & WithStyles<CssRules>;
64 interface SearchBarState {
68 interface RenderQueriesProps {
69 text: string | JSX.Element;
72 export const RecentQueriesItem = (props: RenderQueriesProps) => {
73 return <ListItem button>
74 <ListItemText secondary={props.text} />
79 export const RenderSavedQueries = (props: RenderQueriesProps) => {
80 return <ListItem button>
81 <ListItemText secondary={props.text} />
82 <ListItemSecondaryAction>
83 <Tooltip title="Remove">
84 <IconButton aria-label="Remove">
88 </ListItemSecondaryAction>
92 export const DEFAULT_SEARCH_DEBOUNCE = 1000;
94 export const SearchBarView = withStyles(styles)(
95 class extends React.Component<SearchBarProps> {
96 state: SearchBarState = {
103 const { classes, currentView, openView, closeView, open } = this.props;
104 return <ClickAwayListener onClickAway={() => closeView()}>
105 <Paper className={classes.container} >
106 <form onSubmit={this.handleSubmit} className={classes.searchBar}>
108 className={classes.input}
109 onChange={this.handleChange}
111 value={this.state.value}
113 disableUnderline={true}
114 onClick={() => openView()}
116 <InputAdornment position="end">
117 <Tooltip title='Search'>
125 <div className={classes.view}>
126 {open && this.getView(currentView)}
129 </ClickAwayListener>;
132 componentDidMount() {
133 this.setState({ value: this.props.searchValue });
136 componentWillReceiveProps(nextProps: SearchBarProps) {
137 if (nextProps.searchValue !== this.props.searchValue) {
138 this.setState({ value: nextProps.searchValue });
142 componentWillUnmount() {
143 clearTimeout(this.timeout);
146 getView = (currentView: string) => {
147 switch (currentView) {
148 case SearchView.BASIC:
149 return <SearchBarBasicView setView={this.props.onSetView} />;
150 case SearchView.ADVANCED:
151 return <SearchBarAdvancedView setView={this.props.onSetView} />;
152 case SearchView.AUTOCOMPLETE:
153 return <SearchBarAutocompleteView
154 searchResults={this.props.searchResults}
155 searchValue={this.props.searchValue} />;
157 return <SearchBarBasicView setView={this.props.onSetView} />;
161 handleSubmit = (event: React.FormEvent<HTMLFormElement>) => {
162 event.preventDefault();
163 clearTimeout(this.timeout);
164 this.props.onSearch(this.state.value);
167 handleChange = (event: React.ChangeEvent<HTMLInputElement>) => {
168 clearTimeout(this.timeout);
169 this.setState({ value: event.target.value });
170 this.timeout = window.setTimeout(
171 () => this.props.onSearch(this.state.value),
172 this.props.debounce || DEFAULT_SEARCH_DEBOUNCE
174 if (event.target.value.length > 0) {
175 this.props.onSetView(SearchView.AUTOCOMPLETE);
177 this.props.onSetView(SearchView.BASIC);