Latest repo

This commit is contained in:
Marc
2025-06-02 16:42:16 +00:00
parent 53ddf1a329
commit cde5fae175
27907 changed files with 3875388 additions and 1 deletions

View File

@@ -0,0 +1,84 @@
import { MouseEvent } from 'react';
import { DayPickerProps } from 'DayPicker';
import { renderDayPickerHook } from 'test/render';
import { freezeBeforeAll } from 'test/utils';
import { DayPickerSingleProps } from 'types/DayPickerSingle';
import { ActiveModifiers } from 'types/Modifiers';
import {
SelectSingleContextValue,
useSelectSingle
} from './SelectSingleContext';
const today = new Date(2021, 11, 8);
freezeBeforeAll(today);
function renderHook(props?: Partial<DayPickerProps>) {
return renderDayPickerHook<SelectSingleContextValue>(useSelectSingle, props);
}
describe('when is not a single select DayPicker', () => {
test('the selected day should be undefined', () => {
const result = renderHook();
expect(result.current.selected).toBeUndefined();
});
});
describe('when a day is selected from DayPicker props', () => {
test('the selected day should be today', () => {
const dayPickerProps: DayPickerSingleProps = {
mode: 'single',
selected: today
};
const result = renderHook(dayPickerProps);
expect(result.current.selected).toBe(today);
});
});
describe('when onDayClick is called', () => {
const dayPickerProps: DayPickerSingleProps = {
mode: 'single',
onSelect: jest.fn(),
onDayClick: jest.fn()
};
const result = renderHook(dayPickerProps);
const activeModifiers = {};
const event = {} as MouseEvent;
test('should call the `onSelect` event handler', () => {
result.current.onDayClick?.(today, activeModifiers, event);
expect(dayPickerProps.onSelect).toHaveBeenCalledWith(
today,
today,
activeModifiers,
event
);
});
test('should call the `onDayClick` event handler', () => {
result.current.onDayClick?.(today, activeModifiers, event);
expect(dayPickerProps.onDayClick).toHaveBeenCalledWith(
today,
activeModifiers,
event
);
});
});
describe('if a selected day is not required', () => {
const dayPickerProps: DayPickerSingleProps = {
mode: 'single',
onSelect: jest.fn(),
required: false
};
test('should call the `onSelect` event handler with an undefined day', () => {
const result = renderHook(dayPickerProps);
const activeModifiers: ActiveModifiers = { selected: true };
const event = {} as MouseEvent;
result.current.onDayClick?.(today, activeModifiers, event);
expect(dayPickerProps.onSelect).toHaveBeenCalledWith(
undefined,
today,
activeModifiers,
event
);
});
});

View File

@@ -0,0 +1,96 @@
import { createContext, ReactNode, useContext } from 'react';
import { DayPickerBase } from 'types/DayPickerBase';
import { DayPickerSingleProps, isDayPickerSingle } from 'types/DayPickerSingle';
import { DayClickEventHandler } from 'types/EventHandlers';
/** Represents the value of a {@link SelectSingleContext}. */
export interface SelectSingleContextValue {
/** The day that has been selected. */
selected: Date | undefined;
/** Event handler to attach to the day button to enable the single select. */
onDayClick?: DayClickEventHandler;
}
/**
* The SelectSingle context shares details about the selected days when in
* single selection mode.
*
* Access this context from the {@link useSelectSingle} hook.
*/
export const SelectSingleContext = createContext<
SelectSingleContextValue | undefined
>(undefined);
export interface SelectSingleProviderProps {
initialProps: DayPickerBase;
children?: ReactNode;
}
/** Provides the values for the {@link SelectSingleProvider}. */
export function SelectSingleProvider(
props: SelectSingleProviderProps
): JSX.Element {
if (!isDayPickerSingle(props.initialProps)) {
const emptyContextValue: SelectSingleContextValue = {
selected: undefined
};
return (
<SelectSingleContext.Provider value={emptyContextValue}>
{props.children}
</SelectSingleContext.Provider>
);
}
return (
<SelectSingleProviderInternal
initialProps={props.initialProps}
children={props.children}
/>
);
}
/** @private */
export interface SelectSingleProviderInternal {
initialProps: DayPickerSingleProps;
children?: ReactNode;
}
export function SelectSingleProviderInternal({
initialProps,
children
}: SelectSingleProviderInternal): JSX.Element {
const onDayClick: DayClickEventHandler = (day, activeModifiers, e) => {
initialProps.onDayClick?.(day, activeModifiers, e);
if (activeModifiers.selected && !initialProps.required) {
initialProps.onSelect?.(undefined, day, activeModifiers, e);
return;
}
initialProps.onSelect?.(day, day, activeModifiers, e);
};
const contextValue: SelectSingleContextValue = {
selected: initialProps.selected,
onDayClick
};
return (
<SelectSingleContext.Provider value={contextValue}>
{children}
</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;
}

View File

@@ -0,0 +1 @@
export * from './SelectSingleContext';