aboutsummaryrefslogtreecommitdiffstats
path: root/src/components/atoms/lists/description-list.tsx
diff options
context:
space:
mode:
authorArmand Philippot <git@armandphilippot.com>2022-04-07 19:00:13 +0200
committerArmand Philippot <git@armandphilippot.com>2022-04-07 19:05:09 +0200
commit06396f8e942c58254ee4e87f610d3e33197e0d73 (patch)
tree30e003c15c9d6b3b5428d4af833a967e72843f09 /src/components/atoms/lists/description-list.tsx
parentff2b6c55cc691f0b62396d9ba481c75fc870cd6a (diff)
chore: add a DescriptionList component
Diffstat (limited to 'src/components/atoms/lists/description-list.tsx')
-rw-r--r--src/components/atoms/lists/description-list.tsx60
1 files changed, 60 insertions, 0 deletions
diff --git a/src/components/atoms/lists/description-list.tsx b/src/components/atoms/lists/description-list.tsx
new file mode 100644
index 0000000..df2880f
--- /dev/null
+++ b/src/components/atoms/lists/description-list.tsx
@@ -0,0 +1,60 @@
+import { FC } from 'react';
+import styles from './description-list.module.scss';
+
+export type DescriptionListItem = {
+ /**
+ * The item id.
+ */
+ id: string;
+ /**
+ * A list term.
+ */
+ term: string;
+ /**
+ * An array of values for the list term.
+ */
+ value: any[];
+};
+
+export type DescriptionListProps = {
+ /**
+ * Set additional classes to the list wrapper.
+ */
+ classes?: string;
+ /**
+ * The list items.
+ */
+ items: DescriptionListItem[];
+};
+
+/**
+ * DescriptionList component
+ *
+ * Render a description list.
+ */
+const DescriptionList: FC<DescriptionListProps> = ({ classes = '', items }) => {
+ /**
+ * Retrieve the description list items wrapped in a div element.
+ *
+ * @param {DescriptionListItem[]} listItems - An array of term and description couples.
+ * @returns {JSX.Element[]} The description list items.
+ */
+ const getItems = (listItems: DescriptionListItem[]): JSX.Element[] => {
+ return listItems.map(({ id, term, value }) => {
+ return (
+ <div key={id} className={styles.list__item}>
+ <dt className={styles.list__term}>{term}</dt>
+ {value.map((currentValue, index) => (
+ <dd key={`${id}-${index}`} className={styles.list__description}>
+ {currentValue}
+ </dd>
+ ))}
+ </div>
+ );
+ });
+ };
+
+ return <dl className={`${styles.list} ${classes}`}>{getItems(items)}</dl>;
+};
+
+export default DescriptionList;