-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(timeline): Start using compound components
This change introduces the concept of compound components, applied to Row and Event sub components of Timeline.
- Loading branch information
Showing
12 changed files
with
279 additions
and
210 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
packages/react-component-library/src/components/Timeline/Events.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import React from 'react' | ||
|
||
import { EventProps } from '.' | ||
|
||
export interface EventsProps extends ComponentWithClass { | ||
children: | ||
| React.ReactElement<EventProps> | ||
| React.ReactElement<EventProps>[] | ||
} | ||
|
||
export const Events: React.FC<EventsProps> = (props) => { | ||
return ( | ||
<div {...props} /> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
packages/react-component-library/src/components/Timeline/Rows.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import React from 'react' | ||
import classNames from 'classnames' | ||
|
||
import { RowProps } from '.' | ||
import { getKey } from './helpers' | ||
import { NO_DATA_MESSAGE } from './constants' | ||
|
||
export interface RowsProps extends ComponentWithClass { | ||
children: React.ReactElement<RowProps> | React.ReactElement<RowProps>[] | ||
} | ||
|
||
const noData = ( | ||
<span className="timeline__empty" data-testid="timeline-no-data"> | ||
{NO_DATA_MESSAGE} | ||
</span> | ||
) | ||
|
||
export const Rows: React.FC<RowsProps> = ({ children, className }) => { | ||
const classes = classNames('timeline__main', className) | ||
const childrenWithKey = React.Children.map( | ||
children, | ||
(child: React.ReactElement<RowProps>, index: number) => | ||
React.cloneElement(child, { | ||
...child.props, | ||
key: getKey('timeline-row', index), | ||
}) | ||
) | ||
|
||
return ( | ||
<main className={classes}> | ||
{childrenWithKey && childrenWithKey.length ? childrenWithKey : noData} | ||
</main> | ||
) | ||
} | ||
|
||
Rows.displayName = 'Rows' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
62 changes: 42 additions & 20 deletions
62
packages/react-component-library/src/components/Timeline/Timeline.stories.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,51 @@ | ||
import React from 'react' | ||
import { storiesOf } from '@storybook/react' | ||
|
||
import { Timeline } from '.' | ||
import { Timeline, Event, Events, Row, Rows } from '.' | ||
|
||
const stories = storiesOf('Timeline', module) | ||
|
||
stories.add('No data', () => <Timeline rowData={[]} />) | ||
|
||
const rowData = [ | ||
{ | ||
name: 'Example Row', | ||
events: [ | ||
{ | ||
title: 'Example Event', | ||
startDate: new Date(2020, 3, 13), | ||
endDate: new Date(2020, 3, 22), | ||
}, | ||
], | ||
}, | ||
] | ||
stories.add('No data', () => ( | ||
<Timeline> | ||
<Rows>{}</Rows> | ||
</Timeline> | ||
)) | ||
|
||
stories.add('With data', () => ( | ||
<Timeline | ||
startDate={new Date(2020, 4, 0)} | ||
today={new Date(2020, 3, 15)} | ||
rowData={rowData} | ||
/> | ||
<Timeline startDate={new Date(2020, 4, 0)} today={new Date(2020, 3, 15)}> | ||
<Rows> | ||
<Row name="Row 1"> | ||
<Events> | ||
<Event | ||
startDate={new Date(2020, 3, 13)} | ||
endDate={new Date(2020, 3, 18)} | ||
> | ||
Event 1 | ||
</Event> | ||
<Event | ||
startDate={new Date(2020, 3, 20)} | ||
endDate={new Date(2020, 3, 22)} | ||
> | ||
Event 2 | ||
</Event> | ||
</Events> | ||
</Row> | ||
<Row name="Row 2"> | ||
<Events> | ||
<Event | ||
startDate={new Date(2020, 3, 15)} | ||
endDate={new Date(2020, 3, 20)} | ||
> | ||
Event 3 | ||
</Event> | ||
<Event | ||
startDate={new Date(2020, 3, 22)} | ||
endDate={new Date(2020, 3, 24)} | ||
> | ||
Event 4 | ||
</Event> | ||
</Events> | ||
</Row> | ||
</Rows> | ||
</Timeline> | ||
)) |
27 changes: 6 additions & 21 deletions
27
packages/react-component-library/src/components/Timeline/Timeline.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 0 additions & 42 deletions
42
packages/react-component-library/src/components/Timeline/__tests__/Event.test.tsx
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.