Skip to content
Closed
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
46 changes: 41 additions & 5 deletions lib/src/commands/Commands.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,14 @@ describe('Commands', () => {
root: { component: { name: 'com.example.MyScreen' } },
});
expect(layoutProcessor.process).toBeCalledWith(
{ component: { name: 'com.example.MyScreen', options: {}, id: 'Component+UNIQUE_ID' } },
{
component: {
name: 'com.example.MyScreen',
options: {},
id: 'Component+UNIQUE_ID',
processedByNavigation: true,
},
},
CommandName.SetRoot
);
});
Expand Down Expand Up @@ -168,6 +175,7 @@ describe('Commands', () => {
id: 'Component+UNIQUE_ID',
name: 'com.example.MyScreen',
options: { topBar: { visible: false } },
processedByNavigation: true,
},
},
CommandName.SetRoot
Expand Down Expand Up @@ -326,7 +334,14 @@ describe('Commands', () => {
it('process layout with layoutProcessor', () => {
uut.showModal({ component: { name: 'com.example.MyScreen' } });
expect(layoutProcessor.process).toBeCalledWith(
{ component: { id: 'Component+UNIQUE_ID', name: 'com.example.MyScreen', options: {} } },
{
component: {
id: 'Component+UNIQUE_ID',
name: 'com.example.MyScreen',
options: {},
processedByNavigation: true,
},
},
CommandName.ShowModal
);
});
Expand Down Expand Up @@ -437,7 +452,14 @@ describe('Commands', () => {
it('process layout with layoutProcessor', () => {
uut.push('theComponentId', { component: { name: 'com.example.MyScreen' } });
expect(layoutProcessor.process).toBeCalledWith(
{ component: { id: 'Component+UNIQUE_ID', name: 'com.example.MyScreen', options: {} } },
{
component: {
id: 'Component+UNIQUE_ID',
name: 'com.example.MyScreen',
options: {},
processedByNavigation: true,
},
},
CommandName.Push
);
});
Expand Down Expand Up @@ -573,7 +595,14 @@ describe('Commands', () => {
it('process layout with layoutProcessor', () => {
uut.setStackRoot('theComponentId', [{ component: { name: 'com.example.MyScreen' } }]);
expect(layoutProcessor.process).toBeCalledWith(
{ component: { id: 'Component+UNIQUE_ID', name: 'com.example.MyScreen', options: {} } },
{
component: {
id: 'Component+UNIQUE_ID',
name: 'com.example.MyScreen',
options: {},
processedByNavigation: true,
},
},
CommandName.SetStackRoot
);
});
Expand Down Expand Up @@ -619,7 +648,14 @@ describe('Commands', () => {
it('process layout with layoutProcessor', () => {
uut.showOverlay({ component: { name: 'com.example.MyScreen' } });
expect(layoutProcessor.process).toBeCalledWith(
{ component: { id: 'Component+UNIQUE_ID', name: 'com.example.MyScreen', options: {} } },
{
component: {
id: 'Component+UNIQUE_ID',
name: 'com.example.MyScreen',
options: {},
processedByNavigation: true,
},
},
CommandName.ShowOverlay
);
});
Expand Down
1 change: 1 addition & 0 deletions lib/src/commands/Commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export class Commands {
const input = cloneLayout(simpleApi);
this.optionsCrawler.crawl(input.root);
const processedRoot = this.layoutProcessor.process(input.root, CommandName.SetRoot);
this.optionsCrawler.crawl(processedRoot);
const root = this.layoutTreeParser.parse(processedRoot);

const modals = map(input.modals, (modal) => {
Expand Down
7 changes: 5 additions & 2 deletions lib/src/commands/OptionsCrawler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import isFunction from 'lodash/isFunction';
import { Store } from '../components/Store';
import { Options } from '../interfaces/Options';
import {
InnerLayoutComponent,
Layout,
LayoutBottomTabs,
LayoutComponent,
Expand Down Expand Up @@ -75,17 +76,19 @@ export class OptionsCrawler {
return (component as ComponentWithOptions).options !== undefined;
}

private applyStaticOptions(layout: LayoutComponent) {
private applyStaticOptions(layout: InnerLayoutComponent) {
if (layout.processedByNavigation) return;
const staticOptions = this.staticOptionsIfPossible(layout);
layout.options = merge({}, staticOptions, layout.options);
layout.processedByNavigation = true;
}

private staticOptionsIfPossible(layout: LayoutComponent) {
const foundReactGenerator = this.store.getComponentClassForName(layout.name!);
const reactComponent = foundReactGenerator ? foundReactGenerator() : undefined;
if (reactComponent && this.isComponentWithOptions(reactComponent)) {
return isFunction(reactComponent.options)
? reactComponent.options({ componentId: layout.id, ...layout.passProps } || {})
? reactComponent.options({ componentId: layout.id, ...layout.passProps })
: reactComponent.options;
}
return {};
Expand Down
4 changes: 4 additions & 0 deletions lib/src/interfaces/Layout.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import { Options } from './Options';

export interface InnerLayoutComponent<P = {}> extends LayoutComponent<P> {
processedByNavigation?: boolean;
}

export interface LayoutComponent<P = {}> {
/**
* Component reference id, Auto generated if empty
Expand Down