Skip to content

Commit

Permalink
refactor: simplify Datepicker refs
Browse files Browse the repository at this point in the history
  • Loading branch information
Martí Malek committed Oct 6, 2023
1 parent 84af4a4 commit 6453d08
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 70 deletions.
123 changes: 59 additions & 64 deletions src/components/Datepicker/Datepicker.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { FC, RefObject } from 'react';
import React from 'react';
import { useDatepicker, MonthType, UseDatepickerProps } from '@datepicker-react/hooks';
import styled from 'styled-components';

Expand Down Expand Up @@ -49,72 +49,67 @@ const Forward = styled(ChevronRightIcon)`
}
`;

interface BaseDatepickerProps extends UseDatepickerProps {
forwardedRef: RefObject<HTMLDivElement>;
interface DatepickerProps extends UseDatepickerProps {
locale: Locale;
}

const BaseDatepicker: FC<BaseDatepickerProps> = ({ forwardedRef, focusedInput, locale, ...datepickerProps }) => {
const {
firstDayOfWeek,
activeMonths,
isDateSelected,
isDateHovered,
isFirstOrLastSelectedDate,
isDateBlocked,
isDateFocused,
focusedDate,
onDateHover,
onDateSelect,
onDateFocus,
goToPreviousMonths,
goToNextMonths
} = useDatepicker({
focusedInput,
...datepickerProps
});
export const Datepicker = React.forwardRef<HTMLDivElement, DatepickerProps>(
({ focusedInput, locale, ...datepickerProps }, ref) => {
const {
firstDayOfWeek,
activeMonths,
isDateSelected,
isDateHovered,
isFirstOrLastSelectedDate,
isDateBlocked,
isDateFocused,
focusedDate,
onDateHover,
onDateSelect,
onDateFocus,
goToPreviousMonths,
goToNextMonths
} = useDatepicker({
focusedInput,
...datepickerProps
});

return (
<DatepickerContext.Provider
value={{
focusedDate,
isDateFocused,
isDateSelected,
isDateHovered,
isDateBlocked,
isFirstOrLastSelectedDate,
onDateSelect,
onDateFocus,
onDateHover
}}
>
<DatepickerContainer
ref={forwardedRef}
onMouseDown={e => {
// Prevent mousedown event on Datepicker, so everything else dont lose focus
e.preventDefault();
return (
<DatepickerContext.Provider
value={{
focusedDate,
isDateFocused,
isDateSelected,
isDateHovered,
isDateBlocked,
isFirstOrLastSelectedDate,
onDateSelect,
onDateFocus,
onDateHover
}}
>
<Back onClick={goToPreviousMonths} />
<Forward onClick={goToNextMonths} />
<DatepickerWrapper activeMonths={activeMonths}>
{activeMonths.map(monthInformation => (
<Month
key={`${monthInformation.year}-${monthInformation.month}`}
year={monthInformation.year}
month={monthInformation.month}
firstDayOfWeek={firstDayOfWeek}
locale={locale}
/>
))}
</DatepickerWrapper>
</DatepickerContainer>
</DatepickerContext.Provider>
);
};

export const Datepicker = React.forwardRef(
(props: Omit<BaseDatepickerProps, 'forwardedRef'>, ref: RefObject<HTMLDivElement>) => (
<BaseDatepicker {...props} forwardedRef={ref} />
)
);
<DatepickerContainer
ref={ref}
onMouseDown={e => {
// Prevent mousedown event on Datepicker, so everything else dont lose focus
e.preventDefault();
}}
>
<Back onClick={goToPreviousMonths} />
<Forward onClick={goToNextMonths} />
<DatepickerWrapper activeMonths={activeMonths}>
{activeMonths.map(monthInformation => (
<Month
key={`${monthInformation.year}-${monthInformation.month}`}
year={monthInformation.year}
month={monthInformation.month}
firstDayOfWeek={firstDayOfWeek}
locale={locale}
/>
))}
</DatepickerWrapper>
</DatepickerContainer>
</DatepickerContext.Provider>
);
}
);
4 changes: 2 additions & 2 deletions src/components/Datepicker/DatepickerRangeInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ const DatepickerRangeInput: FC<DatepickerRangeInputProps> = ({
variant = 'normal',
disabled,
...rest
}: DatepickerRangeInputProps) => {
}) => {
const [triggerReference, setTriggerReference] = useState(undefined);
const [contentReference, setContentReference] = useState(undefined);
const [arrowReference, setArrowReference] = useState(undefined);
Expand Down Expand Up @@ -446,4 +446,4 @@ const DatepickerRangeInput: FC<DatepickerRangeInputProps> = ({
);
};

export { DatepickerRangeInput, DatepickerRangeInputProps };
export { DatepickerRangeInput, DatepickerRangeInputProps };
6 changes: 2 additions & 4 deletions src/components/Datepicker/DatepickerSingleInput.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { FirstDayOfWeek, START_DATE } from '@datepicker-react/hooks';
import parse from 'date-fns/parse';
import React, { ChangeEventHandler, FC, useEffect, useRef, useState } from 'react';
import React, { ChangeEventHandler, FC, useEffect, useState } from 'react';
import { MarginProps, WidthProps } from 'styled-system';
import { usePopper } from 'react-popper';
import { createPortal } from 'react-dom';
Expand Down Expand Up @@ -94,13 +94,12 @@ const DatepickerSingleInput: FC<DatepickerSingleInputProps> = ({
inputId,
disabled,
...rest
}: DatepickerSingleInputProps) => {
}) => {
const [triggerReference, setTriggerReference] = useState(undefined);
const [contentReference, setContentReference] = useState(undefined);
const [arrowReference, setArrowReference] = useState(undefined);

const localeObject = useLocaleObject(locale);
const inputRef = useRef<HTMLDivElement>();
const [isFocused, setIsFocused] = useState(false);
const [inputText, setInputText] = useState(dateToDisplayText(localeObject, displayFormat, value));
const [error, setError] = useState(false);
Expand Down Expand Up @@ -172,7 +171,6 @@ const DatepickerSingleInput: FC<DatepickerSingleInputProps> = ({
<Input
ref={element => {
setTriggerReference(element);
inputRef.current = element;
}}
id={id}
autoComplete="off"
Expand Down

0 comments on commit 6453d08

Please sign in to comment.