Skip to content

Commit

Permalink
feat(story): add story for search component
Browse files Browse the repository at this point in the history
fix: minor changes
  • Loading branch information
kahboom committed Dec 2, 2024
1 parent 5b7db8d commit 0435a90
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 8 deletions.
72 changes: 72 additions & 0 deletions client/src/app/pages/search/components/SearchMenu.stories.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import type { Meta, StoryObj } from "@storybook/react";
import { IEntity, SearchMenu } from "./SearchMenu";
import { Label, MenuItem } from "@patternfly/react-core";
import React from "react";

const meta = {
title: "Components/Search/SearchMenu",
component: SearchMenu,
tags: ["autodocs"],
} satisfies Meta<typeof SearchMenu>;

export default meta;
type Story = StoryObj<typeof meta>;

function customFilterIncludes(list: IEntity[], searchString: string) {
let options: React.JSX.Element[] = list
.filter(
(option) =>
option.id.toLowerCase().includes(searchString.toLowerCase()) ||
option.title?.toLowerCase().includes(searchString.toLowerCase()) ||
option.description?.toLowerCase().includes(searchString.toLowerCase())
)
.map((option) => (
<MenuItem
itemId={option.id}
key={option.id}
description={option.description}
to={option.navLink}
>
{option.title} <Label color={option.typeColor}>{option.type}</Label>
</MenuItem>
));

if (options.length > 10) {
options = options.slice(0, 10);
} else {
options = [
...options,
...list
.filter(
(option: IEntity) =>
!option.id.startsWith(searchString.toLowerCase()) &&
option.id.includes(searchString.toLowerCase())
)
.map((option: IEntity) => (
<MenuItem
itemId={option.id}
key={option.id}
description={option.description}
to={option.navLink}
>
{option.title} <Label color={option.typeColor}>{option.type}</Label>
</MenuItem>
)),
].slice(0, 10);
}

return options;
}

export const DefaultState: Story = {
args: {
onChangeSearch: jest.fn(),
},
};

export const WithCustomFilter: Story = {
args: {
filterFunction: customFilterIncludes,
onChangeSearch: jest.fn(),
},
};
12 changes: 5 additions & 7 deletions client/src/app/pages/search/components/SearchMenu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { HubRequestParams } from "@app/api/models";
import { useFetchPackages } from "@app/queries/packages";
import { useFetchSBOMs } from "@app/queries/sboms";
import { useFetchVulnerabilities } from "@app/queries/vulnerabilities";
import { Link } from "react-router-dom";

export interface IEntity {
id: string;
Expand Down Expand Up @@ -50,9 +51,10 @@ export function filterEntityListByValue(list: IEntity[], searchString: string) {
itemId={option.id}
key={option.id}
description={option.description}
to={option.navLink}
>
{option.title} <Label color={option.typeColor}>{option.type}</Label>
<Link to={option.navLink}>
{option.title} <Label color={option.typeColor}>{option.type}</Label>
</Link>
</MenuItem>
));

Expand Down Expand Up @@ -120,8 +122,7 @@ function useAllEntities(filterText: string) {

const transformedPackages: IEntity[] = packages.map((item) => ({
id: item.uuid,
title: "item.decomposedPurl ? item.decomposedPurl?.name : item.purl",
description: "item.decomposedPurl?.namespace",
title: item.purl,
navLink: `/packages/${item.uuid}`,
type: "Package",
typeColor: "cyan",
Expand Down Expand Up @@ -193,10 +194,7 @@ export const SearchMenu: React.FC<ISearchMenu> = ({
) {
setIsAutocompleteOpen(true);

console.table(entityList);
console.log(newValue);
const options = filterFunction(entityList, newValue);
console.log(options);

// The menu is hidden if there are no options
setIsAutocompleteOpen(options.length > 0);
Expand Down
2 changes: 1 addition & 1 deletion client/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"$schema": "https://json.schemastore.org/tsconfig",
"include": ["src/**/*", "config/**/*", "types/**/*", "setupTests.ts"],
"include": ["src/**/*", "config/**/*", "types/**/*"],
"compilerOptions": {
"outDir": "dist",
"baseUrl": ".",
Expand Down

0 comments on commit 0435a90

Please sign in to comment.