-
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
1 parent
f98dc94
commit 342ea48
Showing
52 changed files
with
758 additions
and
3,127 deletions.
There are no files selected for viewing
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 |
---|---|---|
@@ -1,30 +1,94 @@ | ||
import React, { ReactNode, useContext } from 'react'; | ||
import { ChildSlotControllerContext, Slot } from './Slot'; | ||
|
||
/** | ||
* Props of the {@link Outlet} component. | ||
* | ||
* @group Components | ||
*/ | ||
export interface OutletProps { | ||
import React, { Component, createContext, ReactNode, Suspense } from 'react'; | ||
import { Presenter } from './Presenter'; | ||
|
||
export const PresenterContext = createContext<Presenter | null>(null); | ||
|
||
export const ChildPresenterContext = createContext<Presenter | null>(null); | ||
|
||
interface OutletState { | ||
/** | ||
* An error captured by an error boundary. | ||
*/ | ||
error: unknown; | ||
|
||
/** | ||
* A content that is rendered if there's no route to render. | ||
* `true` if an error boundary was triggered. | ||
*/ | ||
children?: ReactNode; | ||
hasError: boolean; | ||
} | ||
|
||
export class Outlet extends Component<{}, OutletState> { | ||
static contextType = ChildPresenterContext; | ||
|
||
declare context: Presenter | null; | ||
|
||
// static getDerivedStateFromError(error: unknown): Partial<SlotState> | null { | ||
// return { error, hasError: true }; | ||
// } | ||
|
||
// static getDerivedStateFromProps(nextProps: Readonly<SlotProps>, prevState: SlotState): Partial<SlotState> | null { | ||
// if (prevState.hasError) { | ||
// nextProps.controller.renderedController.onCatch(prevState.error); | ||
// | ||
// return { error: undefined, hasError: false }; | ||
// } | ||
// return null; | ||
// } | ||
|
||
render(): ReactNode { | ||
if (this.context === null) { | ||
return null; | ||
} | ||
|
||
const { renderedPresenter } = this.context; | ||
|
||
const children = ( | ||
<InternalOutlet | ||
canSuspend={true} | ||
presenter={renderedPresenter} | ||
/> | ||
); | ||
|
||
if (renderedPresenter.loadingComponent === undefined) { | ||
return children; | ||
} | ||
|
||
return ( | ||
<Suspense | ||
fallback={ | ||
<InternalOutlet | ||
canSuspend={false} | ||
presenter={renderedPresenter} | ||
/> | ||
} | ||
> | ||
{children} | ||
</Suspense> | ||
); | ||
} | ||
} | ||
|
||
interface InternalOutletProps { | ||
canSuspend: boolean; | ||
presenter: Presenter; | ||
} | ||
|
||
/** | ||
* Renders a route provided by an enclosing {@link Router}. | ||
* | ||
* @group Components | ||
*/ | ||
export function Outlet(props: OutletProps): ReactNode { | ||
const controller = useContext(ChildSlotControllerContext); | ||
function InternalOutlet(props: InternalOutletProps): ReactNode { | ||
const { canSuspend, presenter } = props; | ||
|
||
const Component = canSuspend ? presenter.getComponentOrSuspend() : presenter.loadingComponent; | ||
|
||
if (Component === undefined) { | ||
return null; | ||
} | ||
|
||
return controller === undefined ? props.children : <Slot controller={controller} />; | ||
return ( | ||
<PresenterContext.Provider value={presenter}> | ||
<ChildPresenterContext.Provider value={presenter.childPresenter}> | ||
<Component /> | ||
</ChildPresenterContext.Provider> | ||
</PresenterContext.Provider> | ||
); | ||
} | ||
|
||
/** | ||
* @internal | ||
*/ | ||
Outlet.displayName = 'Outlet'; | ||
InternalOutlet.displayName = 'InternalOutlet'; |
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.