-
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.
* Router: <Set wrap={}> * Route: update comment to reflect implementation * Router: Review comment fixes * for-of, optional useAuth, comments * Stricter route prop types
- Loading branch information
Showing
13 changed files
with
778 additions
and
285 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import React, { ReactElement, ReactNode, FunctionComponentElement } from 'react' | ||
|
||
import { useLocation } from './location' | ||
import { isRoute } from './router' | ||
import { useRouterState } from './router-context' | ||
import { flattenAll, matchPath } from './util' | ||
|
||
interface PropsWithChildren { | ||
children: ReactNode | ||
} | ||
|
||
type WrapperType = (props: { children: any }) => ReactElement | null | ||
type ReduceType = FunctionComponentElement<PropsWithChildren> | undefined | ||
|
||
interface Props { | ||
wrap: WrapperType | WrapperType[] | ||
children: ReactNode | ||
} | ||
|
||
export const Set: React.FC<Props> = ({ children, wrap }) => { | ||
const routerState = useRouterState() | ||
const location = useLocation() | ||
const wrappers = Array.isArray(wrap) ? wrap : [wrap] | ||
|
||
const flatChildArray = flattenAll(children) | ||
|
||
const matchingChildRoute = flatChildArray.some((child) => { | ||
if (isRoute(child)) { | ||
const { path } = child.props | ||
|
||
if (path) { | ||
const { match } = matchPath( | ||
path, | ||
location.pathname, | ||
routerState.paramTypes | ||
) | ||
|
||
if (match) { | ||
return true | ||
} | ||
} | ||
} | ||
|
||
return false | ||
}) | ||
|
||
return matchingChildRoute | ||
? wrappers.reduceRight<ReduceType>((acc, wrapper) => { | ||
return React.createElement(wrapper, undefined, acc ? acc : children) | ||
}, undefined) || null | ||
: 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
Oops, something went wrong.