Skip to content

Commit

Permalink
feat(CalendarMonth): event param updates
Browse files Browse the repository at this point in the history
  • Loading branch information
wise-king-sullyman committed Mar 2, 2023
1 parent ef75271 commit 0664407
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 14 deletions.
20 changes: 10 additions & 10 deletions packages/react-core/src/components/CalendarMonth/CalendarMonth.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,11 @@ export interface CalendarProps extends CalendarFormat, Omit<React.HTMLProps<HTML
/** Flag to set browser focus on the passed date. **/
isDateFocused?: boolean;
/** Callback when date is selected. */
onChange?: (date: Date) => void;
onChange?: (event: React.MouseEvent<HTMLButtonElement, MouseEvent>, date: Date) => void;
/** Callback when month or year is changed. */
onMonthChange?: (
newDate?: Date,
event?: React.MouseEvent | React.ChangeEvent | React.FormEvent<HTMLInputElement>
event?: React.MouseEvent | React.ChangeEvent | React.FormEvent<HTMLInputElement>,
newDate?: Date
) => void;
/** @hide Internal prop to allow pressing escape in select menu to not close popover. */
onSelectToggle?: (open: boolean) => void;
Expand Down Expand Up @@ -187,11 +187,11 @@ export const CalendarMonth = ({
}
}, [focusedDate, isDateFocused, focusedDateValidated, focusRef]);

const onMonthClick = (newDate: Date, ev: React.MouseEvent) => {
const onMonthClick = (ev: React.MouseEvent, newDate: Date) => {
setFocusedDate(newDate);
setHoveredDate(newDate);
setShouldFocus(false);
onMonthChange(newDate, ev);
onMonthChange(ev, newDate);
};

const onKeyDown = (ev: React.KeyboardEvent<HTMLTableSectionElement>) => {
Expand Down Expand Up @@ -252,7 +252,7 @@ export const CalendarMonth = ({
<Button
variant="plain"
aria-label={prevMonthAriaLabel}
onClick={(ev: React.MouseEvent) => onMonthClick(prevMonth, ev)}
onClick={(ev: React.MouseEvent) => onMonthClick(ev, prevMonth)}
>
<AngleLeftIcon aria-hidden={true} />
</Button>
Expand Down Expand Up @@ -282,7 +282,7 @@ export const CalendarMonth = ({
setFocusedDate(newDate);
setHoveredDate(newDate);
setShouldFocus(false);
onMonthChange(newDate, ev);
onMonthChange(ev, newDate);
}, 0);
}}
variant="single"
Expand All @@ -306,7 +306,7 @@ export const CalendarMonth = ({
setFocusedDate(newDate);
setHoveredDate(newDate);
setShouldFocus(false);
onMonthChange(newDate, ev);
onMonthChange(ev, newDate);
}}
/>
</div>
Expand All @@ -315,7 +315,7 @@ export const CalendarMonth = ({
<Button
variant="plain"
aria-label={nextMonthAriaLabel}
onClick={(ev: React.MouseEvent) => onMonthClick(nextMonth, ev)}
onClick={(ev: React.MouseEvent) => onMonthClick(ev, nextMonth)}
>
<AngleRightIcon aria-hidden={true} />
</Button>
Expand Down Expand Up @@ -377,7 +377,7 @@ export const CalendarMonth = ({
!isValid && styles.modifiers.disabled
)}
type="button"
onClick={() => onChange(date)}
onClick={(event) => onChange(event, date)}
onMouseOver={() => setHoveredDate(date)}
tabIndex={isFocused ? 0 : -1}
disabled={!isValid}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ import { CalendarMonth, Title, CalendarMonthInlineProps } from '@patternfly/reac
export const CalendarMonthSelectableDate: React.FunctionComponent = () => {
const [date, setDate] = React.useState(new Date(2020, 10, 24));

const onMonthChange = (newDate: Date) => {
const onMonthChange = (
_event: React.MouseEvent | React.ChangeEvent | React.FormEvent<HTMLInputElement> | undefined,
newDate: Date
) => {
// eslint-disable-next-line no-console
console.log(`updated month: ${newDate.getMonth()}, updated year: ${newDate.getFullYear()}`);
};
Expand All @@ -21,7 +24,12 @@ export const CalendarMonthSelectableDate: React.FunctionComponent = () => {

return (
<>
<CalendarMonth date={date} onChange={setDate} onMonthChange={onMonthChange} inlineProps={inlineProps} />
<CalendarMonth
date={date}
onChange={(_event: React.MouseEvent<HTMLButtonElement, MouseEvent>, date: Date) => setDate(date)}
onMonthChange={onMonthChange}
inlineProps={inlineProps}
/>
<pre>Selected date: {date.toString()}</pre>
</>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ const DatePickerBase = (
}
};

const onDateClick = (newValueDate: Date) => {
const onDateClick = (_event: React.MouseEvent<HTMLButtonElement, MouseEvent>, newValueDate: Date) => {
const newValue = dateFormat(newValueDate);
setValue(newValue);
setValueDate(newValueDate);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export const DateTimePicker: React.FunctionComponent = () => {
setIsCalendarOpen(false);
};

const onSelectCalendar = (newValueDate: Date) => {
const onSelectCalendar = (_event: React.MouseEvent<HTMLButtonElement, MouseEvent>, newValueDate: Date) => {
const newValue = dateFormat(newValueDate);
setValueDate(newValue);
setIsCalendarOpen(!isCalendarOpen);
Expand Down

0 comments on commit 0664407

Please sign in to comment.