-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
👻 Adds colors to drawer tags & removes boilerplate (#1481)
Follow up to: #1470 - Added a reusable component for drawer labels. - Adds random colors to the tags just for differentiation between the tags when viewing in the drawer. --------- Signed-off-by: Ian Bolton <ibolton@redhat.com>
- Loading branch information
1 parent
8b0c97d
commit eb632fe
Showing
3 changed files
with
62 additions
and
62 deletions.
There are no files selected for viewing
51 changes: 51 additions & 0 deletions
51
client/src/app/components/labels-from-items/labels-from-items.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,51 @@ | ||
import React from "react"; | ||
import { LabelGroup, LabelProps } from "@patternfly/react-core"; | ||
import { LabelCustomColor } from "../LabelCustomColor"; | ||
import { useTranslation } from "react-i18next"; | ||
|
||
interface RandomColorLabelProps extends LabelProps {} | ||
|
||
export const RandomColorLabel: React.FC<RandomColorLabelProps> = ({ | ||
...props | ||
}) => { | ||
const getRandomColor = (() => { | ||
"use strict"; | ||
|
||
const randomInt = (min: number, max: number) => { | ||
return Math.floor(Math.random() * (max - min + 1)) + min; | ||
}; | ||
|
||
return () => { | ||
const h = randomInt(0, 360); | ||
const s = randomInt(42, 98); | ||
const l = randomInt(40, 90); | ||
return `hsl(${h},${s}%,${l}%)`; | ||
}; | ||
})(); | ||
|
||
const randomColor = getRandomColor(); | ||
|
||
return <LabelCustomColor color={randomColor} {...props} />; | ||
}; | ||
|
||
export function LabelsFromItems<T extends { name: string }>({ | ||
items, | ||
noneMessage, | ||
}: { | ||
items?: T[]; | ||
noneMessage?: string; | ||
}): JSX.Element { | ||
const { t } = useTranslation(); | ||
|
||
if (items?.length ?? 0 === 0) { | ||
return <div>{noneMessage || t("terms.none")}</div>; | ||
} | ||
|
||
return ( | ||
<LabelGroup> | ||
{items?.map((item, index) => ( | ||
<RandomColorLabel key={index}>{item.name}</RandomColorLabel> | ||
))} | ||
</LabelGroup> | ||
); | ||
} |
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