X-Git-Url: https://git.arvados.org/arvados-workbench2.git/blobdiff_plain/23f12738ed71ef736cb9c85a530cb4795c760d8f..5b44e10c627fa07287d1700c554ee1231ca7733b:/src/components/search-bar/search-bar.tsx diff --git a/src/components/search-bar/search-bar.tsx b/src/components/search-bar/search-bar.tsx index ebb7f5e9..2036a662 100644 --- a/src/components/search-bar/search-bar.tsx +++ b/src/components/search-bar/search-bar.tsx @@ -11,10 +11,15 @@ import { WithStyles, Tooltip, InputAdornment, Input, - List, ListItem, ListItemText, ListItemSecondaryAction + List, ListItem, ListItemText, ListItemSecondaryAction, Button } from '@material-ui/core'; import SearchIcon from '@material-ui/icons/Search'; import { RemoveIcon } from '~/components/icon/icon'; +import { connect } from 'react-redux'; +import { RootState } from '~/store/store'; +import { Dispatch } from 'redux'; +import { goToView } from '~/store/structured-search/structured-search-actions'; +import { SearchView } from '~/store/structured-search/structured-search-reducer'; type CssRules = 'container' | 'input' | 'advanced' | 'searchQueryList' | 'list' | 'searchView' | 'searchBar'; @@ -34,7 +39,8 @@ const styles: StyleRulesCallback = theme => { justifyContent: 'flex-end', paddingRight: theme.spacing.unit * 2, paddingBottom: theme.spacing.unit, - fontSize: '14px' + fontSize: '14px', + cursor: 'pointer' }, searchQueryList: { padding: `${theme.spacing.unit / 2}px ${theme.spacing.unit}px `, @@ -55,11 +61,13 @@ const styles: StyleRulesCallback = theme => { interface SearchBarDataProps { value: string; + currentView: string; } interface SearchBarActionProps { onSearch: (value: string) => any; debounce?: number; + onSetView: (currentView: string) => void; } type SearchBarProps = SearchBarDataProps & SearchBarActionProps & WithStyles; @@ -69,9 +77,21 @@ interface SearchBarState { isSearchViewOpen: boolean; } +const mapStateToProps = ({ structuredSearch }: RootState) => { + return { + currentView: structuredSearch.currentView + }; +}; + +const mapDispatchToProps = (dispatch: Dispatch) => ({ + onSetView: (currentView: string) => { + dispatch(goToView(currentView)); + } +}); + export const DEFAULT_SEARCH_DEBOUNCE = 1000; -export const SearchBar = withStyles(styles)( +export const SearchBar = connect(mapStateToProps, mapDispatchToProps)(withStyles(styles)( class extends React.Component { state: SearchBarState = { value: "", @@ -81,8 +101,8 @@ export const SearchBar = withStyles(styles)( timeout: number; render() { - const { classes } = this.props; - return + const { classes, currentView } = this.props; + return
@@ -102,19 +122,7 @@ export const SearchBar = withStyles(styles)( } /> - {this.state.isSearchViewOpen && -
Saved search queries
- - {this.renderSavedQueries('Test')} - {this.renderSavedQueries('Demo')} - -
Recent search queries
- - {this.renderRecentQueries('cos')} - {this.renderRecentQueries('testtest')} - -
Advanced search
-
} + {this.state.isSearchViewOpen && this.getView(currentView)}
; } @@ -133,13 +141,42 @@ export const SearchBar = withStyles(styles)( clearTimeout(this.timeout); } - closeSearchView = () => - this.setState({ isSearchViewOpen: false }) - - - openSearchView = () => - this.setState({ isSearchViewOpen: true }) + toggleSearchView = () => + this.setState({ isSearchViewOpen: !this.state.isSearchViewOpen }) + getView = (currentView: string) => { + switch (currentView) { + case SearchView.BASIC: + return +
Saved search queries
+ + {this.renderSavedQueries('Test')} + {this.renderSavedQueries('Demo')} + +
Recent search queries
+ + {this.renderRecentQueries('cos')} + {this.renderRecentQueries('testtest')} + +
this.props.onSetView(SearchView.ADVANCED)}>Advanced search
+
; + case SearchView.ADVANCED: + return + + {this.renderRecentQueries('ADVANCED VIEW')} + + + ; + case SearchView.AUTOCOMPLETE: + return + + {this.renderRecentQueries('AUTOCOMPLETE VIEW')} + + ; + default: + return ''; + } + } renderRecentQueries = (text: string) => @@ -171,7 +208,11 @@ export const SearchBar = withStyles(styles)( () => this.props.onSearch(this.state.value), this.props.debounce || DEFAULT_SEARCH_DEBOUNCE ); - + if (event.target.value.length > 0) { + this.props.onSetView(SearchView.AUTOCOMPLETE); + } else { + this.props.onSetView(SearchView.BASIC); + } } } -); +));