1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
5 import React from 'react';
6 import { CustomStyleRulesCallback } from 'common/custom-theme';
7 import { Paper, List, ListItem, ListItemText } from '@mui/material';
8 import { WithStyles } from '@mui/styles';
9 import withStyles from '@mui/styles/withStyles';
10 import { GroupContentsResource } from 'services/groups-service/groups-service';
11 import Highlighter from "react-highlight-words";
12 import { SearchBarSelectedItem } from "store/search-bar/search-bar-reducer";
14 type CssRules = 'searchView' | 'list' | 'listItem';
16 const styles: CustomStyleRulesCallback<CssRules> = theme => {
19 borderRadius: `0 0 ${theme.spacing(0.5)} ${theme.spacing(0.5)}`
25 paddingLeft: theme.spacing(1),
26 paddingRight: theme.spacing(2),
31 export interface SearchBarAutocompleteViewDataProps {
32 searchResults: GroupContentsResource[];
34 selectedItem: SearchBarSelectedItem;
37 export interface SearchBarAutocompleteViewActionProps {
38 navigateTo: (uuid: string) => void;
41 type SearchBarAutocompleteViewProps = SearchBarAutocompleteViewDataProps & SearchBarAutocompleteViewActionProps & WithStyles<CssRules>;
43 export const SearchBarAutocompleteView = withStyles(styles)(
44 ({ classes, searchResults, searchValue, navigateTo, selectedItem }: SearchBarAutocompleteViewProps) => {
45 return <Paper className={classes.searchView}>
46 <List component="nav" className={classes.list}>
47 <ListItem button className={classes.listItem} selected={!selectedItem || searchValue === selectedItem.id}>
48 <ListItemText secondary={searchValue}/>
50 {searchResults.map((item: GroupContentsResource) =>
51 <ListItem button key={item.uuid} className={classes.listItem} selected={item.uuid === selectedItem.id}>
52 <ListItemText secondary={getFormattedText(item.name, searchValue)}
53 onClick={() => navigateTo(item.uuid)}/>
60 const getFormattedText = (textToHighlight: string, searchString = '') => {
61 return <Highlighter searchWords={[searchString]} autoEscape={true} textToHighlight={textToHighlight} />;