Skip to content

Commit

Permalink
fix DatePikcerProps
Browse files Browse the repository at this point in the history
  • Loading branch information
yuki0410-dev committed Jun 29, 2024
1 parent f90932c commit ee69d7b
Showing 1 changed file with 43 additions and 24 deletions.
67 changes: 43 additions & 24 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ export type DatePickerProps = Omit<
| "selectsMultiple"
| "dropdownMode"
> &
Pick<AdditionalProps, "excludeScrollbar"> &
Partial<Pick<AdditionalProps, "excludeScrollbar">> &
Pick<CalendarIconProps, "icon"> &
Omit<PortalProps, "children" | "portalId"> &
Omit<
Expand Down Expand Up @@ -197,7 +197,7 @@ export type DatePickerProps = Omit<
onChangeRaw?: (
event?: React.MouseEvent<HTMLElement> | React.KeyboardEvent<HTMLElement>,
) => void;
onSelect: (
onSelect?: (
date: Date | null,
event?:
| React.MouseEvent<HTMLElement, MouseEvent>
Expand All @@ -207,7 +207,7 @@ export type DatePickerProps = Omit<
| {
selectsRange?: never;
selectsMultiple?: never;
onChange: (
onChange?: (
date: Date | null,
event?:
| React.MouseEvent<HTMLElement>
Expand All @@ -217,7 +217,7 @@ export type DatePickerProps = Omit<
| {
selectsRange: true;
selectsMultiple?: never;
onChange: (
onChange?: (
date: [Date | null, Date | null],
event?:
| React.MouseEvent<HTMLElement>
Expand All @@ -227,7 +227,7 @@ export type DatePickerProps = Omit<
| {
selectsRange?: never;
selectsMultiple: true;
onChange: (
onChange?: (
date: Date[] | null,
event?:
| React.MouseEvent<HTMLElement>
Expand Down Expand Up @@ -752,27 +752,41 @@ export default class DatePicker extends Component<
const hasStartRange = startDate && !endDate;
const isRangeFilled = startDate && endDate;
if (noRanges) {
onChange([changedDate, null], event);
onChange
? onChange([changedDate, null], event)
: DatePicker.defaultProps.onChange;

Check warning on line 757 in src/index.tsx

View check run for this annotation

Codecov / codecov/patch

src/index.tsx#L757

Added line #L757 was not covered by tests
} else if (hasStartRange) {
if (changedDate === null) {
onChange([null, null], event);
onChange
? onChange([null, null], event)
: DatePicker.defaultProps.onChange;

Check warning on line 762 in src/index.tsx

View check run for this annotation

Codecov / codecov/patch

src/index.tsx#L762

Added line #L762 was not covered by tests
} else if (isDateBefore(changedDate, startDate)) {
if (swapRange) {
onChange([changedDate, startDate], event);
onChange
? onChange([changedDate, startDate], event)
: DatePicker.defaultProps.onChange;

Check warning on line 767 in src/index.tsx

View check run for this annotation

Codecov / codecov/patch

src/index.tsx#L767

Added line #L767 was not covered by tests
} else {
onChange([changedDate, null], event);
onChange
? onChange([changedDate, null], event)
: DatePicker.defaultProps.onChange;

Check warning on line 771 in src/index.tsx

View check run for this annotation

Codecov / codecov/patch

src/index.tsx#L771

Added line #L771 was not covered by tests
}
} else {
onChange([startDate, changedDate], event);
onChange
? onChange([startDate, changedDate], event)
: DatePicker.defaultProps.onChange;

Check warning on line 776 in src/index.tsx

View check run for this annotation

Codecov / codecov/patch

src/index.tsx#L776

Added line #L776 was not covered by tests
}
}
if (isRangeFilled) {
onChange([changedDate, null], event);
onChange
? onChange([changedDate, null], event)
: DatePicker.defaultProps.onChange;

Check warning on line 782 in src/index.tsx

View check run for this annotation

Codecov / codecov/patch

src/index.tsx#L782

Added line #L782 was not covered by tests
}
} else if (selectsMultiple) {
if (changedDate !== null) {
if (!selectedDates?.length) {
onChange([changedDate], event);
onChange
? onChange([changedDate], event)
: DatePicker.defaultProps.onChange;

Check warning on line 789 in src/index.tsx

View check run for this annotation

Codecov / codecov/patch

src/index.tsx#L789

Added line #L789 was not covered by tests
} else {
const isChangedDateAlreadySelected = selectedDates.some(
(selectedDate) => isSameDay(selectedDate, changedDate),
Expand All @@ -783,19 +797,26 @@ export default class DatePicker extends Component<
(selectedDate) => !isSameDay(selectedDate, changedDate),
);

onChange(nextDates, event);
onChange
? onChange(nextDates, event)
: DatePicker.defaultProps.onChange;

Check warning on line 802 in src/index.tsx

View check run for this annotation

Codecov / codecov/patch

src/index.tsx#L802

Added line #L802 was not covered by tests
} else {
onChange([...selectedDates, changedDate], event);
onChange
? onChange([...selectedDates, changedDate], event)
: DatePicker.defaultProps.onChange;

Check warning on line 806 in src/index.tsx

View check run for this annotation

Codecov / codecov/patch

src/index.tsx#L806

Added line #L806 was not covered by tests
}
}
}
} else {
onChange(changedDate, event);
onChange
? onChange(changedDate, event)
: DatePicker.defaultProps.onChange;

Check warning on line 813 in src/index.tsx

View check run for this annotation

Codecov / codecov/patch

src/index.tsx#L813

Added line #L813 was not covered by tests
}
}

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

this.props.onChange(changedDate);
const onChange = this.props.onChange ?? DatePicker.defaultProps.onChange;
onChange(changedDate);
if (this.props.shouldCloseOnSelect && !this.props.showTimeInput) {
this.sendFocusBackToInput();
this.setOpen(false);
Expand Down Expand Up @@ -1154,16 +1176,13 @@ export default class DatePicker extends Component<

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

Check warning on line 1181 in src/index.tsx

View check run for this annotation

Codecov / codecov/patch

src/index.tsx#L1181

Added line #L1181 was not covered by tests
} else {
onChange(null, event);
onChange ? onChange(null, event) : DatePicker.defaultProps.onChange();
}

if (this.props.selectsRange) {
this.props.onChange([null, null], event);
} else {
this.props.onChange(null, event);
}
this.setState({ inputValue: null });
};

Expand Down

0 comments on commit ee69d7b

Please sign in to comment.