Add optional badge to Catalog Category label (#5156)

* Add optional badge to Catalog Category label.

Signed-off-by: Panu Horsmalahti <phorsmalahti@mirantis.com>

* Don't render badge container if there is no badge. Change CatalogCategoryLabel interface.

Signed-off-by: Panu Horsmalahti <phorsmalahti@mirantis.com>

* Add badgeBackgroundColor CSS variable for extensions. Regenerate theme-vars.css.

Signed-off-by: Panu Horsmalahti <phorsmalahti@mirantis.com>

* Use category as prop in CatalogCategoryLabel

Signed-off-by: Panu Horsmalahti <phorsmalahti@mirantis.com>
This commit is contained in:
Panu Horsmalahti authored and GitHub committed 2022-04-06 15:20:41 +03:00
1 parent 66330485b1
commit ca21e0842a
8 files changed
+117 -4

No files matched your search

@@ -0,0 +1,53 @@
/**
* Copyright (c) OpenLens Authors. All rights reserved.
* Licensed under MIT License. See LICENSE in root directory for more information.
*/
import { render, screen } from "@testing-library/react";
import React from "react";
import "@testing-library/jest-dom/extend-expect";
import { CatalogCategory, CatalogCategorySpec } from "../../../../common/catalog";
import { CatalogCategoryLabel } from "../catalog-category-label";
class CatalogCategoryWithoutBadge extends CatalogCategory {
public readonly apiVersion = "catalog.k8slens.dev/v1alpha1";
public readonly kind = "CatalogCategory";
public metadata = {
name: "Test Category",
icon: "",
};
public spec: CatalogCategorySpec = {
group: "entity.k8slens.dev",
versions: [],
names: {
kind: "Test",
},
};
}
class CatalogCategoryWithBadge extends CatalogCategoryWithoutBadge {
getBadge() {
return (<div>Test Badge</div>);
}
}
describe("CatalogCategoryLabel", () => {
it("renders without a badge", async () => {
const category = new CatalogCategoryWithoutBadge();
render(<CatalogCategoryLabel category={category}/>);
expect(await screen.findByText("Test Category")).toBeInTheDocument();
});
it("renders with a badge", async () => {
const category = new CatalogCategoryWithBadge();
render(<CatalogCategoryLabel category={category}/>);
expect(await screen.findByText("Test Category")).toBeInTheDocument();
expect(await screen.findByText("Test Badge")).toBeInTheDocument();
});
});
@@ -0,0 +1,24 @@
/**
* Copyright (c) OpenLens Authors. All rights reserved.
* Licensed under MIT License. See LICENSE in root directory for more information.
*/
import React from "react";
import type { CatalogCategory } from "../../../common/catalog/catalog-entity";
interface CatalogCategoryLabelProps {
category: CatalogCategory;
}
/**
* Display label for Catalog Category for the Catalog menu
*/
export const CatalogCategoryLabel = ({ category }: CatalogCategoryLabelProps) => {
const badge = category.getBadge();
return (
<div className="flex">
<div>{category.metadata.name}</div>
{badge ? (<div className="flex items-center">{badge}</div>) : null}
</div>
);
};
@@ -14,6 +14,7 @@ import { StylesProvider } from "@material-ui/core";
import { cssNames } from "../../utils";
import type { CatalogCategory } from "../../api/catalog-entity";
import { observer } from "mobx-react";
import { CatalogCategoryLabel } from "./catalog-category-label";
export interface CatalogMenuProps {
activeItem: string;
@@ -66,7 +67,7 @@ export const CatalogMenu = observer((props: CatalogMenuProps) => {
icon={getCategoryIcon(category)}
key={category.getId()}
nodeId={category.getId()}
label={category.metadata.name}
label={<CatalogCategoryLabel category={category}/>}
data-testid={`${category.getId()}-tab`}
onClick={() => props.onItemClick(category.getId())}
/>