aboutsummaryrefslogtreecommitdiffstats
path: root/src/components/molecules/meta-list/meta-item/meta-item.tsx
diff options
context:
space:
mode:
authorArmand Philippot <git@armandphilippot.com>2023-10-10 19:37:51 +0200
committerArmand Philippot <git@armandphilippot.com>2023-11-11 18:14:41 +0100
commitc87c615b5866b8a8f361eeb0764bfdea85740e90 (patch)
treec27bda05fd96bbe3154472e170ba1abd5f9ea499 /src/components/molecules/meta-list/meta-item/meta-item.tsx
parent15522ec9146f6f1956620355c44dea2a6a75b67c (diff)
refactor(components): replace Meta component with MetaList
It removes items complexity by allowing consumers to use any label/value association. Translations should also be defined by the consumer. Each item can now be configured separately (borders, layout...).
Diffstat (limited to 'src/components/molecules/meta-list/meta-item/meta-item.tsx')
-rw-r--r--src/components/molecules/meta-list/meta-item/meta-item.tsx90
1 files changed, 90 insertions, 0 deletions
diff --git a/src/components/molecules/meta-list/meta-item/meta-item.tsx b/src/components/molecules/meta-list/meta-item/meta-item.tsx
new file mode 100644
index 0000000..c5223c2
--- /dev/null
+++ b/src/components/molecules/meta-list/meta-item/meta-item.tsx
@@ -0,0 +1,90 @@
+import {
+ type ForwardRefRenderFunction,
+ type ReactElement,
+ type ReactNode,
+ forwardRef,
+} from 'react';
+import { Description, Group, type GroupProps, Term } from '../../../atoms';
+import styles from './meta-item.module.scss';
+
+export type MetaValue = string | ReactElement;
+
+export type MetaValues = {
+ id: string;
+ value: MetaValue;
+};
+
+export type MetaItemProps = Omit<GroupProps, 'children' | 'spacing'> & {
+ /**
+ * Should the values be bordered?
+ *
+ * @default false
+ */
+ hasBorderedValues?: boolean;
+ /**
+ * Should the values be inlined?
+ *
+ * @warning If you use it make sure the value is larger than the label. It
+ * could mess up your design since we are removing the label width.
+ *
+ * @default false
+ */
+ hasInlinedValues?: boolean;
+ /**
+ * Should the label and values be centered?
+ *
+ * @default false
+ */
+ isCentered?: boolean;
+ /**
+ * The item label.
+ */
+ label: ReactNode;
+ /**
+ * The item value or values.
+ */
+ value: MetaValue | MetaValues[];
+};
+
+const MetaItemWithRef: ForwardRefRenderFunction<
+ HTMLDivElement,
+ MetaItemProps
+> = (
+ {
+ className = '',
+ hasBorderedValues = false,
+ hasInlinedValues = false,
+ isCentered = false,
+ isInline = false,
+ label,
+ value,
+ ...props
+ },
+ ref
+) => {
+ const itemClass = [
+ styles.item,
+ styles[hasBorderedValues ? 'item--bordered-values' : ''],
+ styles[hasInlinedValues ? 'item--inlined-values' : ''],
+ styles[isCentered ? 'item--centered' : ''],
+ styles[isInline ? 'item--inlined' : 'item--stacked'],
+ className,
+ ].join(' ');
+
+ return (
+ <Group {...props} className={itemClass} isInline={isInline} ref={ref}>
+ <Term className={styles.label}>{label}</Term>
+ {Array.isArray(value) ? (
+ value.map((item) => (
+ <Description className={styles.value} key={item.id}>
+ {item.value}
+ </Description>
+ ))
+ ) : (
+ <Description className={styles.value}>{value}</Description>
+ )}
+ </Group>
+ );
+};
+
+export const MetaItem = forwardRef(MetaItemWithRef);