Skip to content

Commit

Permalink
simplifying helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
MichalPaszowski committed Aug 3, 2022
1 parent 6cb3eb0 commit f24b312
Showing 1 changed file with 39 additions and 37 deletions.
76 changes: 39 additions & 37 deletions packages/react-components/src/components/Datepicker/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,21 +36,18 @@ export const calculateDatePickerMonth = (
initialToDate?: Date,
toMonth?: Date
): Date => {
if (initialToDate) {
const forcePreviousMonth =
initialFromDate && !isSameMonth(initialFromDate, initialToDate);

if (
forcePreviousMonth ||
(toMonth && isSameMonth(initialToDate, toMonth))
) {
return subMonths(initialToDate, 1);
}

return initialToDate;
if (!initialToDate) {
return subMonths(toMonth || new Date(), 1);
}

return subMonths(toMonth || new Date(), 1);
const forcePreviousMonth =
initialFromDate && !isSameMonth(initialFromDate, initialToDate);

if (forcePreviousMonth || (toMonth && isSameMonth(initialToDate, toMonth))) {
return subMonths(initialToDate, 1);
}

return initialToDate;
};

export const getRangeDatePickerModifiers = (
Expand All @@ -73,7 +70,9 @@ export const getRangeDatePickerModifiers = (
...base,
[styles[`${baseClass}__day--end`]]: to,
};
} else if (diff < 0) {
}

if (diff < 0) {
return {
...base,
[styles[`${baseClass}__day--start`]]: to,
Expand Down Expand Up @@ -104,30 +103,33 @@ export const getInitialStateFromProps = (
): Partial<IRangeDatePickerState> => {
const state: Partial<IRangeDatePickerState> = {};

if (props.initialSelectedItemKey) {
const selectedOption = getSelectedOption(
props.initialSelectedItemKey,
props.options
);

if (!selectedOption) {
return {};
}

state.selectedItem = props.initialSelectedItemKey;

if (!selectedOption.isManual) {
return state;
}

if (props.initialFromDate) {
state.from = props.initialFromDate;
}
if (props.initialToDate) {
state.to = props.initialToDate;
state.temporaryTo = props.initialToDate;
}
if (!props.initialSelectedItemKey) {
return state;
}

const selectedOption = getSelectedOption(
props.initialSelectedItemKey,
props.options
);

if (!selectedOption) {
return {};
}

state.selectedItem = props.initialSelectedItemKey;

if (!selectedOption.isManual) {
return state;
}

if (props.initialFromDate) {
state.from = props.initialFromDate;
}
if (props.initialToDate) {
state.to = props.initialToDate;
state.temporaryTo = props.initialToDate;
}

return state;
};

Expand Down

0 comments on commit f24b312

Please sign in to comment.