-
Notifications
You must be signed in to change notification settings - Fork 1k
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
10 changed files
with
368 additions
and
270 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import React, { ReactElement, ReactNode, FunctionComponentElement } from 'react' | ||
|
||
type WrapperType = (props: { children: any }) => ReactElement | null | ||
type ReduceType = FunctionComponentElement<PropsWithChildren> | undefined | ||
|
||
interface Props { | ||
wrap: WrapperType | WrapperType[] | ||
children: ReactNode | ||
} | ||
|
||
interface PropsWithChildren { | ||
children: ReactNode | ||
} | ||
|
||
export const Set: React.FC<Props> = ({ children, wrap }) => { | ||
const wrappers = Array.isArray(wrap) ? wrap : [wrap] | ||
|
||
return ( | ||
wrappers.reduceRight<ReduceType>((acc, wrapper) => { | ||
return React.createElement(wrapper, { | ||
children: acc ? acc : children, | ||
}) | ||
}, undefined) || null | ||
) | ||
} |
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 |
---|---|---|
@@ -0,0 +1,83 @@ | ||
import { render } from '@testing-library/react' | ||
import '@testing-library/jest-dom/extend-expect' | ||
|
||
import { Set } from '../Set' | ||
|
||
// SETUP | ||
const ChildA = () => <h1>ChildA</h1> | ||
const ChildB = () => <h1>ChildB</h1> | ||
const ChildC = () => <h1>ChildC</h1> | ||
const Parent = ({ children }) => <div>{children}</div> | ||
const GlobalLayout: React.FC = ({ children }) => ( | ||
<div> | ||
<h1>Global Layout</h1> | ||
{children} | ||
<footer>This is a footer</footer> | ||
</div> | ||
) | ||
const CustomWrapper: React.FC = ({ children }) => ( | ||
<div> | ||
<h1>Custom Wrapper</h1> | ||
{children} | ||
<p>Custom Wrapper End</p> | ||
</div> | ||
) | ||
const BLayout = ({ children }) => ( | ||
<div> | ||
<h1>Layout for B</h1> | ||
{children} | ||
</div> | ||
) | ||
|
||
test('wraps components in other components', async () => { | ||
const TestSet = () => ( | ||
<Parent> | ||
<Set wrap={[CustomWrapper, GlobalLayout]}> | ||
<ChildA /> | ||
<Set wrap={BLayout}> | ||
<ChildB /> | ||
</Set> | ||
</Set> | ||
<ChildC /> | ||
</Parent> | ||
) | ||
|
||
const screen = render(<TestSet />) | ||
|
||
expect(screen.container).toMatchInlineSnapshot(` | ||
<div> | ||
<div> | ||
<div> | ||
<h1> | ||
Custom Wrapper | ||
</h1> | ||
<div> | ||
<h1> | ||
Global Layout | ||
</h1> | ||
<h1> | ||
ChildA | ||
</h1> | ||
<div> | ||
<h1> | ||
Layout for B | ||
</h1> | ||
<h1> | ||
ChildB | ||
</h1> | ||
</div> | ||
<footer> | ||
This is a footer | ||
</footer> | ||
</div> | ||
<p> | ||
Custom Wrapper End | ||
</p> | ||
</div> | ||
<h1> | ||
ChildC | ||
</h1> | ||
</div> | ||
</div> | ||
`) | ||
}) |
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 was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import React, { useReducer, createContext, useContext } from 'react' | ||
|
||
import type { AuthContextInterface, useAuth } from '@redwoodjs/auth' | ||
|
||
import { ParamType } from './internal' | ||
|
||
const DEFAULT_PAGE_LOADING_DELAY = 1000 // milliseconds | ||
|
||
export interface RouterState { | ||
paramTypes?: Record<string, ParamType> | ||
pageLoadingDelay?: number | ||
useAuth: typeof useAuth | ||
} | ||
|
||
const RouterStateContext = createContext<RouterState | undefined>(undefined) | ||
|
||
export interface RouterSetContextProps { | ||
setState: (newState: Partial<RouterState>) => void | ||
} | ||
|
||
const RouterSetContext = createContext< | ||
React.Dispatch<Partial<RouterState>> | undefined | ||
>(undefined) | ||
|
||
function stateReducer(state: RouterState, newState: Partial<RouterState>) { | ||
return { ...state, ...newState } | ||
} | ||
|
||
export const RouterContextProvider: React.FC<RouterState> = ({ | ||
useAuth = () => ({} as AuthContextInterface), | ||
paramTypes, | ||
pageLoadingDelay = DEFAULT_PAGE_LOADING_DELAY, | ||
children, | ||
}) => { | ||
const [state, setState] = useReducer(stateReducer, { | ||
useAuth, | ||
paramTypes, | ||
pageLoadingDelay, | ||
}) | ||
|
||
return ( | ||
<RouterStateContext.Provider value={state}> | ||
<RouterSetContext.Provider value={setState}> | ||
{children} | ||
</RouterSetContext.Provider> | ||
</RouterStateContext.Provider> | ||
) | ||
} | ||
|
||
export const useRouterState = () => { | ||
const context = useContext(RouterStateContext) | ||
|
||
if (context === undefined) { | ||
throw new Error( | ||
'useRouterState must be used within a RouterContextProvider' | ||
) | ||
} | ||
|
||
return context | ||
} | ||
|
||
export const useRouterStateSetter = () => { | ||
const context = useContext(RouterSetContext) | ||
|
||
if (context === undefined) { | ||
throw new Error( | ||
'useRouterStateSetter must be used within a RouterContextProvider' | ||
) | ||
} | ||
|
||
return context | ||
} |
Oops, something went wrong.