Skip to content

Commit

Permalink
chore: reorganize contexts files (#1564)
Browse files Browse the repository at this point in the history
  • Loading branch information
gpbl authored Sep 26, 2022
1 parent 4a66b37 commit 235ed89
Show file tree
Hide file tree
Showing 29 changed files with 116 additions and 154 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { createContext, ReactNode } from 'react';
import React, { createContext, ReactNode, useContext } from 'react';

import { DayPickerProps } from 'DayPicker';

Expand Down Expand Up @@ -139,3 +139,17 @@ export function DayPickerProvider(props: DayPickerProviderProps): JSX.Element {
</DayPickerContext.Provider>
);
}

/**
* Hook to access the {@link DayPickerContextValue}.
*
* Use the DayPicker context to access to the props passed to DayPicker inside
* internal or custom components.
*/
export function useDayPicker(): DayPickerContextValue {
const context = useContext(DayPickerContext);
if (!context) {
throw new Error(`useDayPicker must be used within a DayPickerProvider.`);
}
return context;
}
1 change: 0 additions & 1 deletion packages/react-day-picker/src/contexts/DayPicker/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
export * from './useDayPicker';
export * from './DayPickerContext';
17 changes: 0 additions & 17 deletions packages/react-day-picker/src/contexts/DayPicker/useDayPicker.ts

This file was deleted.

16 changes: 15 additions & 1 deletion packages/react-day-picker/src/contexts/Focus/FocusContext.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { createContext, ReactNode, useState } from 'react';
import React, { createContext, ReactNode, useContext, useState } from 'react';

import isSameDay from 'date-fns/isSameDay';

Expand Down Expand Up @@ -119,3 +119,17 @@ export function FocusProvider(props: { children: ReactNode }): JSX.Element {
</FocusContext.Provider>
);
}

/**
* Hook to access the {@link FocusContextValue}. Use this hook to handle the
* focus state of the elements.
*
* This hook is meant to be used inside internal or custom components.
*/
export function useFocusContext(): FocusContextValue {
const context = useContext(FocusContext);
if (!context) {
throw new Error('useFocusContext must be used within a FocusProvider');
}
return context;
}
1 change: 0 additions & 1 deletion packages/react-day-picker/src/contexts/Focus/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
export * from './FocusContext';
export * from './useFocusContext';
17 changes: 0 additions & 17 deletions packages/react-day-picker/src/contexts/Focus/useFocusContext.ts

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { createContext, ReactNode } from 'react';
import React, { createContext, ReactNode, useContext } from 'react';

import { useDayPicker } from 'contexts/DayPicker';
import { useSelectMultiple } from 'contexts/SelectMultiple';
Expand Down Expand Up @@ -42,3 +42,18 @@ export function ModifiersProvider(props: ModifiersProviderProps): JSX.Element {
</ModifiersContext.Provider>
);
}

/**
* Return the modifiers used by DayPicker.
*
* This hook is meant to be used inside internal or custom components.
* Requires to be wrapped into {@link ModifiersProvider}.
*
*/
export function useModifiers(): Modifiers {
const context = useContext(ModifiersContext);
if (!context) {
throw new Error('useModifiers must be used within a ModifiersProvider');
}
return context;
}
2 changes: 1 addition & 1 deletion packages/react-day-picker/src/contexts/Modifiers/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export * from './useModifiers';
export * from './ModifiersContext';
export * from './utils/getActiveModifiers';
20 changes: 0 additions & 20 deletions packages/react-day-picker/src/contexts/Modifiers/useModifiers.ts

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ import { freezeBeforeAll } from 'test/utils';

import { DayPickerBase } from 'types/DayPickerBase';

import { NavigationContextValue } from './NavigationContext';
import { useNavigation } from './useNavigation';
import { NavigationContextValue, useNavigation } from './NavigationContext';

const today = new Date(2021, 11, 8);
const todaysMonth = startOfMonth(today);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { createContext, ReactNode } from 'react';
import React, { createContext, ReactNode, useContext } from 'react';

import addMonths from 'date-fns/addMonths';
import isBefore from 'date-fns/isBefore';
Expand Down Expand Up @@ -81,3 +81,17 @@ export function NavigationProvider(props: {
</NavigationContext.Provider>
);
}

/**
* Hook to access the {@link NavigationContextValue}. Use this hook to navigate
* between months or years in DayPicker.
*
* This hook is meant to be used inside internal or custom components.
*/
export function useNavigation(): NavigationContextValue {
const context = useContext(NavigationContext);
if (!context) {
throw new Error('useNavigation must be used within a NavigationProvider');
}
return context;
}
1 change: 0 additions & 1 deletion packages/react-day-picker/src/contexts/Navigation/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
export * from './NavigationContext';
export * from './useNavigation';
17 changes: 0 additions & 17 deletions packages/react-day-picker/src/contexts/Navigation/useNavigation.ts

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@ import { isMatch } from 'contexts/Modifiers/utils/isMatch';
import { DayPickerMultipleProps } from 'types/DayPickerMultiple';
import { ActiveModifiers } from 'types/Modifiers';

import { SelectMultipleContextValue } from './SelectMultipleContext';
import { useSelectMultiple } from './useSelectMultiple';
import {
SelectMultipleContextValue,
useSelectMultiple
} from './SelectMultipleContext';

const today = new Date(2021, 11, 8);
freezeBeforeAll(today);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { createContext, ReactNode } from 'react';
import React, { createContext, ReactNode, useContext } from 'react';

import isSameDay from 'date-fns/isSameDay';

Expand Down Expand Up @@ -133,3 +133,18 @@ export function SelectMultipleProviderInternal({
</SelectMultipleContext.Provider>
);
}

/**
* Hook to access the {@link SelectMultipleContextValue}.
*
* This hook is meant to be used inside internal or custom components.
*/
export function useSelectMultiple(): SelectMultipleContextValue {
const context = useContext(SelectMultipleContext);
if (!context) {
throw new Error(
'useSelectMultiple must be used within a SelectMultipleProvider'
);
}
return context;
}
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
export * from './SelectMultipleContext';
export * from './useSelectMultiple';

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ import { DayPickerBase } from 'types/DayPickerBase';
import { DayPickerRangeProps } from 'types/DayPickerRange';
import { ActiveModifiers } from 'types/Modifiers';

import { SelectRangeContextValue } from './SelectRangeContext';
import { useSelectRange } from './useSelectRange';
import { SelectRangeContextValue, useSelectRange } from './SelectRangeContext';

const today = new Date(2021, 11, 8);
freezeBeforeAll(today);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { createContext, ReactNode } from 'react';
import React, { createContext, ReactNode, useContext } from 'react';

import differenceInCalendarDays from 'date-fns/differenceInCalendarDays';
import isAfter from 'date-fns/isAfter';
Expand Down Expand Up @@ -156,3 +156,16 @@ export function SelectRangeProviderInternal({
</SelectRangeContext.Provider>
);
}

/**
* Hook to access the {@link SelectRangeContextValue}.
*
* This hook is meant to be used inside internal or custom components.
*/
export function useSelectRange(): SelectRangeContextValue {
const context = useContext(SelectRangeContext);
if (!context) {
throw new Error('useSelectRange must be used within a SelectRangeProvider');
}
return context;
}
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
export * from './SelectRangeContext';
export * from './useSelectRange';

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { DayPickerBase } from 'types/DayPickerBase';
import { DayPickerSingleProps } from 'types/DayPickerSingle';
import { ActiveModifiers } from 'types/Modifiers';

import { useSelectSingle } from './useSelectSingle';
import { useSelectSingle } from './SelectSingleContext';

const today = new Date(2021, 11, 8);
freezeBeforeAll(today);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { createContext } from 'react';
import React, { createContext, useContext } from 'react';

import { DayPickerBase } from 'types/DayPickerBase';
import { DayPickerSingleProps, isDayPickerSingle } from 'types/DayPickerSingle';
Expand Down Expand Up @@ -78,3 +78,18 @@ export function SelectSingleProviderInternal({
</SelectSingleContext.Provider>
);
}

/**
* Hook to access the {@link SelectSingleContextValue}.
*
* This hook is meant to be used inside internal or custom components.
*/
export function useSelectSingle(): SelectSingleContextValue {
const context = useContext(SelectSingleContext);
if (!context) {
throw new Error(
'useSelectSingle must be used within a SelectSingleProvider'
);
}
return context;
}
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
export * from './SelectSingleContext';
export * from './useSelectSingle';

This file was deleted.

2 changes: 1 addition & 1 deletion website/examples/focus-recursive.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { DayPicker } from 'react-day-picker';
/** Test for the next focus day to not cause an infinite recursion. */
export default function App() {
const disabledDays = [
// new Date(2022, 5, 4),
new Date(2022, 5, 4),
{
after: new Date(2022, 5, 26)
}
Expand Down

0 comments on commit 235ed89

Please sign in to comment.