Skip to content

Commit

Permalink
feat: add new actions show and toggle for year, month, day (for examp…
Browse files Browse the repository at this point in the history
…le showDay)

BREAKING CHAMNGE: Remove dayMonthYear, dayMonth, dayYear and monthYear action.
use new toggle and show action instead
  • Loading branch information
aliakbarazizi committed Oct 13, 2023
1 parent bce06e8 commit 6b5123e
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 21 deletions.
37 changes: 27 additions & 10 deletions src/components/datepicker/button/Button.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,9 @@ const meta = {
'toggle',
'next',
'prev',
'dayMonthYear',
'dayMonth',
'dayYear',
'monthYear',
'year',
'month',
'day',
'today',
'todayHour',
],
Expand Down Expand Up @@ -91,21 +90,39 @@ export const Toggle = {
],
} satisfies Story;

export const DayMonth = {
export const ShowYear = {
args: {
action: 'dayMonth',
action: 'showYear',
className:
'leading-2 p-2 text-lg font-semibold hover:bg-gray-700 hover:text-white',
children: 'Toggle Day-Month',
children: 'Show year',
},
} satisfies Story;

export const DayYear = {
export const ToggleYear = {
args: {
action: 'dayYear',
action: 'toggleYear',
className:
'leading-2 p-2 text-lg font-semibold hover:bg-gray-700 hover:text-white',
children: 'Toggle Day-Year',
children: 'Toggle year',
},
} satisfies Story;

export const ShowMonth = {
args: {
action: 'showMonth',
className:
'leading-2 p-2 text-lg font-semibold hover:bg-gray-700 hover:text-white',
children: 'Show month',
},
} satisfies Story;

export const ToggleMonth = {
args: {
action: 'toggleMonth',
className:
'leading-2 p-2 text-lg font-semibold hover:bg-gray-700 hover:text-white',
children: 'Toggle month',
},
} satisfies Story;

Expand Down
20 changes: 9 additions & 11 deletions src/components/datepicker/button/Button.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { ElementType, Ref, useContext, useRef } from 'react';
import {
Actions,
Action,
DatepickerSlot,
useDatepickerSlot,
} from '../../../context/context';
Expand All @@ -25,8 +25,7 @@ export type ButtonProps<
*
* The only exception is `today` and `todayHour` since they set value for all pickers.
*
* For `open`, `close` or `toggle` the default target Picker is the first Picker in siblings.
* For others the default target Picker is the parent Picker.
* The default target Picker is the parent Picker.
* If no picker found, it will be the first Picker.
*
* Action can be one of these
Expand All @@ -37,12 +36,11 @@ export type ButtonProps<
* - `toggle` or `'toggle' + pickerId` close the calendar
* - `next` or `'next' + pickerId` go to next month or year (depend on calendar mode)
* - `prev` or `'prev' + pickerId` go to prev month or year (depend on calendar mode)
* - `dayMonthYear` or `'dayMonthYear' + pickerId` toggle calendar mode between day and month and year
* - `dayMonth` or `'dayMonth' + pickerId` toggle calendar mode between day and month
* - `dayYear` or `'dayYear' + pickerId` toggle calendar mode between day and year
* - `monthYear` or `'monthYear' + pickerId` toggle calendar mode between month and year
* - `year` or `'year' + pickerId` set showing items to year
* - `month` or `'month' + pickerId` set showing items to month
* - `day` or `'day' + pickerId` set showing items to day
*/
action: Actions;
action: Action;
}
>;

Expand All @@ -51,7 +49,7 @@ export const Button = forwardRef(
{ action, ...props }: ButtonProps<ElemenElementTag>,
ref: Ref<HTMLElement>,
) => {
const { id, nestedLevel } = useContext(PickerContext);
const { id } = useContext(PickerContext);

const buttonRef = useRef<HTMLButtonElement | null>(null);
useSyncRef(buttonRef, ref);
Expand All @@ -61,8 +59,8 @@ export const Button = forwardRef(
const ourProps = {
onClick: () =>
dispatch({
type: action,
payload: { ref: buttonRef, nestedLevel, pickerId: id },
type: 'action',
payload: { action, ref: buttonRef, pickerId: id },
}),
};

Expand Down

0 comments on commit 6b5123e

Please sign in to comment.