2022-08-11 04:33:29 +00:00
|
|
|
import { createContext, PropsWithChildren, useContext } from 'react';
|
|
|
|
|
|
|
|
export function createRowContext<TContext>() {
|
|
|
|
const Context = createContext<TContext | null>(null);
|
2022-09-20 18:14:24 +00:00
|
|
|
Context.displayName = 'RowContext';
|
2022-08-11 04:33:29 +00:00
|
|
|
|
|
|
|
return { RowProvider, useRowContext };
|
|
|
|
|
|
|
|
function RowProvider({
|
|
|
|
children,
|
|
|
|
context,
|
|
|
|
}: PropsWithChildren<{ context: TContext }>) {
|
|
|
|
return <Context.Provider value={context}>{children}</Context.Provider>;
|
|
|
|
}
|
|
|
|
|
|
|
|
function useRowContext() {
|
|
|
|
const context = useContext(Context);
|
|
|
|
if (!context) {
|
|
|
|
throw new Error('should be nested under RowProvider');
|
|
|
|
}
|
|
|
|
|
|
|
|
return context;
|
|
|
|
}
|
|
|
|
}
|