Skip to content

feat: replace date-fns formatting with formatDate callback #101

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jun 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 20 additions & 22 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ A compact, masonry style alternating timeline react component which is fully cus

- 🎛️ Customize everything.
- 🎨 Consistent ([BEM](https://getbem.com)) class naming for easy styling with CSS, emotion...
- ⏰ Date formatting using [date-fns](date-fns.org) standard.
- ⏰ Custom date formatting.
- ⚖️ Alternating, left or right positioning.
- 🖼️ Render images and custom content.
- 🪄 Built with Typescript.
Expand Down Expand Up @@ -49,32 +49,30 @@ const items: TimelineItemsProps = [

The available properties of the `Timeline` component:

| Property | Type | Description | Default |
| :--------------- | :------------------------------------------ | :-------------------------------------------------------------------------------------------------------------------------------------------- | :-------- | ----------------------------------------------------------- | --------------- |
| `items` | [`TimelineItemsProps`](#timelineitemsprops) | Array of timeline items | |
| `positioning?` | `'alternating' | 'left' | 'right'` | How the items should be positioned relative to the timeline | `'alternating'` |
| `minMarkerGap?` | `number` | The minimum gap markers will have between each other | 50 (`px`) |
| `dateLocal?` | `Local` | Date locale | |
| `dateFormat?` | `string` | Specific date format according to date-fns [specification](https://date-fns.org/v2.29.3/docs/format). Ignored when passing a `string` as date | `'P'` |
| `customMarker?` | `ReactElement` | Custom maker element replacing the default | |
| `customPointer?` | `ReactElement` | Custom pointer element replacing the default | |
| `styleConfig?` | [`StyleConfig`](#styleconfig) | Style config object for customizing timeline by setting css custom properties | |
| `className?` | `string` | Additional class name | |
| Property | Type | Description | Default |
| :--------------- | :------------------------------------------ | :---------------------------------------------------------------------------- | :-------------- |
| `items` | [`TimelineItemsProps`](#timelineitemsprops) | Array of timeline items | |
| `positioning?` | `'alternating' \| 'left' \| 'right'` | How the items should be positioned relative to the timeline | `'alternating'` |
| `minMarkerGap?` | `number` | The minimum gap markers will have between each other | 50 (`px`) |
| `formatDate?` | `(date: Date) => string` | Callback to format date | |
| `customMarker?` | `ReactElement` | Custom maker element replacing the default | |
| `customPointer?` | `ReactElement` | Custom pointer element replacing the default | |
| `styleConfig?` | [`StyleConfig`](#styleconfig) | Style config object for customizing timeline by setting css custom properties | |
| `className?` | `string` | Additional class name | |

### TimelineItemsProps

An array of the following properties:

| Property | Type | Description |
| :--------------- | :--------------- | :------------------------------------------------------------------------------- |
| `key` | `Key` | Unique key for each item |
| `title?` | `string` | Optional title paragraph displayed bold |
| `date` | `Date \| string` | Date either being formatted according to provided format or passed as a `string` |
| `children?` | `ReactNode` | Pass custom content as `children` to the component |
| `dateFormat?` | `string` | Overwriting `dateFormat` property of parent `Timeline` |
| `dateLocale?` | `string` | Overwriting `dateLocale` property of parent `Timeline` |
| `customMarker?` | `ReactElement` | Overwriting `customMarker` property of parent `Timeline` |
| `customPointer?` | `ReactElement` | Overwriting `customPointer` property of parent `Timeline` |
| Property | Type | Description |
| :--------------- | :----------------------- | :------------------------------------------------------------------------------- |
| `key` | `Key` | Unique key for each item |
| `title?` | `string` | Optional title paragraph displayed bold |
| `date` | `Date \| string` | Date either being formatted according to provided format or passed as a `string` |
| `children?` | `ReactNode` | Pass custom content as `children` to the component |
| `formatDate?` | `(date: Date) => string` | Callback to format date of specific item |
| `customMarker?` | `ReactElement` | Overwriting `customMarker` property of parent `Timeline` |
| `customPointer?` | `ReactElement` | Overwriting `customPointer` property of parent `Timeline` |

### StyleConfig

Expand Down
26 changes: 0 additions & 26 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@
"@vitejs/plugin-react": "^3.0.0",
"@vitest/coverage-istanbul": "^0.32.0",
"babel-loader": "^9.1.0",
"date-fns": "^2.29.3",
"eslint": "^8.34.0",
"eslint-config-airbnb": "19.0.4",
"eslint-config-airbnb-typescript": "^17.0.0",
Expand Down
8 changes: 8 additions & 0 deletions src/components/Timeline.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -265,3 +265,11 @@ export const CustomMarkerAndPointer: StoryObj<typeof Timeline> = {
customPointer: <div className="pointy" />,
},
};

export const DateFormatting: StoryObj<typeof Timeline> = {
args: {
items,
...defaultTimelineConfig,
formatDate: (date) => date.toDateString(),
},
};
9 changes: 3 additions & 6 deletions src/components/Timeline.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ export type TimelineProps = {
items: TimelineItemsProps;
positioning?: Positioning;
minMarkerGap?: number;
dateFormat?: string;
dateLocale?: Locale;
formatDate?: (date: Date) => string;
customMarker?: ReactElement;
customPointer?: ReactElement;
styleConfig?: StyleConfig;
Expand All @@ -19,11 +18,10 @@ export type TimelineProps = {
export const defaultTimelineConfig: Partial<TimelineProps> = {
positioning: 'alternating',
minMarkerGap: 50,
dateFormat: 'P',
};

export function Timeline(props: TimelineProps) {
const { items, positioning, minMarkerGap, className, dateFormat, dateLocale, customMarker, customPointer, styleConfig } = {
const { items, positioning, minMarkerGap, className, customMarker, customPointer, styleConfig, formatDate } = {
...defaultTimelineConfig,
...props,
};
Expand Down Expand Up @@ -107,10 +105,9 @@ export function Timeline(props: TimelineProps) {
{/* First all items are rendered in the left column. They will be assigned to the corresponding column later */}
{items.map((item) => (
<TimelineItem
dateFormat={dateFormat}
dateLocale={dateLocale}
customMarker={customMarker}
customPointer={customPointer}
formatDate={formatDate}
{...item}
ref={(node) => {
const map = getRefMap();
Expand Down
8 changes: 3 additions & 5 deletions src/components/TimelineItem.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { forwardRef, Key, PropsWithChildren, ReactElement, useRef, useImperativeHandle } from 'react';
import { format } from 'date-fns';

type PropsWithKey<T> = T & {
key: Key;
Expand All @@ -13,14 +12,13 @@ export type TimelineItemProps = PropsWithChildren<{
className?: string;
title?: string;
date: Date | string;
dateFormat?: string;
dateLocale?: Locale;
formatDate?: (date: Date) => string;
customMarker?: ReactElement;
customPointer?: ReactElement;
}>;

export const TimelineItem = forwardRef<TimelineItemRefs, TimelineItemProps>(
({ className, title, date, children, dateFormat, dateLocale, customMarker, customPointer }, ref) => {
({ className, title, date, formatDate, children, customMarker, customPointer }, ref) => {
const itemRef = useRef<HTMLDivElement>(null);
const pointerRef = useRef<HTMLDivElement>(null);
const markerRef = useRef<HTMLDivElement>(null);
Expand All @@ -40,7 +38,7 @@ export const TimelineItem = forwardRef<TimelineItemRefs, TimelineItemProps>(
<div ref={pointerRef} className={['timeline-card__pointer', customPointer && 'timeline-card__pointer--custom'].join(' ')}>
{customPointer}
</div>
<p className="timeline-card__date">{date instanceof Date ? format(date, dateFormat ?? 'P', { locale: dateLocale }) : date}</p>
<p className="timeline-card__date">{date instanceof Date ? formatDate?.(date) ?? date.toLocaleDateString() : date}</p>
{title && <p className="timeline-card__title">{title}</p>}
{children}
</div>
Expand Down