Skip to content
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

Use TypeScript and controls for DateTimePicker's stories #40986

Merged
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
21 changes: 20 additions & 1 deletion packages/components/src/date-time/date.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,26 @@ function DatePickerDay( { day, events = [] }: DatePickerDayProps ) {
);
}

function DatePicker( {
/**
* DatePicker is a React component that renders a calendar for date selection.
*
* ```jsx
* import { DatePicker } from '@wordpress/components';
* import { useState } from '@wordpress/element';
*
* const MyDatePicker = () => {
* const [ date, setDate ] = useState( new Date() );
*
* return (
* <DatePicker
* currentDate={ date }
* onChange={ ( newDate ) => setDate( newDate ) }
* />
* );
* };
* ```
*/
export function DatePicker( {
currentDate,
onChange,
events,
Expand Down
3 changes: 1 addition & 2 deletions packages/components/src/date-time/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,6 @@ function UnforwardedDateTimePicker(
* date and time selection. The calendar and clock components can be accessed
* individually using the `DatePicker` and `TimePicker` components respectively.
*
* @example
* ```jsx
* import { DateTimePicker } from '@wordpress/components';
* import { useState } from '@wordpress/element';
Expand All @@ -186,7 +185,7 @@ function UnforwardedDateTimePicker(
* <DateTimePicker
* currentDate={ date }
* onChange={ ( newDate ) => setDate( newDate ) }
* is12Hour={ true }
* is12Hour
* />
* );
* };
Expand Down
17 changes: 0 additions & 17 deletions packages/components/src/date-time/stories/date.js

This file was deleted.

73 changes: 73 additions & 0 deletions packages/components/src/date-time/stories/date.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/**
* External dependencies
*/
import type { ComponentMeta, ComponentStory } from '@storybook/react';

/**
* WordPress dependencies
*/
import { useState, useEffect } from '@wordpress/element';

/**
* Internal dependencies
*/
import DatePicker from '../date';
import { daysFromNow, isWeekend } from './utils';

const meta: ComponentMeta< typeof DatePicker > = {
title: 'Components/DatePicker',
component: DatePicker,
argTypes: {
currentDate: { control: 'date' },
onChange: { action: 'onChange', control: { type: null } },
},
parameters: {
controls: { expanded: true },
docs: { source: { state: 'open' } },
},
};
export default meta;

const Template: ComponentStory< typeof DatePicker > = ( {
currentDate,
onChange,
...args
} ) => {
const [ date, setDate ] = useState( currentDate );
useEffect( () => {
setDate( currentDate );
}, [ currentDate ] );
return (
<DatePicker
{ ...args }
currentDate={ date }
onChange={ ( newDate ) => {
setDate( newDate );
onChange?.( newDate );
} }
/>
);
};

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

export const WithEvents: ComponentStory< typeof DatePicker > = Template.bind(
{}
);
WithEvents.args = {
currentDate: new Date(),
events: [
{ date: daysFromNow( 2 ) },
{ date: daysFromNow( 4 ) },
{ date: daysFromNow( 6 ) },
{ date: daysFromNow( 8 ) },
],
};

export const WithInvalidDates: ComponentStory<
typeof DatePicker
> = Template.bind( {} );
WithInvalidDates.args = {
currentDate: new Date(),
isInvalidDate: isWeekend,
};
91 changes: 0 additions & 91 deletions packages/components/src/date-time/stories/index.js

This file was deleted.

75 changes: 75 additions & 0 deletions packages/components/src/date-time/stories/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/**
* External dependencies
*/
import type { ComponentMeta, ComponentStory } from '@storybook/react';

/**
* WordPress dependencies
*/
import { useState, useEffect } from '@wordpress/element';

/**
* Internal dependencies
*/
import DateTimePicker from '..';
import { daysFromNow, isWeekend } from './utils';

const meta: ComponentMeta< typeof DateTimePicker > = {
title: 'Components/DateTimePicker',
component: DateTimePicker,
argTypes: {
currentDate: { control: 'date' },
onChange: { action: 'onChange', control: { type: null } },
},
parameters: {
controls: { expanded: true },
docs: { source: { state: 'open' } },
},
};
export default meta;

const Template: ComponentStory< typeof DateTimePicker > = ( {
currentDate,
onChange,
...args
} ) => {
const [ date, setDate ] = useState( currentDate );
useEffect( () => {
setDate( currentDate );
}, [ currentDate ] );
return (
<DateTimePicker
{ ...args }
currentDate={ date }
onChange={ ( newDate ) => {
setDate( newDate );
onChange?.( newDate );
} }
/>
);
};

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

export const WithEvents: ComponentStory<
typeof DateTimePicker
> = Template.bind( {} );
WithEvents.args = {
currentDate: new Date(),
events: [
{ date: daysFromNow( 2 ) },
{ date: daysFromNow( 4 ) },
{ date: daysFromNow( 6 ) },
{ date: daysFromNow( 8 ) },
],
};

export const WithInvalidDates: ComponentStory<
typeof DateTimePicker
> = Template.bind( {} );
WithInvalidDates.args = {
currentDate: new Date(),
isInvalidDate: isWeekend,
};
32 changes: 0 additions & 32 deletions packages/components/src/date-time/stories/time.js

This file was deleted.

51 changes: 51 additions & 0 deletions packages/components/src/date-time/stories/time.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/**
* External dependencies
*/
import type { ComponentMeta, ComponentStory } from '@storybook/react';

/**
* WordPress dependencies
*/
import { useState, useEffect } from '@wordpress/element';

/**
* Internal dependencies
*/
import TimePicker from '../time';

const meta: ComponentMeta< typeof TimePicker > = {
title: 'Components/TimePicker',
component: TimePicker,
argTypes: {
currentTime: { control: 'date' },
onChange: { action: 'onChange', control: { type: null } },
},
parameters: {
controls: { expanded: true },
docs: { source: { state: 'open' } },
},
};
export default meta;

const Template: ComponentStory< typeof TimePicker > = ( {
currentTime,
onChange,
...args
} ) => {
const [ time, setTime ] = useState( currentTime );
useEffect( () => {
setTime( currentTime );
}, [ currentTime ] );
return (
<TimePicker
{ ...args }
currentTime={ time }
onChange={ ( newTime ) => {
setTime( newTime );
onChange?.( newTime );
} }
/>
);
};

export const Default: ComponentStory< typeof TimePicker > = Template.bind( {} );
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;
}
Loading