aboutsummaryrefslogtreecommitdiffstats
path: root/src/components/molecules/meta-list/meta-list.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-list.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-list.tsx')
-rw-r--r--src/components/molecules/meta-list/meta-list.tsx78
1 files changed, 78 insertions, 0 deletions
diff --git a/src/components/molecules/meta-list/meta-list.tsx b/src/components/molecules/meta-list/meta-list.tsx
new file mode 100644
index 0000000..288fd9a
--- /dev/null
+++ b/src/components/molecules/meta-list/meta-list.tsx
@@ -0,0 +1,78 @@
+import { type ForwardRefRenderFunction, forwardRef } from 'react';
+import { DescriptionList, type DescriptionListProps } from '../../atoms';
+import { MetaItem, type MetaItemProps } from './meta-item';
+import styles from './meta-list.module.scss';
+
+export type MetaItemData = Pick<
+ MetaItemProps,
+ | 'hasBorderedValues'
+ | 'hasInlinedValues'
+ | 'isCentered'
+ | 'isInline'
+ | 'label'
+ | 'value'
+> & {
+ id: string;
+};
+
+export type MetaListProps = Omit<DescriptionListProps, 'children' | 'spacing'> &
+ Pick<MetaItemProps, 'hasBorderedValues' | 'hasInlinedValues'> & {
+ /**
+ * Should the items be inlined?
+ *
+ * @default false
+ */
+ hasInlinedItems?: boolean;
+ /**
+ * Should the meta be centered?
+ *
+ * @default false
+ */
+ isCentered?: boolean;
+ /**
+ * The meta items.
+ */
+ items: MetaItemData[];
+ };
+
+const MetaListWithRef: ForwardRefRenderFunction<
+ HTMLDListElement,
+ MetaListProps
+> = (
+ {
+ className = '',
+ hasBorderedValues = false,
+ hasInlinedItems = false,
+ hasInlinedValues = false,
+ isCentered = false,
+ isInline = false,
+ items,
+ ...props
+ },
+ ref
+) => {
+ const listClass = [
+ styles.list,
+ styles[isCentered ? 'list--centered' : ''],
+ styles[isInline ? 'list--inlined' : 'list--stacked'],
+ className,
+ ].join(' ');
+
+ return (
+ <DescriptionList {...props} className={listClass} ref={ref}>
+ {items.map(({ id, ...item }) => (
+ <MetaItem
+ hasBorderedValues={hasBorderedValues}
+ hasInlinedValues={hasInlinedValues}
+ isCentered={isCentered}
+ isInline={hasInlinedItems}
+ // Each item should be able to override the global settings.
+ {...item}
+ key={id}
+ />
+ ))}
+ </DescriptionList>
+ );
+};
+
+export const MetaList = forwardRef(MetaListWithRef);