Skip to content

Commit

Permalink
Merge pull request #5045 from laug/simplify-event-handlers
Browse files Browse the repository at this point in the history
Simplify event handlers
  • Loading branch information
martijnrusschen authored Aug 28, 2024
2 parents e22e88f + a85fabe commit 6b396e4
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 81 deletions.
28 changes: 8 additions & 20 deletions src/calendar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -369,17 +369,11 @@ export default class Calendar extends Component<CalendarProps, CalendarState> {
};

handleYearChange = (date: Date): void => {
if (this.props.onYearChange) {
this.props.onYearChange(date);
this.setState({ isRenderAriaLiveMessage: true });
}
this.props.onYearChange?.(date);
this.setState({ isRenderAriaLiveMessage: true });
if (this.props.adjustDateOnChange) {
if (this.props.onSelect) {
this.props.onSelect(date);
}
if (this.props.setOpen) {
this.props.setOpen(true);
}
this.props.onSelect(date);
this.props.setOpen?.(true);
}

this.props.setPreSelection && this.props.setPreSelection(date);
Expand All @@ -388,22 +382,16 @@ export default class Calendar extends Component<CalendarProps, CalendarState> {
handleMonthChange = (date: Date): void => {
this.handleCustomMonthChange(date);
if (this.props.adjustDateOnChange) {
if (this.props.onSelect) {
this.props.onSelect(date);
}
if (this.props.setOpen) {
this.props.setOpen(true);
}
this.props.onSelect(date);
this.props.setOpen?.(true);
}

this.props.setPreSelection && this.props.setPreSelection(date);
};

handleCustomMonthChange = (date: Date): void => {
if (this.props.onMonthChange) {
this.props.onMonthChange(date);
this.setState({ isRenderAriaLiveMessage: true });
}
this.props.onMonthChange?.(date);
this.setState({ isRenderAriaLiveMessage: true });
};

handleMonthYearChange = (date: Date): void => {
Expand Down
70 changes: 16 additions & 54 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -260,22 +260,10 @@ export default class DatePicker extends Component<
allowSameDay: false,
dateFormat: "MM/dd/yyyy",
dateFormatCalendar: "LLLL yyyy",
onChange() {},
disabled: false,
disabledKeyboardNavigation: false,
dropdownMode: "scroll" as const,
onFocus() {},
onBlur() {},
onKeyDown() {},
onInputClick() {},
onSelect() {},
onClickOutside() {},
onMonthChange() {},
onCalendarOpen() {},
onCalendarClose() {},
preventOpenOnFocus: false,
onYearChange() {},
onInputError() {},
monthsShown: 1,
readOnly: false,
withPortal: false,
Expand Down Expand Up @@ -751,41 +739,27 @@ export default class DatePicker extends Component<
const hasStartRange = startDate && !endDate;
const isRangeFilled = startDate && endDate;
if (noRanges) {
onChange
? onChange([changedDate, null], event)
: DatePicker.defaultProps.onChange;
onChange?.([changedDate, null], event);
} else if (hasStartRange) {
if (changedDate === null) {
onChange
? onChange([null, null], event)
: DatePicker.defaultProps.onChange;
onChange?.([null, null], event);
} else if (isDateBefore(changedDate, startDate)) {
if (swapRange) {
onChange
? onChange([changedDate, startDate], event)
: DatePicker.defaultProps.onChange;
onChange?.([changedDate, startDate], event);
} else {
onChange
? onChange([changedDate, null], event)
: DatePicker.defaultProps.onChange;
onChange?.([changedDate, null], event);
}
} else {
onChange
? onChange([startDate, changedDate], event)
: DatePicker.defaultProps.onChange;
onChange?.([startDate, changedDate], event);
}
}
if (isRangeFilled) {
onChange
? onChange([changedDate, null], event)
: DatePicker.defaultProps.onChange;
onChange?.([changedDate, null], event);
}
} else if (selectsMultiple) {
if (changedDate !== null) {
if (!selectedDates?.length) {
onChange
? onChange([changedDate], event)
: DatePicker.defaultProps.onChange;
onChange?.([changedDate], event);
} else {
const isChangedDateAlreadySelected = selectedDates.some(
(selectedDate) => isSameDay(selectedDate, changedDate),
Expand All @@ -796,26 +770,19 @@ export default class DatePicker extends Component<
(selectedDate) => !isSameDay(selectedDate, changedDate),
);

onChange
? onChange(nextDates, event)
: DatePicker.defaultProps.onChange;
onChange?.(nextDates, event);
} else {
onChange
? onChange([...selectedDates, changedDate], event)
: DatePicker.defaultProps.onChange;
onChange?.([...selectedDates, changedDate], event);
}
}
}
} else {
onChange
? onChange(changedDate, event)
: DatePicker.defaultProps.onChange;
onChange?.(changedDate, event);
}
}

if (!keepInput) {
const onSelect = this.props.onSelect ?? DatePicker.defaultProps.onSelect;
onSelect(changedDate, event);
this.props.onSelect?.(changedDate, event);
this.setState({ inputValue: null });
}
};
Expand Down Expand Up @@ -876,8 +843,7 @@ export default class DatePicker extends Component<
preSelection: changedDate,
});

const onChange = this.props.onChange ?? DatePicker.defaultProps.onChange;
onChange(changedDate);
this.props.onChange?.(changedDate);
if (this.props.shouldCloseOnSelect && !this.props.showTimeInput) {
this.sendFocusBackToInput();
this.setOpen(false);
Expand Down Expand Up @@ -913,7 +879,7 @@ export default class DatePicker extends Component<
eventKey === KeyType.ArrowUp ||
eventKey === KeyType.Enter
) {
this.onInputClick();
this.onInputClick?.();
}
return;
}
Expand Down Expand Up @@ -1126,9 +1092,7 @@ export default class DatePicker extends Component<
break;
}
if (!newSelection) {
if (this.props.onInputError) {
this.props.onInputError({ code: 1, msg: INPUT_ERR_1 });
}
this.props.onInputError?.({ code: 1, msg: INPUT_ERR_1 });
return;
}
event.preventDefault();
Expand Down Expand Up @@ -1175,11 +1139,9 @@ export default class DatePicker extends Component<

const { selectsRange, onChange } = this.props;
if (selectsRange) {
onChange
? onChange([null, null], event)
: DatePicker.defaultProps.onChange();
onChange?.([null, null], event);
} else {
onChange ? onChange(null, event) : DatePicker.defaultProps.onChange();
onChange?.(null, event);
}

this.setState({ inputValue: null });
Expand Down
10 changes: 3 additions & 7 deletions src/year_dropdown.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -118,20 +118,16 @@ export default class YearDropdown extends Component<
date: Date,
event?: React.MouseEvent<HTMLDivElement>,
): void => {
this.onSelect(date, event);
this.onSelect?.(date, event);
this.setOpen();
};

onSelect = (date: Date, event?: React.MouseEvent<HTMLDivElement>): void => {
if (this.props.onSelect) {
this.props.onSelect(date, event);
}
this.props.onSelect?.(date, event);
};

setOpen = (): void => {
if (this.props.setOpen) {
this.props.setOpen(true);
}
this.props.setOpen?.(true);
};

render(): JSX.Element {
Expand Down

0 comments on commit 6b396e4

Please sign in to comment.