|
1
|
import React, { useMemo } from "react" |
|
2
|
import { Link } from "../Link" |
|
3
|
|
|
4
|
type SplitListItem = { |
|
5
|
title: string |
|
6
|
link: string |
|
7
|
description?: string |
|
8
|
} |
|
9
|
|
|
10
|
export type SplitListProps = { |
|
11
|
items: SplitListItem[] |
|
12
|
listsNum?: number |
|
13
|
} |
|
14
|
|
|
15
|
export const SplitList = ({ items, listsNum = 2 }: SplitListProps) => { |
|
16
|
const lists = useMemo(() => { |
|
17
|
const lists: SplitListItem[][] = new Array(listsNum).fill(0).map(() => []) |
|
18
|
// Split the items into listsNum lists |
|
19
|
// by pushing each item into the list at index i % listsNum |
|
20
|
// where i is the index of the item in the items array |
|
21
|
// This will create a round-robin distribution of the items |
|
22
|
// across the lists |
|
23
|
// For example, if items = [1, 2, 3, 4, 5] and listsNum = 2 |
|
24
|
// the result will be [[1, 3, 5], [2, 4]] |
|
25
|
items.forEach((item, index) => { |
|
26
|
lists[index % listsNum].push(item) |
|
27
|
}) |
|
28
|
return lists |
|
29
|
}, [items, listsNum]) |
|
30
|
|
|
31
|
return ( |
|
32
|
<div |
|
33
|
className="flex flex-col md:flex-row gap-docs_0.5 w-full" |
|
34
|
data-testid="split-list-container" |
|
35
|
> |
|
36
|
{lists.map((list, index) => ( |
|
37
|
<ul key={index} className="flex-1"> |
|
38
|
{list.map((item) => ( |
|
39
|
<li key={item.link} className="mb-docs_0.5"> |
|
40
|
<Link href={item.link}>{item.title}</Link> |
|
41
|
{item.description && ( |
|
42
|
<> |
|
43
|
:{" "} |
|
44
|
<p |
|
45
|
className="text-docs_3" |
|
46
|
data-testid="split-list-description" |
|
47
|
> |
|
48
|
{item.description} |
|
49
|
</p> |
|
50
|
</> |
|
51
|
)} |
|
52
|
</li> |
|
53
|
))} |
|
54
|
</ul> |
|
55
|
))} |
|
56
|
</div> |
|
57
|
) |
|
58
|
} |