-
Notifications
You must be signed in to change notification settings - Fork 5.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Initial SearchTypeFacet implementation.
Signed-off-by: Eric Peterson <ericpeterson@spotify.com>
- Loading branch information
Showing
8 changed files
with
301 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
--- | ||
'@backstage/plugin-search': patch | ||
--- | ||
|
||
Introduces a `<SearchTypeFacet />` component, which operates on the same part of a search query as the `<SearchType />` component, but in a more opinionated way (as a single-select control surface suitable for faceted search UIs). | ||
|
||
Check the [search plugin storybook](https://backstage.io/storybook/?path=/story/plugins-search-searchtypefacet--default) to see how it can be used. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
plugins/search/src/components/SearchTypeFacet/SearchTypeFacet.stories.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/* | ||
* Copyright 2021 The Backstage Authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
import React, { useState } from 'react'; | ||
import CatalogIcon from '@material-ui/icons/MenuBook'; | ||
import DocsIcon from '@material-ui/icons/Description'; | ||
|
||
import { SearchTypeFacet } from '../index'; | ||
import { SearchContext } from '../SearchContext'; | ||
|
||
export default { | ||
title: 'Plugins/Search/SearchTypeFacet', | ||
component: SearchTypeFacet, | ||
}; | ||
|
||
export const Default = () => { | ||
const [types, setTypes] = useState<string[]>([]); | ||
|
||
return ( | ||
<SearchContext.Provider | ||
value={{ types, setTypes, setPageCursor: () => {} } as any} | ||
> | ||
<SearchTypeFacet | ||
name="Result Type" | ||
defaultValue="software-catalog" | ||
types={[ | ||
{ | ||
value: 'software-catalog', | ||
name: 'Software Catalog', | ||
icon: <CatalogIcon />, | ||
}, | ||
{ value: 'techdocs', name: 'Documentation', icon: <DocsIcon /> }, | ||
]} | ||
/> | ||
</SearchContext.Provider> | ||
); | ||
}; |
184 changes: 184 additions & 0 deletions
184
plugins/search/src/components/SearchTypeFacet/SearchTypeFacet.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,184 @@ | ||
/* | ||
* Copyright 2021 The Backstage Authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
import React, { | ||
ChangeEvent, | ||
cloneElement, | ||
Fragment, | ||
useEffect, | ||
useState, | ||
} from 'react'; | ||
import { useSearch } from '../SearchContext'; | ||
import { | ||
Accordion, | ||
AccordionSummary, | ||
AccordionDetails, | ||
Card, | ||
CardContent, | ||
CardHeader, | ||
Divider, | ||
List, | ||
ListItem, | ||
ListItemIcon, | ||
ListItemText, | ||
makeStyles, | ||
} from '@material-ui/core'; | ||
import ExpandMoreIcon from '@material-ui/icons/ExpandMore'; | ||
import AllIcon from '@material-ui/icons/FontDownload'; | ||
|
||
const useStyles = makeStyles(theme => ({ | ||
card: { | ||
backgroundColor: 'rgba(0, 0, 0, .11)', | ||
}, | ||
cardContent: { | ||
paddingTop: theme.spacing(1), | ||
}, | ||
icon: { | ||
color: theme.palette.common.black, | ||
}, | ||
list: { | ||
width: '100%', | ||
}, | ||
listItemIcon: { | ||
width: '24px', | ||
height: '24px', | ||
}, | ||
accordion: { | ||
backgroundColor: theme.palette.background.paper, | ||
}, | ||
accordionSummary: { | ||
minHeight: 'auto', | ||
'&.Mui-expanded': { | ||
minHeight: 'auto', | ||
}, | ||
}, | ||
accordionSummaryContent: { | ||
margin: theme.spacing(2, 0), | ||
'&.Mui-expanded': { | ||
margin: theme.spacing(2, 0), | ||
}, | ||
}, | ||
accordionDetails: { | ||
padding: theme.spacing(0, 0, 1), | ||
}, | ||
})); | ||
|
||
/** | ||
* @public | ||
*/ | ||
export type SearchTypeFacetProps = { | ||
/* what about this? */ | ||
name: string; | ||
types: Array<{ | ||
value: string; | ||
name: string; | ||
icon: JSX.Element; | ||
}>; | ||
defaultValue?: string; | ||
}; | ||
|
||
/** | ||
* A control surface for the search query's "types" property, displayed as a | ||
* single-select collapsible accordion suitable for use in faceted search UIs. | ||
* @public | ||
*/ | ||
export const SearchTypeFacet = (props: SearchTypeFacetProps) => { | ||
const classes = useStyles(); | ||
const { setPageCursor, setTypes, types } = useSearch(); | ||
const [expanded, setExpanded] = useState(true); | ||
const { defaultValue, name, types: givenTypes } = props; | ||
|
||
const handleChange = (_event: ChangeEvent<{}>, newExpanded: boolean) => | ||
setExpanded(newExpanded); | ||
const handleClick = (type: string) => { | ||
return () => { | ||
setTypes(type !== '' ? [type] : []); | ||
setPageCursor('0'); | ||
setExpanded(false); | ||
}; | ||
}; | ||
|
||
// Handle any provided defaultValue | ||
useEffect(() => { | ||
if (defaultValue) { | ||
setTypes([defaultValue]); | ||
} | ||
// eslint-disable-next-line react-hooks/exhaustive-deps | ||
}, []); | ||
|
||
const definedTypes = [ | ||
{ | ||
value: '', | ||
name: 'All', | ||
icon: <AllIcon />, | ||
}, | ||
...givenTypes, | ||
]; | ||
const selected = types[0] || ''; | ||
|
||
return ( | ||
<Card className={classes.card}> | ||
<CardHeader title={name} titleTypographyProps={{ variant: 'overline' }} /> | ||
<CardContent className={classes.cardContent}> | ||
<Accordion | ||
className={classes.accordion} | ||
expanded={expanded} | ||
onChange={handleChange} | ||
> | ||
<AccordionSummary | ||
classes={{ | ||
root: classes.accordionSummary, | ||
content: classes.accordionSummaryContent, | ||
}} | ||
expandIcon={<ExpandMoreIcon className={classes.icon} />} | ||
IconButtonProps={{ size: 'small' }} | ||
> | ||
{expanded | ||
? 'Collapse' | ||
: definedTypes.filter(t => t.value === selected)[0]!.name} | ||
</AccordionSummary> | ||
<AccordionDetails classes={{ root: classes.accordionDetails }}> | ||
<List | ||
className={classes.list} | ||
component="nav" | ||
aria-label="filter by type" | ||
disablePadding | ||
dense | ||
> | ||
{definedTypes.map(type => ( | ||
<Fragment key={type.value}> | ||
<Divider /> | ||
<ListItem | ||
selected={types.includes(type.value)} | ||
onClick={handleClick(type.value)} | ||
button | ||
> | ||
<ListItemIcon> | ||
{cloneElement(type.icon, { | ||
className: classes.listItemIcon, | ||
})} | ||
</ListItemIcon> | ||
<ListItemText primary={type.name} /> | ||
</ListItem> | ||
</Fragment> | ||
))} | ||
</List> | ||
</AccordionDetails> | ||
</Accordion> | ||
</CardContent> | ||
</Card> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
/* | ||
* Copyright 2021 The Backstage Authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
export { SearchTypeFacet } from './SearchTypeFacet'; | ||
export type { SearchTypeFacetProps } from './SearchTypeFacet'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters