Skip to content

Commit

Permalink
DRY up daysFromNow and isWeekend
Browse files Browse the repository at this point in the history
  • Loading branch information
noisysocks committed May 12, 2022
1 parent aca93e6 commit 176fe23
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 16 deletions.
10 changes: 2 additions & 8 deletions packages/components/src/date-time/stories/date.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import type { ComponentMeta, ComponentStory } from '@storybook/react';
* Internal dependencies
*/
import DatePicker from '../date';
import { daysFromNow, isWeekend } from './utils';

const meta: ComponentMeta< typeof DatePicker > = {
title: 'Components/DatePicker',
Expand All @@ -28,12 +29,6 @@ const Template: ComponentStory< typeof DatePicker > = ( args ) => (

export const Default: ComponentStory< typeof DatePicker > = Template.bind( {} );

function daysFromNow( days: number ) {
const date = new Date();
date.setDate( date.getDate() + days );
return date;
}

export const WithEvents: ComponentStory< typeof DatePicker > = Template.bind(
{}
);
Expand All @@ -52,6 +47,5 @@ export const WithInvalidDates: ComponentStory<
> = Template.bind( {} );
WithInvalidDates.args = {
currentDate: new Date(),
// Mark Saturdays and Sundays as invalid.
isInvalidDate: ( date ) => date.getDay() === 0 || date.getDay() === 6,
isInvalidDate: isWeekend,
};
10 changes: 2 additions & 8 deletions packages/components/src/date-time/stories/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import type { ComponentMeta, ComponentStory } from '@storybook/react';
* Internal dependencies
*/
import DateTimePicker from '..';
import { daysFromNow, isWeekend } from './utils';

const meta: ComponentMeta< typeof DateTimePicker > = {
title: 'Components/DateTimePicker',
Expand All @@ -30,12 +31,6 @@ export const Default: ComponentStory< typeof DateTimePicker > = Template.bind(
{}
);

function daysFromNow( days: number ) {
const date = new Date();
date.setDate( date.getDate() + days );
return date;
}

export const WithEvents: ComponentStory<
typeof DateTimePicker
> = Template.bind( {} );
Expand All @@ -54,6 +49,5 @@ export const WithInvalidDates: ComponentStory<
> = Template.bind( {} );
WithInvalidDates.args = {
currentDate: new Date(),
// Mark Saturdays and Sundays as invalid.
isInvalidDate: ( date ) => date.getDay() === 0 || date.getDay() === 6,
isInvalidDate: isWeekend,
};
9 changes: 9 additions & 0 deletions packages/components/src/date-time/stories/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export function daysFromNow( days: number ) {
const date = new Date();
date.setDate( date.getDate() + days );
return date;
}

export function isWeekend( date: Date ) {
return date.getDay() === 0 || date.getDay() === 6;
}

0 comments on commit 176fe23

Please sign in to comment.