From daea3f03105dd88f800caa450d99b6c2739461f1 Mon Sep 17 00:00:00 2001 From: = <=> Date: Fri, 12 Jan 2024 17:33:54 +0530 Subject: [PATCH] make month selector a table --- .../components/Calendar/Calendar.stories.tsx | 16 ++++- .../src/components/Calendar/YearCalendar.tsx | 72 ++++++++++--------- .../DatePicker/DatePicker.stories.tsx | 3 +- 3 files changed, 54 insertions(+), 37 deletions(-) diff --git a/packages/opub-ui/src/components/Calendar/Calendar.stories.tsx b/packages/opub-ui/src/components/Calendar/Calendar.stories.tsx index 35f80fce..e4acbfa1 100644 --- a/packages/opub-ui/src/components/Calendar/Calendar.stories.tsx +++ b/packages/opub-ui/src/components/Calendar/Calendar.stories.tsx @@ -1,9 +1,9 @@ import { Calendar } from './Calendar' import { RangeCalendar } from './RangeCalendar' import { YearCalendar } from './YearCalendar' -import { isWeekend } from '@internationalized/date' +import { getLocalTimeZone, isWeekend } from '@internationalized/date' import { Meta, StoryObj } from '@storybook/react' -import { useLocale } from 'react-aria' +import { useDateFormatter, useLocale } from 'react-aria' /** * A calendar displays one or more date grids and allows users to select a single date. @@ -49,7 +49,17 @@ export const CalendarRange: RangeStory = { export const Year: any = { render: ({ ...args }) => { - return + let formatter = useDateFormatter({ dateStyle: 'medium' }) + + return ( + { + const formatted = formatter.format(date.toDate(getLocalTimeZone())) + console.log(formatted) + }} + /> + ) }, args: {}, } diff --git a/packages/opub-ui/src/components/Calendar/YearCalendar.tsx b/packages/opub-ui/src/components/Calendar/YearCalendar.tsx index 383abf93..f4a16a88 100644 --- a/packages/opub-ui/src/components/Calendar/YearCalendar.tsx +++ b/packages/opub-ui/src/components/Calendar/YearCalendar.tsx @@ -13,8 +13,8 @@ import { useLocale, } from 'react-aria' import { - CalendarStateOptions, CalendarState, + CalendarStateOptions, useCalendarState, } from 'react-stately' @@ -73,7 +73,7 @@ export const YearCalendar = ( - + ) } @@ -92,7 +92,7 @@ function YearDropdown({ state }: { state: CalendarState }) { ) } -function MonthSelector({ state }: any) { +function MonthSelector({ state, props }: any) { const [_, setChosenDate] = React.useState(null) let onClick = (e: React.MouseEvent) => { @@ -104,56 +104,62 @@ function MonthSelector({ state }: any) { } const monthsObj: { - [key: number]: { month: string; value: number }[] + [key: number]: { month: string; value: number; label: string }[] } = { 0: [ - { month: 'Jan', value: 1 }, - { month: 'Feb', value: 2 }, - { month: 'Mar', value: 3 }, - { month: 'Apr', value: 4 }, + { month: 'Jan', value: 1, label: 'January' }, + { month: 'Feb', value: 2, label: 'February' }, + { month: 'Mar', value: 3, label: 'March' }, + { month: 'Apr', value: 4, label: 'April' }, ], 1: [ - { month: 'May', value: 5 }, - { month: 'Jun', value: 6 }, - { month: 'Jul', value: 7 }, - { month: 'Aug', value: 8 }, + { month: 'May', value: 5, label: 'May' }, + { month: 'Jun', value: 6, label: 'June' }, + { month: 'Jul', value: 7, label: 'July' }, + { month: 'Aug', value: 8, label: 'August' }, ], 2: [ - { month: 'Sep', value: 9 }, - { month: 'Oct', value: 10 }, - { month: 'Nov', value: 11 }, - { month: 'Dec', value: 12 }, + { month: 'Sep', value: 9, label: 'September' }, + { month: 'Oct', value: 10, label: 'October' }, + { month: 'Nov', value: 11, label: 'November' }, + { month: 'Dec', value: 12, label: 'December' }, ], } const calendar = [...new Array(3).keys()].map((_, i) => { return ( -
+ {monthsObj[i].map((mon, i) => { const selected = state.focusedDate.month === mon.value && state.focusedDate.year === state.value?.year return ( - + + + ) })} -
+ ) }) - return
{calendar}
+ return ( + + {calendar} +
+ ) } diff --git a/packages/opub-ui/src/components/DatePicker/DatePicker.stories.tsx b/packages/opub-ui/src/components/DatePicker/DatePicker.stories.tsx index dfbbc113..22f63488 100644 --- a/packages/opub-ui/src/components/DatePicker/DatePicker.stories.tsx +++ b/packages/opub-ui/src/components/DatePicker/DatePicker.stories.tsx @@ -41,6 +41,7 @@ export const Month: Story = { args: { label: 'Month Picker', onChange: (val) => console.log(val), + defaultValue: parseDate('2023-02-04'), }, } @@ -50,7 +51,7 @@ export const DisabledDates: StoryRange = { label: 'Date Picker', minValue: today(getLocalTimeZone()), defaultValue: { - start: parseDate('2022-02-03'), + start: parseDate('2022-02-04'), end: parseDate('2022-05-03'), }, errorMessage: 'Date must be in the future',