-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2ebf49f
commit 1abb2fe
Showing
6 changed files
with
148 additions
and
33 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
124 changes: 112 additions & 12 deletions
124
packages/react-components/src/components/DatePicker/DatePicker.stories.tsx
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 |
---|---|---|
@@ -1,25 +1,125 @@ | ||
import * as React from 'react'; | ||
|
||
import { Meta } from '@storybook/react'; | ||
import { Meta, StoryFn } from '@storybook/react'; | ||
|
||
import { DatePicker } from './DatePicker'; | ||
import { IDatePickerProps } from './types'; | ||
|
||
const DISABLED_RANGE_OPTIONS = [ | ||
'No Disabled Dates', | ||
'Disable Before Current Date', | ||
'Disable After Current Date', | ||
]; | ||
|
||
export default { | ||
title: 'Components/DatePicker', | ||
component: DatePicker, | ||
argTypes: { | ||
weekStartsOn: { | ||
description: 'Number representing first day of the week', | ||
table: { | ||
type: { | ||
summary: 'number: 0-6', | ||
detail: '0 - Sunday, 1 - Monday and so on, up to 6 - Saturday', | ||
}, | ||
}, | ||
defaultValue: 1, | ||
control: { | ||
type: 'number', | ||
}, | ||
}, | ||
disabled: { | ||
description: 'Range of disabled dates', | ||
control: { | ||
type: 'select', | ||
}, | ||
options: DISABLED_RANGE_OPTIONS, | ||
defaultValue: DISABLED_RANGE_OPTIONS[0], | ||
table: { | ||
type: { | ||
summary: 'object', | ||
detail: | ||
'{ before: Date } | { after: Date } | { dayOfWeek: []: array of numbers 0-6 } | undefined', | ||
}, | ||
}, | ||
}, | ||
month: { | ||
description: 'Providing this prop will make the date picker controlled', | ||
control: { | ||
type: 'date', | ||
}, | ||
}, | ||
startMonth: { | ||
description: 'Start month for the date picker', | ||
control: { | ||
type: 'date', | ||
}, | ||
}, | ||
endMonth: { | ||
description: 'End month for the date picker', | ||
control: { | ||
type: 'date', | ||
}, | ||
}, | ||
}, | ||
} as Meta<typeof DatePicker>; | ||
|
||
export const Default = (): React.ReactElement => <DatePicker />; | ||
const StoryTemplate = (args: IDatePickerProps) => { | ||
const { month, ...restArgs } = args; | ||
|
||
const [selectedDate, setSelectedDate] = React.useState<Date | undefined>(); | ||
const [selectedMonth, setSelectedMonth] = React.useState<Date | undefined>(); | ||
const [initialMonth] = React.useState<Date | undefined>( | ||
month ? new Date(month) : undefined | ||
); | ||
|
||
React.useEffect(() => { | ||
const newMonth = month ? new Date(month) : undefined; | ||
|
||
if (newMonth !== initialMonth) { | ||
setSelectedMonth(newMonth); | ||
} | ||
}, [month, initialMonth]); | ||
|
||
const getDisabledDates = () => { | ||
switch (restArgs.disabled as unknown) { | ||
case DISABLED_RANGE_OPTIONS[1]: | ||
return { before: new Date() }; | ||
case DISABLED_RANGE_OPTIONS[2]: | ||
return { after: new Date() }; | ||
default: | ||
return undefined; | ||
} | ||
}; | ||
|
||
return ( | ||
<DatePicker | ||
{...restArgs} | ||
mode="single" | ||
selected={selectedDate} | ||
month={selectedMonth || initialMonth} | ||
onSelect={setSelectedDate} | ||
onMonthChange={setSelectedMonth} | ||
disabled={getDisabledDates()} | ||
/> | ||
); | ||
}; | ||
|
||
export const Default: StoryFn = StoryTemplate.bind({}); | ||
Default.args = {}; | ||
|
||
export const WithCustomCurrentDate = () => ( | ||
<DatePicker today={new Date('01/20/2024')} /> | ||
); | ||
export const WithCustomCurrentDate: StoryFn = StoryTemplate.bind({}); | ||
WithCustomCurrentDate.args = { | ||
today: new Date('01/20/2024'), | ||
}; | ||
|
||
export const WithDatesBetweenTwoMonths = () => ( | ||
<DatePicker | ||
startMonth={new Date('11/20/2023')} | ||
endMonth={new Date('03/20/2024')} | ||
/> | ||
); | ||
export const WithDatesBetweenTwoMonths: StoryFn = StoryTemplate.bind({}); | ||
WithDatesBetweenTwoMonths.args = { | ||
startMonth: new Date('11/20/2023'), | ||
endMonth: new Date('01/20/2024'), | ||
}; | ||
|
||
export const WithCustomFirstDayOfWeek = () => <DatePicker weekStartsOn={0} />; | ||
export const WithCustomFirstDayOfWeek: StoryFn = StoryTemplate.bind({}); | ||
WithCustomFirstDayOfWeek.args = { | ||
weekStartsOn: 0, | ||
}; |
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 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