-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
152 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,35 +1,60 @@ | ||
import { useMemo } from 'react'; | ||
import highlightRange from '../plugins/highlight-range'; | ||
import controls from '../plugins/controls'; | ||
import { CalendarPlugin } from '../plugin'; | ||
import { CalendarPlugin, CalendarPluginDefinition, CalendarPlugins } from '../plugin'; | ||
import { CalendarControls } from '../context/CalendarContext'; | ||
import { CalendarOptions, useCalendar } from './useCalendar'; | ||
import type { Dayjs } from 'dayjs'; | ||
import type { Setter } from '../types/Setter'; | ||
|
||
type Expect<Assertion extends true> = Assertion; | ||
export type Equals<X, Y> = (<T>() => T extends X ? 1 : 2) extends < | ||
T, | ||
>() => T extends Y ? 1 : 2 | ||
type Equals<X, Y> = (<T>() => T extends X ? 1 : 2) extends <T>() => T extends Y | ||
? 1 | ||
: 2 | ||
? true | ||
: false; | ||
|
||
// The plugin definition to be preserved | ||
() => { | ||
const plugins = useMemo(() => [highlightRange(), controls()] as const, []); | ||
|
||
type _ = Expect< | ||
Equals< | ||
readonly [ | ||
CalendarPlugin<{ | ||
dayInnerProps: { | ||
isOutOfRange: boolean; | ||
}; | ||
}>, | ||
CalendarPlugin<{ | ||
calendarInnerProps: { | ||
controls: CalendarControls; | ||
}; | ||
}>, | ||
], | ||
typeof plugins | ||
> | ||
>; | ||
(): Equals< | ||
readonly [ | ||
CalendarPlugin<{ | ||
dayInnerProps: { | ||
isOutOfRange: boolean; | ||
}; | ||
}>, | ||
CalendarPlugin<{ | ||
rootConfiguration: { | ||
viewedDate: Dayjs; | ||
setViewedDate: Setter<Dayjs>; | ||
}; | ||
calendarInnerProps: { | ||
controls: CalendarControls; | ||
}; | ||
}>, | ||
], | ||
typeof plugins | ||
> => true; | ||
|
||
const { calendar } = useCalendar({ plugins, setViewedDate: () => {} }); | ||
|
||
// type Prettify<T extends Record<PropertyKey, unknown>> = { | ||
// [K in keyof T]: T[K]; | ||
// }; | ||
|
||
type Options = MergedPluginProps<typeof plugins, 'dayInnerProps'>; | ||
|
||
return <calendar.Root>{({ dayjs }) => <p>hello world</p>}</calendar.Root>; | ||
}; | ||
|
||
type MergedPluginProps<T extends CalendarPlugins, Key extends keyof CalendarPluginDefinition> = T extends [ | ||
infer First, | ||
...infer Rest | ||
] | ||
? First extends CalendarPlugin<infer FirstProps> | ||
? Rest extends CalendarPlugins | ||
? FirstProps[Key] & MergedPluginProps<Rest, Key> | ||
: FirstProps[Key] | ||
: {} | ||
: {}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,93 @@ | ||
import type { CalendarPlugins } from '../plugin'; | ||
import { useMemo, type FC, ReactNode, useCallback } from 'react'; | ||
import type { CalendarPlugin, CalendarPlugins } from '../plugin'; | ||
import day, { type Dayjs } from 'dayjs'; | ||
import type { CalendarState } from '../context/CalendarContext'; | ||
import type { UnionToIntersection } from '../types/UnionToIntersection'; | ||
|
||
export interface Calendar<TPlugins extends CalendarPlugins> { | ||
plugins: TPlugins; | ||
calendar: { | ||
Root: FC<CalendarProps<TPlugins>>; | ||
}; | ||
} | ||
|
||
export function useCalendar<const TPlugins extends CalendarPlugins>({ | ||
export type CalendarOptions< TPlugins extends CalendarPlugins> = { | ||
plugins: TPlugins; | ||
viewedDate?: Dayjs; | ||
dayjs?: () => Dayjs; | ||
} & (TPlugins[number] extends CalendarPlugin<infer U> | ||
? U | ||
: {}); | ||
// ? UnionToIntersection<U['rootConfiguration']> extends Record<string, unknown> | ||
// ? UnionToIntersection<U['rootConfiguration']> extends undefined | ||
// ? never | ||
// : UnionToIntersection<U['rootConfiguration']> | ||
// : never); | ||
// : {}); | ||
|
||
export type CalendarProps<TPlugins extends CalendarPlugins> = { | ||
children: | ||
| ReactNode | ||
| (( | ||
props: CalendarState & | ||
(TPlugins[number] extends CalendarPlugin<infer U> | ||
? UnionToIntersection<U['calendarInnerProps']> | ||
: never), | ||
) => ReactNode); | ||
}; | ||
|
||
export function useCalendar< | ||
const TPlugins extends CalendarPlugins = readonly [], | ||
>({ | ||
plugins, | ||
}: { plugins: TPlugins }): Calendar<TPlugins> { | ||
return { plugins }; | ||
dayjs = () => day(), | ||
viewedDate, | ||
...props | ||
}: CalendarOptions<TPlugins>): Calendar<TPlugins> { | ||
function RootComponent({ children }: CalendarProps<TPlugins>) { | ||
const dayFactory = useCallback( | ||
() => dayjs().utc(true).second(0).minute(0).hour(12), | ||
[dayjs], | ||
); | ||
|
||
const baseState: CalendarState = { | ||
dayjs, | ||
viewedDate: viewedDate ?? dayFactory(), | ||
...props, | ||
}; | ||
|
||
const state = { | ||
...baseState, | ||
...plugins | ||
.map((plugin) => | ||
plugin.calendarHook !== undefined | ||
? plugin.calendarHook(baseState) | ||
: {}, | ||
) | ||
.reduce((acc, curr) => ({ ...acc, ...curr }), {}), | ||
}; | ||
|
||
return ( | ||
<>{typeof children === 'function' ? children(state as any) : children}</> | ||
); | ||
} | ||
|
||
// const Root = useMemo(() => RootComponent, [dayjs]); | ||
|
||
return { | ||
plugins, | ||
calendar: { | ||
Root: RootComponent, | ||
}, | ||
}; | ||
} | ||
|
||
// const plugins = [{} as CalendarPlugin<{dayInnerProps: {isOutOfRange: boolean}}>, {} as CalendarPlugin<{dayInnerProps: {isSomething: boolean}}>] as const; | ||
// const {calendar} = useCalendar({plugins}); | ||
// ty | ||
// const e = <calendar.Root>{(props) => <p>Hello</p>}</calendar.Root> | ||
// type E = CalendarState & (typeof plugins)[number] extends CalendarPlugin<infer U> ? U['dayInnerProps'] : never; | ||
// type UnionToIntersection<U> = | ||
// (U extends any ? (k: U)=>void : never) extends ((k: infer I)=>void) ? I : never; | ||
|
||
// type What = (typeof plugins)[number] extends CalendarPlugin<infer U> ? UnionToIntersection<U['dayInnerProps']> : false; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters