diff --git a/README.md b/README.md index bddba80..ae2c997 100644 --- a/README.md +++ b/README.md @@ -124,13 +124,17 @@ Non Chakra-related configurations : ### other props: -Name | Type | Default value | Description -----------------------|------------------------|-------------------------|-------------- -name | string | undefined | name attribute for `` element -usePortal | boolean | undefined | to prevent parent styles from clipping or hiding content -defaultIsOpen | boolean | undefined | open the date panel at the beginning -minDate | Date | undefined | minimum date -maxDate | Date | undefined | maximum date +Name | Type | Default value | Description | Single/Range +----------------------|------------------------|-------------------------|--------------------------------------------------------|--------------- +name | string | undefined | name attribute for `` element | both +usePortal | boolean | undefined | to prevent parent styles from clipping or hiding content| both +defaultIsOpen | boolean | undefined | open the date panel at the beginning | both +minDate | Date | undefined | minimum date | both +maxDate | Date | undefined | maximum date | both +onMonthViewChange | ({calendars, offset}: {calendars: Calendar[]; offset: number;}) => void | undefined | runs when the current month is changed | single +onPopoverOpen | () => void | undefined | runs when the calendar popover is opened | single +customDateButton | (dateObj: DateObjExtended, { dateProps, btnProps }: CustomDateButtonProps) => ReactElement; | undefined | custom date button renderer that replaces the original one | single + For version < `npm@0.1.6`:
`dayOfMonthBtnProps` extends from `ButtonProps` and has only `selectedBg` support, diff --git a/src/components/calendarPanel.tsx b/src/components/calendarPanel.tsx index 1ee8370..f79fe0f 100644 --- a/src/components/calendarPanel.tsx +++ b/src/components/calendarPanel.tsx @@ -10,7 +10,12 @@ import { import { useDayzed, Props as DayzedHookProps } from 'dayzed'; import { ArrowKeysReact } from '../utils/reactKeysArrow'; import React, { useCallback, useMemo } from 'react'; -import { CalendarConfigs, DatepickerProps } from '../utils/commonTypes'; +import { + CalendarConfigs, + DatepickerProps, + CustomDateButton, + OnMonthViewChange, +} from '../utils/commonTypes'; import { DatepickerBackBtns, DatepickerForwardBtns } from './dateNavBtns'; import { DayOfMonth } from './dayOfMonth'; @@ -19,6 +24,8 @@ interface CalendarPanelProps extends DatepickerProps { configs: CalendarConfigs; onMouseEnterHighlight?: (date: Date) => void; isInRange?: (date: Date) => boolean | null; + onMonthViewChange?: OnMonthViewChange; + customDateButton?: CustomDateButton; } export const CalendarPanel: React.FC = ({ @@ -27,6 +34,8 @@ export const CalendarPanel: React.FC = ({ propsConfigs, onMouseEnterHighlight, isInRange, + onMonthViewChange, + customDateButton, }) => { const renderProps = useDayzed(dayzedHookProps); const { calendars, getBackProps, getForwardProps } = renderProps; @@ -96,6 +105,7 @@ export const CalendarPanel: React.FC = ({ calendars={calendars} getBackProps={getBackProps} propsConfigs={propsConfigs} + onMonthViewChange={onMonthViewChange} /> {configs.monthNames[calendar.month]} {calendar.year} @@ -104,6 +114,7 @@ export const CalendarPanel: React.FC = ({ calendars={calendars} getForwardProps={getForwardProps} propsConfigs={propsConfigs} + onMonthViewChange={onMonthViewChange} /> @@ -128,6 +139,7 @@ export const CalendarPanel: React.FC = ({ onMouseEnter={() => { if (onMouseEnterHighlight) onMouseEnterHighlight(date); }} + customDateButton={customDateButton} /> ); }); diff --git a/src/components/dateNavBtns.tsx b/src/components/dateNavBtns.tsx index f49be6b..bb3a1c8 100644 --- a/src/components/dateNavBtns.tsx +++ b/src/components/dateNavBtns.tsx @@ -1,11 +1,15 @@ import { Button, ButtonProps } from '@chakra-ui/react'; import { Calendar, GetBackForwardPropsOptions } from 'dayzed'; -import React, { Fragment } from 'react'; -import { DatepickerProps } from '../utils/commonTypes'; +import React from 'react'; +import { + DatepickerProps, + OnMonthViewChange, +} from '../utils/commonTypes'; export interface DatepickerBackBtnsProps extends DatepickerProps { calendars: Calendar[]; getBackProps: (data: GetBackForwardPropsOptions) => Record; + onMonthViewChange?: OnMonthViewChange; } const DefaultBtnStyle: ButtonProps = { @@ -16,60 +20,77 @@ const DefaultBtnStyle: ButtonProps = { export const DatepickerBackBtns: React.FC = ( props ) => { - const { calendars, getBackProps } = props; + const { calendars, getBackProps, onMonthViewChange } = props; const customBtnProps = props.propsConfigs?.dateNavBtnProps; + + const backProps = getBackProps({ calendars }); + const backPropsWithOffset = getBackProps({ calendars, offset: 12 }); return ( - + <> - + ); }; export interface DatepickerForwardBtnsProps extends DatepickerProps { calendars: Calendar[]; getForwardProps: (data: GetBackForwardPropsOptions) => Record; + onMonthViewChange?: OnMonthViewChange; } export const DatepickerForwardBtns: React.FC = ( props ) => { - const { calendars, getForwardProps } = props; + const { calendars, getForwardProps, onMonthViewChange } = props; const customBtnProps = props.propsConfigs?.dateNavBtnProps; + + const forwardProps = getForwardProps({ calendars }); + const forwardPropsWithOffset = getForwardProps({ calendars, offset: 12 }); return ( - + <> - + ); }; diff --git a/src/components/dayOfMonth.tsx b/src/components/dayOfMonth.tsx index e109417..19eab40 100644 --- a/src/components/dayOfMonth.tsx +++ b/src/components/dayOfMonth.tsx @@ -1,13 +1,18 @@ import { Button } from '@chakra-ui/react'; import { DateObj, RenderProps } from 'dayzed'; import React, { useMemo } from 'react'; -import { DatepickerProps, DayOfMonthBtnStyleProps } from '../utils/commonTypes'; +import { + CustomDateButton, + DatepickerProps, + DayOfMonthBtnStyleProps, +} from '../utils/commonTypes'; interface DayOfMonthProps extends DatepickerProps { renderProps: RenderProps; isInRange?: boolean | null; dateObj: DateObj; onMouseEnter?: React.MouseEventHandler | undefined; + customDateButton?: CustomDateButton; } const halfGap = 0.125; //default Chakra-gap-space-1 is 0.25rem @@ -18,6 +23,7 @@ export const DayOfMonth: React.FC = ({ isInRange, renderProps, onMouseEnter, + customDateButton, }) => { const { date, selected, selectable, today } = dateObj; const { getDateProps } = renderProps; @@ -77,7 +83,19 @@ export const DayOfMonth: React.FC = ({ ] ); - return ( + return customDateButton ? ( + customDateButton( + { ...dateObj, isInRange }, + { + dateProps: getDateProps({ + dateObj, + disabled: !selectable, + onMouseEnter, + }), + btnProps: styleBtnProps, + } + ) + ) : (