Skip to content

Commit

Permalink
fix(DateTime): allow not showing weekday
Browse files Browse the repository at this point in the history
  • Loading branch information
mxdvl committed Jan 18, 2024
1 parent 8e9393a commit 4b3f210
Show file tree
Hide file tree
Showing 10 changed files with 99 additions and 16 deletions.
4 changes: 3 additions & 1 deletion dotcom-rendering/src/components/Card/components/CardAge.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,9 @@ export const CardAge = ({
date={new Date(webPublicationDate)}
display="relative"
editionId="UK"
show="date"
showWeekday={false}
showDate={true}
showTime={false}
/>
</span>
);
Expand Down
8 changes: 7 additions & 1 deletion dotcom-rendering/src/components/CricketScoreboard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,13 @@ export const CricketScoreboard = ({
return (
<div css={containerStyle}>
<h2 css={screenReaderOnlyStyle}>
<DateTime date={date} editionId={editionId} show="date" />
<DateTime
date={date}
editionId={editionId}
showWeekday={false}
showDate={true}
showTime={false}
/>
{match.competitionName}, {match.venueName}
</h2>
<table css={tableStyle}>
Expand Down
58 changes: 52 additions & 6 deletions dotcom-rendering/src/components/DateTime.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,27 +28,73 @@ export const UK: Story = {
};

export const TimeOnly: Story = {
args: { date, editionId: 'UK', show: 'time' },
args: {
date,
editionId: 'UK',
showWeekday: false,
showDate: false,
showTime: true,
},
};

export const DateOnly: Story = {
args: { date, editionId: 'UK', show: 'date' },
args: {
date,
editionId: 'UK',
showWeekday: false,
showDate: true,
showTime: false,
},
};

export const WeekdayDateOnly: Story = {
args: {
date,
editionId: 'UK',
showWeekday: true,
showDate: true,
showTime: false,
},
};

export const US: Story = {
args: { date, editionId: 'US' },
args: {
date,
editionId: 'US',
showWeekday: true,
showDate: true,
showTime: true,
},
};

export const AU: Story = {
args: { date, editionId: 'AU' },
args: {
date,
editionId: 'AU',
showWeekday: true,
showDate: true,
showTime: true,
},
};

export const EUR: Story = {
args: { date, editionId: 'EUR' },
args: {
date,
editionId: 'EUR',
showWeekday: true,
showDate: true,
showTime: true,
},
};

export const INT: Story = {
args: { date, editionId: 'INT' },
args: {
date,
editionId: 'INT',
showWeekday: true,
showDate: true,
showTime: true,
},
};

export default meta;
24 changes: 19 additions & 5 deletions dotcom-rendering/src/components/DateTime.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,21 @@ import { RelativeTime } from './RelativeTime.importable';
type Props = {
date: Date;
editionId: EditionId;
show?: 'date & time' | 'date' | 'time';
showWeekday: boolean;
showDate: boolean;
showTime: boolean;
display?: 'absolute' | 'relative';
};

const formatWeekday = (date: Date, locale: string, timeZone: string) =>
date.toLocaleDateString(locale, {
weekday: 'short',
timeZone,
});

const formatDate = (date: Date, locale: string, timeZone: string) =>
date
.toLocaleDateString(locale, {
weekday: 'short',
day: 'numeric',
month: 'short',
year: 'numeric',
Expand All @@ -35,7 +42,9 @@ const formatTime = (date: Date, locale: string, timeZone: string) =>
export const DateTime = ({
date,
editionId,
show = 'date & time',
showWeekday,
showDate,
showTime,
display = 'absolute',
}: Props) => {
const { locale, timeZone } = getEditionFromId(editionId);
Expand Down Expand Up @@ -63,8 +72,13 @@ export const DateTime = ({
timeZone,
})}
>
{show.includes('date') && formatDate(date, locale, timeZone)}{' '}
{show.includes('time') && formatTime(date, locale, timeZone)}
{[
showWeekday && formatWeekday(date, locale, timeZone),
showDate && formatDate(date, locale, timeZone),
showTime && formatTime(date, locale, timeZone),
]
.filter(isString)
.join(' ')}
</time>
);
};
4 changes: 3 additions & 1 deletion dotcom-rendering/src/components/FirstPublished.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,9 @@ const FirstPublished = ({
date={new Date(firstPublished)}
display="relative"
editionId="UK"
show="date"
showWeekday={false}
showDate={true}
showTime={false}
/>
</span>
)}
Expand Down
3 changes: 3 additions & 0 deletions dotcom-rendering/src/components/KeyEventCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,9 @@ export const KeyEventCard = ({
date={new Date(blockFirstPublished)}
display="relative"
editionId="UK"
showWeekday={false}
showDate={true}
showTime={true}
/>
</div>
<div css={textStyles}>{title}</div>
Expand Down
4 changes: 3 additions & 1 deletion dotcom-rendering/src/components/LastUpdated.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ const LastUpdated = ({
<DateTime
date={new Date(lastUpdated)}
editionId={editionId}
show="time"
showWeekday={false}
showDate={false}
showTime={true}
/>
</div>
);
Expand Down
3 changes: 3 additions & 0 deletions dotcom-rendering/src/components/LatestLinks.importable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,9 @@ export const LatestLinks = ({
}
display="relative"
editionId="UK"
showWeekday={false}
showDate={true}
showTime={false}
/>
</div>
<span className="show-underline">
Expand Down
3 changes: 3 additions & 0 deletions dotcom-rendering/src/components/PinnedPost.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,9 @@ export const PinnedPost = ({ pinnedPost, children, format }: Props) => {
date={new Date(pinnedPost.blockFirstPublished)}
display="relative"
editionId="UK"
showWeekday={false}
showDate={true}
showTime={true}
/>
</div>
)}
Expand Down
4 changes: 3 additions & 1 deletion dotcom-rendering/src/components/WitnessBlockComponent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,9 @@ const WitnessWrapper = ({
<DateTime
date={new Date(dateCreated)}
editionId={editionId}
show="date"
showWeekday={false}
showDate={true}
showTime={false}
/>
</p>
</div>
Expand Down

0 comments on commit 4b3f210

Please sign in to comment.