aboutsummaryrefslogtreecommitdiffstats
path: root/src/components/organisms/widgets/links-list-widget.tsx
blob: 17a588496dcd096db5271ac9533ceea4b777623c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
import type { FC } from 'react';
import { slugify } from '../../../utils/helpers';
import { Link, List, ListItem } from '../../atoms';
import { Collapsible, type CollapsibleProps } from '../../molecules';
import styles from './links-list-widget.module.scss';

export type LinksListItems = {
  /**
   * An array of name/url couple child of this list item.
   */
  child?: LinksListItems[];
  /**
   * The item name.
   */
  name: string;
  /**
   * The item url.
   */
  url: string;
};

export type LinksListWidgetProps = Omit<
  CollapsibleProps,
  'children' | 'disablePadding' | 'hasBorders'
> & {
  className?: string;
  /**
   * Should the links be ordered?
   *
   * @default false
   */
  isOrdered?: boolean;
  /**
   * An array of name/url couple.
   */
  items: LinksListItems[];
};

/**
 * LinksListWidget component
 *
 * Render a list of links inside a widget.
 */
export const LinksListWidget: FC<LinksListWidgetProps> = ({
  className = '',
  isOrdered = false,
  items,
  ...props
}) => {
  const listKindClass = `list--${isOrdered ? 'ordered' : 'unordered'}`;

  /**
   * Format the widget data to be used as List items.
   *
   * @param {LinksListItems[]} data - The widget data.
   * @returns {ListItem[]} The list items data.
   */
  const getListItems = (data: LinksListItems[]) =>
    data.map((item) => (
      <ListItem className={styles.list__item} key={slugify(item.name)}>
        <Link className={styles.list__link} href={item.url}>
          {item.name}
        </Link>
        {item.child?.length ? (
          <List
            className={`${styles.list} ${styles[listKindClass]} ${className}`}
            hideMarker
            isOrdered={isOrdered}
          >
            {getListItems(item.child)}
          </List>
        ) : null}
      </ListItem>
    ));

  return (
    <Collapsible {...props} className={styles.widget} disablePadding hasBorders>
      <List
        className={`${styles.list} ${styles[listKindClass]} ${className}`}
        hideMarker
        isOrdered={isOrdered}
      >
        {getListItems(items)}
      </List>
    </Collapsible>
  );
};