Skip to content
This repository has been archived by the owner on Oct 25, 2022. It is now read-only.

Commit

Permalink
Merge pull request #1 from aajahid/contextWrapper - Allow user to wra…
Browse files Browse the repository at this point in the history
…p the whole app in a context

This enables one to use context based features, like react router,
in popups and dialogs.
  • Loading branch information
aajahid authored Jul 18, 2022
2 parents c9ab64f + b314554 commit 14c9f14
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 3 deletions.
1 change: 1 addition & 0 deletions src/common/Interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ export abstract class App {

export abstract class UserInterface {
abstract setMainView(element: React.ReactElement<any>): void;
abstract setContextWrapper(contextWrapper: ((rootView: React.ReactElement<any>) => React.ReactElement<any>)): void;
abstract registerRootView(viewKey: string, getComponentFunc: Function): void;

abstract useCustomScrollbars(enable?: boolean): void;
Expand Down
11 changes: 11 additions & 0 deletions src/native-common/MainViewStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import SubscribableEvent from 'subscribableevent';

export class MainViewStore extends SubscribableEvent<() => void> {
private _mainView: React.ReactElement<any> | undefined;
private _contextWrapper: ((rootView: React.ReactElement<any>) => React.ReactElement<any>) | undefined;


getMainView(): React.ReactElement<any> | undefined {
return this._mainView;
Expand All @@ -22,6 +24,15 @@ export class MainViewStore extends SubscribableEvent<() => void> {
this._mainView = view;
this.fire();
}

getContextWrapper(): ((rootView: React.ReactElement<any>) => React.ReactElement<any>) | undefined {
return this._contextWrapper;
}

setContextWrapper(contextWrapper: ((rootView: React.ReactElement<any>) => React.ReactElement<any>)): void {
this._contextWrapper = contextWrapper;
this.fire();
}
}

export default new MainViewStore();
12 changes: 10 additions & 2 deletions src/native-common/RootView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ interface RootViewPropsWithMainViewType extends BaseRootViewProps {

interface RootViewState {
mainView?: any;
contextWrapper?: (rootView: React.ReactElement<any>) => React.ReactElement<any>;
announcementText?: string;
}

Expand Down Expand Up @@ -144,7 +145,12 @@ abstract class BaseRootView<P extends BaseRootViewProps> extends React.Component
</RN.Animated.View>
);

return this.renderTopView(content);
const maybeContextWrappedContent =
this.state.contextWrapper === undefined
? content
: this.state.contextWrapper(content);

return this.renderTopView(maybeContextWrappedContent);
}

protected _renderAnnouncerView(): JSX.Element {
Expand Down Expand Up @@ -193,13 +199,15 @@ class RootViewUsingStore extends BaseRootView<BaseRootViewProps> {

private _getStateFromStore(): RootViewState {
let mainView = MainViewStore.getMainView();
let contextWrapper = MainViewStore.getContextWrapper();

if (mainView && !isEqual(mainView.props, this._mainViewProps)) {
mainView = React.cloneElement(mainView, this._mainViewProps);
}

return {
mainView: mainView,
mainView: mainView,
contextWrapper: contextWrapper,
};
}

Expand Down
4 changes: 4 additions & 0 deletions src/native-common/UserInterface.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,10 @@ export class UserInterface extends RX.UserInterface {
MainViewStore.setMainView(element);
}

setContextWrapper(contextWrapper: ((rootView: React.ReactElement<any>) => React.ReactElement<any>)): void {
MainViewStore.setContextWrapper(contextWrapper);
}

registerRootViewUsingPropsFactory(factory: RN.ComponentProvider) {
this._rootViewUsingPropsFactory = factory;
}
Expand Down
13 changes: 12 additions & 1 deletion src/web/FrontLayerViewManager.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,17 @@ export class FrontLayerViewManager {
private _isRtlAllowed = true;
private _isRtlForced = false;

private _contextWrapper: ((rootView: React.ReactElement<any>) => React.ReactElement<any>) | undefined;

setMainView(element: React.ReactElement<any>): void {
this._mainView = element;
this._renderRootView();
}

setContextWrapper(contextWrapper: ((rootView: React.ReactElement<any>) => React.ReactElement<any>)): void {
this._contextWrapper = contextWrapper;
}

isModalDisplayed(modalId?: string): boolean {
if (modalId) {
return this._modalStack.some(d => d.id === modalId);
Expand Down Expand Up @@ -205,7 +211,12 @@ export class FrontLayerViewManager {

const container = document.getElementsByClassName('app-container')[0];

ReactDOM.render(rootView, container);
const maybeContextWrappedRootView =
this._contextWrapper === undefined
? rootView
: this._contextWrapper(rootView);

ReactDOM.render(maybeContextWrappedRootView, container);
}

isPopupDisplayed(popupId?: string): boolean {
Expand Down
4 changes: 4 additions & 0 deletions src/web/UserInterface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,10 @@ export class UserInterface extends RX.UserInterface {
FrontLayerViewManager.setMainView(element);
}

setContextWrapper(contextWrapper: ((rootView: React.ReactElement<any>) => React.ReactElement<any>)): void {
FrontLayerViewManager.setContextWrapper(contextWrapper);
}

registerRootView(viewKey: string, getComponentFunc: Function): void {
// Nothing to do
}
Expand Down

0 comments on commit 14c9f14

Please sign in to comment.