Merge branch '14308-basic-view-saving-and-removing-queries'
[arvados.git] / src / views-components / search-bar / search-bar-autocomplete-view.tsx
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 import * as React from 'react';
6 import { Paper, StyleRulesCallback, withStyles, WithStyles, List } from '@material-ui/core';
7 import { RecentQueriesItem } from '~/views-components/search-bar/search-bar-view';
8 import { GroupContentsResource } from '~/services/groups-service/groups-service';
9 import Highlighter from "react-highlight-words";
10
11 type CssRules = 'list' | 'searchView';
12
13 const styles: StyleRulesCallback<CssRules> = theme => {
14     return {
15         list: {
16             padding: 0
17         },
18         searchView: {
19             borderRadius: `0 0 ${theme.spacing.unit / 2}px ${theme.spacing.unit / 2}px`
20         }
21     };
22 };
23
24 export interface SearchBarAutocompleteViewDataProps {
25     searchResults?: GroupContentsResource[];
26     searchValue?: string;
27 }
28
29 type SearchBarAutocompleteViewProps = SearchBarAutocompleteViewDataProps & WithStyles<CssRules>;
30
31 export const SearchBarAutocompleteView = withStyles(styles)(
32     ({ classes, searchResults, searchValue }: SearchBarAutocompleteViewProps) =>
33         <Paper className={classes.searchView}>
34             {searchResults && <List component="nav" className={classes.list}>
35                 {searchResults.map((item: GroupContentsResource) => {
36                     return <RecentQueriesItem key={item.uuid} text={getFormattedText(item.name, searchValue)} />;
37                 })}
38             </List>}
39         </Paper>
40 );
41
42 const getFormattedText = (textToHighlight: string, searchString = '') => {
43     return <Highlighter searchWords={[searchString]} autoEscape={true} textToHighlight={textToHighlight} />;
44 };