Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(datepickers): use context rather than prop drilling #1311

Merged
merged 1 commit into from
Apr 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions packages/datepickers/.size-snapshot.json
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
{
"index.cjs.js": {
"bundled": 137973,
"minified": 77745,
"gzipped": 17268
"bundled": 137339,
"minified": 77485,
"gzipped": 17213
},
"index.esm.js": {
"bundled": 134810,
"minified": 75058,
"gzipped": 17087,
"bundled": 134176,
"minified": 74798,
"gzipped": 17033,
"treeshaked": {
"rollup": {
"code": 55704,
"code": 55444,
"import_statements": 546
},
"webpack": {
"code": 65130
"code": 64870
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@ import { Month } from './Month';
* @extends HTMLAttributes<HTMLDivElement>
*/
export const Calendar = forwardRef<HTMLDivElement, HTMLAttributes<HTMLDivElement>>((props, ref) => {
const { state, dispatch, locale, isCompact, minValue, maxValue, startValue, endValue } =
useDatepickerRangeContext();
const { state } = useDatepickerRangeContext();

return (
<StyledRangeCalendar
Expand All @@ -27,30 +26,8 @@ export const Calendar = forwardRef<HTMLDivElement, HTMLAttributes<HTMLDivElement
data-test-id="range-calendar"
{...props}
>
<Month
locale={locale}
displayDate={state.previewDate}
isCompact={isCompact}
isNextHidden
dispatch={dispatch}
minValue={minValue}
maxValue={maxValue}
startValue={startValue}
endValue={endValue}
hoverDate={state.hoverDate}
/>
<Month
locale={locale}
displayDate={addMonths(state.previewDate, 1)}
isCompact={isCompact}
isPreviousHidden
dispatch={dispatch}
minValue={minValue}
maxValue={maxValue}
startValue={startValue}
endValue={endValue}
hoverDate={state.hoverDate}
/>
<Month displayDate={state.previewDate} isNextHidden />
<Month displayDate={addMonths(state.previewDate, 1)} isPreviousHidden />
</StyledRangeCalendar>
);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,41 +33,27 @@ import {
StyledHighlight
} from '../../../styled';
import { getStartOfWeek } from '../../../utils/calendar-utils';
import { DatepickerRangeAction } from '../utils/datepicker-range-reducer';
import useDatepickerRangeContext from '../utils/useDatepickerRangeContext';

interface IMonthProps extends Omit<HTMLAttributes<HTMLDivElement>, 'onChange'> {
locale?: string;
displayDate: Date;
isCompact?: boolean;
isPreviousHidden?: boolean;
isNextHidden?: boolean;
dispatch: React.Dispatch<DatepickerRangeAction>;
minValue?: Date;
maxValue?: Date;
startValue?: Date;
endValue?: Date;
hoverDate?: Date;
}

export const Month = forwardRef<HTMLDivElement, IMonthProps>(
(
{
({ displayDate, isPreviousHidden, isNextHidden }, ref) => {
const {
state,
dispatch,
locale,
displayDate,
isCompact,
isPreviousHidden,
isNextHidden,
dispatch,
minValue,
maxValue,
startValue,
endValue,
hoverDate
},
ref
) => {
const { state, onChange } = useDatepickerRangeContext();
onChange
} = useDatepickerRangeContext();

const headerLabelFormatter = useCallback(
date => {
Expand Down Expand Up @@ -173,18 +159,18 @@ export const Month = forwardRef<HTMLDivElement, IMonthProps>(
(isAfter(date, startValue) || isSameDay(date, startValue)) &&
(isBefore(date, endValue) || isSameDay(date, endValue)) &&
!isSameDay(startValue, endValue);
} else if (startValue !== undefined && hoverDate !== undefined) {
} else if (startValue !== undefined && state.hoverDate !== undefined) {
isHighlighted =
(isAfter(date, startValue) || isSameDay(date, startValue)) &&
(isBefore(date, hoverDate) || isSameDay(date, hoverDate));
(isBefore(date, state.hoverDate) || isSameDay(date, state.hoverDate));
}

const isHighlightStart =
(isHighlighted && startValue && isSameDay(date, startValue)) || false;

const isHighlightEnd =
(isHighlighted && endValue && isSameDay(date, endValue)) ||
(hoverDate && isSameDay(date, hoverDate) && !isBefore(date, endValue!)) ||
(state.hoverDate && isSameDay(date, state.hoverDate) && !isBefore(date, endValue!)) ||
false;

let isInvalidDateRange =
Expand Down