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";
10 type CssRules = 'searchView' | 'list' | 'listItem';
12 const styles: StyleRulesCallback<CssRules> = theme => {
15 borderRadius: `0 0 ${theme.spacing.unit / 2}px ${theme.spacing.unit / 2}px`
21 paddingLeft: theme.spacing.unit,
22 paddingRight: theme.spacing.unit * 2,
28 export interface SearchBarAutocompleteViewDataProps {
29 searchResults?: GroupContentsResource[];
33 export interface SearchBarAutocompleteViewActionProps {
34 navigateTo: (uuid: string) => void;
37 type SearchBarAutocompleteViewProps = SearchBarAutocompleteViewDataProps & SearchBarAutocompleteViewActionProps & WithStyles<CssRules>;
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)} />
52 const getFormattedText = (textToHighlight: string, searchString = '') => {
53 return <Highlighter searchWords={[searchString]} autoEscape={true} textToHighlight={textToHighlight} />;