-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use TypeScript and controls for
DateTimePicker
's stories (#40986)
* Use TypeScript and controls for DateTimePicker, DatePicker and TimePicker's stories * DRY up daysFromNow and isWeekend * Show control for events param * Make DateTimePicker stories more interactive * Pass onChange up to storybook
- Loading branch information
1 parent
82ecd50
commit c541147
Showing
12 changed files
with
261 additions
and
164 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
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
This file was deleted.
Oops, something went wrong.
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,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, | ||
}; |
This file was deleted.
Oops, something went wrong.
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,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, | ||
}; |
This file was deleted.
Oops, something went wrong.
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,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( {} ); |
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,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; | ||
} |
Oops, something went wrong.