Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DOP-3690: Initial implementation of Search Facets UI #925

Merged
merged 22 commits into from
Oct 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
139 changes: 94 additions & 45 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@
"@leafygreen-ui/button": "^21.0.5",
"@leafygreen-ui/callout": "^5.0.0",
"@leafygreen-ui/card": "^6.0.1",
"@leafygreen-ui/checkbox": "^6.0.3",
"@leafygreen-ui/checkbox": "^12.0.8",
"@leafygreen-ui/code": "^11.0.0",
"@leafygreen-ui/combobox": "^6.0.0",
"@leafygreen-ui/emotion": "^4.0.0",
Expand Down
5 changes: 4 additions & 1 deletion src/components/RootProvider.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ import { TabProvider } from './Tabs/tab-context';
import { ContentsProvider } from './Contents/contents-context';
import { SearchContextProvider } from './SearchResults/SearchContext';

// Check for feature flag here to make it easier to pass down for testing purposes
const SHOW_FACETS = process.env.GATSBY_FEATURE_FACETED_SEARCH === 'true';

const RootProvider = ({
children,
headingNodes,
Expand All @@ -32,7 +35,7 @@ const RootProvider = ({
>
<TocContextProvider remoteMetadata={remoteMetadata}>
<SidenavContextProvider>
<SearchContextProvider>{children}</SearchContextProvider>
<SearchContextProvider showFacets={SHOW_FACETS}>{children}</SearchContextProvider>
</SidenavContextProvider>
</TocContextProvider>
</VersionContextProvider>
Expand Down
77 changes: 77 additions & 0 deletions src/components/SearchResults/Facets/FacetGroup.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import React, { useCallback, useState } from 'react';
import { css, cx } from '@leafygreen-ui/emotion';
import { Body } from '@leafygreen-ui/typography';
import { palette } from '@leafygreen-ui/palette';
import Icon from '@leafygreen-ui/icon';
import FacetValue from './FacetValue';

// Facet options with the following ids will truncate the initial number of items shown
const TRUNCATE_OPTIONS = ['programming_language', 'sub_product'];
const TRUNCATE_AMOUNT = 5;

const optionStyle = (isNested) => css`
${isNested
? `
margin-left: 22px;
`
: `
margin-bottom: 36px;
`}
`;

const optionNameStyle = css`
margin-bottom: 8px;
font-weight: 600;
`;

const showMoreStyle = css`
display: flex;
align-items: center;
gap: 0 8px;
font-size: 13px;
line-height: 20px;
color: ${palette.gray.dark4};
margin-bottom: 16px;
cursor: pointer;
`;

const showMoreGlyphStyle = css`
color: ${palette.gray.dark2};
`;

// Representative of a "facet-option" from search server response
const FacetGroup = ({ facetOption: { name, id, options }, isNested = false }) => {
seungpark marked this conversation as resolved.
Show resolved Hide resolved
const shouldTruncate = options.length >= TRUNCATE_AMOUNT && TRUNCATE_OPTIONS.includes(id);
const [truncated, setTruncated] = useState(shouldTruncate);
const displayedOptions = truncated ? options.slice(0, TRUNCATE_AMOUNT) : options;
const truncatedState = truncated
? {
glyph: 'ChevronDown',
text: 'Show more',
}
: {
glyph: 'ChevronUp',
text: 'Show less',
};

const handleExpansionClick = useCallback(() => {
setTruncated((prev) => !prev);
}, []);

return (
<div className={cx(optionStyle(isNested))}>
{!isNested && <Body className={cx(optionNameStyle)}>{name}</Body>}
{displayedOptions.map((facet) => {
return <FacetValue key={facet.id} facetValue={facet} isNested={isNested} />;
})}
{shouldTruncate && (
<div className={cx(showMoreStyle)} onClick={handleExpansionClick}>
<Icon className={cx(showMoreGlyphStyle)} glyph={truncatedState.glyph} />
{truncatedState.text}
</div>
)}
</div>
);
};

export default FacetGroup;
Loading