Skip to content

Allow user to wrap the whole app in a context #1264

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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