Merge branch 'master' of git.curoverse.com:arvados-workbench2 into 14276-structured...
[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, ListItem, ListItemText } from '@material-ui/core';
7 import { GroupContentsResource } from '~/services/groups-service/groups-service';
8 import Highlighter from "react-highlight-words";
9
10 type CssRules = 'searchView' | 'list' | 'listItem';
11
12 const styles: StyleRulesCallback<CssRules> = theme => {
13     return {
14         searchView: {
15             borderRadius: `0 0 ${theme.spacing.unit / 2}px ${theme.spacing.unit / 2}px`
16         },
17         list: {
18             padding: 0
19         },
20         listItem: {
21             paddingLeft: theme.spacing.unit,
22             paddingRight: theme.spacing.unit * 2,
23         },
24         
25     };
26 };
27
28 export interface SearchBarAutocompleteViewDataProps {
29     searchResults?: GroupContentsResource[];
30     searchValue?: string;
31 }
32
33 export interface SearchBarAutocompleteViewActionProps {
34     navigateTo: (uuid: string) => void;
35 }
36
37 type SearchBarAutocompleteViewProps = SearchBarAutocompleteViewDataProps & SearchBarAutocompleteViewActionProps & WithStyles<CssRules>;
38
39 export const SearchBarAutocompleteView = withStyles(styles)(
40     ({ classes, searchResults, searchValue, navigateTo }: SearchBarAutocompleteViewProps) =>
41         <Paper className={classes.searchView}>
42             {searchResults && <List component="nav" className={classes.list}>
43                 {searchResults.map((item: GroupContentsResource) =>
44                     <ListItem button key={item.uuid} className={classes.listItem}>
45                         <ListItemText secondary={getFormattedText(item.name, searchValue)} onClick={() => navigateTo(item.uuid)} />
46                     </ListItem>
47                 )}
48             </List>}
49         </Paper>
50 );
51
52 const getFormattedText = (textToHighlight: string, searchString = '') => {
53     return <Highlighter searchWords={[searchString]} autoEscape={true} textToHighlight={textToHighlight} />;
54 };