Skip to content

Commit 04d96c4

Browse files
committed
feat: add unique key to timeline items
1 parent c6217a2 commit 04d96c4

File tree

3 files changed

+42
-9
lines changed

3 files changed

+42
-9
lines changed

src/components/Timeline.stories.mdx

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,42 @@ import { Timeline } from './Timeline';
66
<Timeline
77
items={[
88
{
9-
title: 'test1',
9+
key: 1,
10+
title: '1',
1011
date: new Date(),
1112
children:
1213
'alsjflskdfjsdklfjsdlkjdslkjdsalfjadslkajdfsöfj fsdjf lskdfj alsfj fljsdflö dsakfjd slkfj alsjflskdfjsdklfjsdlkjdslkjdsalfjadslkajdfsöfj fsdjf lskdfj alsfj fljsdflö dsakfjd slkfj alsjflskdfjsdklfjsdlkjdslkjdsalfjadslkajdfsöfj fsdjf lskdfj alsfj fljsdflö dsakfjd slkfj alsjflskdfjsdklfjsdlkjdslkjdsalfjadslkajdfsöfj fsdjf lskdfj alsfj fljsdflö dsakfjd slkfj ',
1314
},
14-
{ title: 'test2', date: new Date(), children: 'askflkfjds flksjfdsölfks flkalkja sflkjdsf lsdkf sdflkajlkfs f' },
15-
{ title: 'test3', date: new Date() },
16-
]}
15+
{ key: 2, title: '2', date: new Date(), children: 'askflkfjds flksjfdsölfks flkalkja sflkjdsf lsdkf sdflkajlkfs f' },
16+
{ key: 3, title: '3', date: new Date(), children: 'askflkfjds flksjfdsölfks flkalkja sflkjdsf lsdkf sdflkajlkfs f' },
17+
{ key: 4, title: '4', date: new Date(), children: 'askflkfjds flksjfdsölfks flkalkja sflkjdsf lsdkf sdflkajlkfs f' },
18+
{ key: 5, title: '5', date: new Date(), children: 'askflkfjds flksjfdsölfks flkalkja sflkjdsf lsdkf sdflkajlkfs f' },
19+
{ key: 6, title: '6', date: new Date(), children: 'askflkfjds flksjfdsölfks flkalkja sflkjdsf lsdkf sdflkajlkfs f' },
20+
{
21+
key: 7,
22+
title: '7',
23+
date: new Date(),
24+
children:
25+
'askflkfjds flksjfdsölfks flkalkja sflkjdsf lsdkf sdflkajlkfs f askflkfjds flksjfdsölfks flkalkja sflkjdsf lsdkf sdflkajlkfs f askflkfjds flksjfdsölfk f askflkfjds flksjfdsölfks flkalkja sflkjdsf lsdkf sdflkajlkfs f askflkfjds flksjfdsölfks fl f askflkfjds flksjfdsölfks flkalkja sflkjdsf lsdkf sdflkajlkfs f askflkfjds flksjfdsölfks fl f askflkfjds flksjfdsölfks flkalkja sflkjdsf lsdkf sdflkajlkfs f askflkfjds flksjfdsölfks fls flkalkja sflkjdsf lsdkf sdflkajlkfs f askflkfjds flksjfdsölfks flkalkja sflkjdsf lsdkf sdflkajlkfs f askflkfjds flksjfdsölfks flkalkja sflkjdsf lsdkf sdflkajlkfs f',
26+
},
27+
{ key: 8,title: '8', date: new Date(), children: 'askflkfjds flksjfdsölfks flkalkja sflkjdsf lsdkf sdflkajlkfs f' },
28+
{key: 9, title: '9', date: new Date(), children: 'askflkfjds flksjfdsölfks flkalkja sflkjdsf lsdkf sdflkajlkfs f' },
29+
{
30+
key: 10,
31+
title: '10',
32+
date: new Date(),
33+
children: (
34+
<>
35+
aslfjasdlfjsdaflkj
36+
<img src="https://upload.wikimedia.org/wikipedia/commons/thumb/6/66/SMPTE_Color_Bars.svg/1200px-SMPTE_Color_Bars.svg.png" />
37+
</>
38+
39+
),
40+
},
41+
{ key: 11,title: '11', date: new Date(), children: 'askflkfjds flksjfdsölfks flkalkja sflkjdsf lsdkf sdflkajlkfs f' },
42+
{ key: 12,title: '12', date: new Date() },
43+
{key: 13, title: '13', date: new Date(), children: 'askflkfjds flksjfdsölfks flkalkja sflkjdsf lsdkf sdflkajlkfs f' },
44+
{ key: 14,title: '14', date: new Date() },
45+
46+
]}
1747
/>

src/components/Timeline.tsx

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import './Timeline.css';
22
import { useEffect, useRef } from 'react';
3-
import { TimelineItem, TimelineItemProps } from './TimelineItem';
3+
import { PropsWithKey, TimelineItem, TimelineItemProps } from './TimelineItem';
44

55
type OffsetConfig = number | { left?: number; right?: number };
66

@@ -18,7 +18,7 @@ const getMarkerCompensationOffset = (left: number, right: number) => {
1818
};
1919

2020
export type TimelineProps = {
21-
items: TimelineItemProps[];
21+
items: PropsWithKey<TimelineItemProps>[];
2222
gap?: number;
2323
offset?: OffsetConfig;
2424
};
@@ -87,7 +87,6 @@ export function Timeline(props: TimelineProps) {
8787
map.delete(item.date);
8888
}
8989
}}
90-
key={item.date.toISOString()}
9190
/>
9291
))}
9392
</div>

src/components/TimelineItem.tsx

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
import { forwardRef, PropsWithChildren } from 'react';
1+
import { forwardRef, Key, PropsWithChildren } from 'react';
2+
3+
export type PropsWithKey<T> = T & {
4+
key: Key;
5+
};
26

37
export type TimelineItemProps = PropsWithChildren<{
48
className?: string;
@@ -8,7 +12,7 @@ export type TimelineItemProps = PropsWithChildren<{
812

913
export const TimelineItem = forwardRef<HTMLDivElement, TimelineItemProps>(({ className, title, date, children }, ref) => {
1014
return (
11-
<div ref={ref} className={`${className} timeline-item`}>
15+
<div ref={ref} className={['timeline-item', className].join(' ')}>
1216
<div className="timeline-item__marker" />
1317
<div className="timeline-item__content">
1418
<p>{date.toISOString()}</p>

0 commit comments

Comments
 (0)