1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
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 import { SearchBarSelectedItem } from "~/store/search-bar/search-bar-reducer";
11 type CssRules = 'searchView' | 'list' | 'listItem';
13 const styles: StyleRulesCallback<CssRules> = theme => {
16 borderRadius: `0 0 ${theme.spacing.unit / 2}px ${theme.spacing.unit / 2}px`
22 paddingLeft: theme.spacing.unit,
23 paddingRight: theme.spacing.unit * 2,
28 export interface SearchBarAutocompleteViewDataProps {
29 searchResults: GroupContentsResource[];
31 selectedItem: SearchBarSelectedItem;
34 export interface SearchBarAutocompleteViewActionProps {
35 navigateTo: (uuid: string) => void;
38 type SearchBarAutocompleteViewProps = SearchBarAutocompleteViewDataProps & SearchBarAutocompleteViewActionProps & WithStyles<CssRules>;
40 export const SearchBarAutocompleteView = withStyles(styles)(
41 ({ classes, searchResults, searchValue, navigateTo, selectedItem }: SearchBarAutocompleteViewProps) => {
42 return <Paper className={classes.searchView}>
43 <List component="nav" className={classes.list}>
44 <ListItem button className={classes.listItem} selected={!selectedItem || searchValue === selectedItem.id}>
45 <ListItemText secondary={searchValue}/>
47 {searchResults.map((item: GroupContentsResource) =>
48 <ListItem button key={item.uuid} className={classes.listItem} selected={item.uuid === selectedItem.id}>
49 <ListItemText secondary={getFormattedText(item.name, searchValue)}
50 onClick={() => navigateTo(item.uuid)}/>
57 const getFormattedText = (textToHighlight: string, searchString = '') => {
58 return <Highlighter searchWords={[searchString]} autoEscape={true} textToHighlight={textToHighlight} />;