Skip to content

Commit

Permalink
Fix broken static options provided as objects
Browse files Browse the repository at this point in the history
RNN supports static options provided as both functions and objects.
Static objects broke in a recent PR - 08f8581
  • Loading branch information
guyca committed Jan 17, 2019
1 parent 64dbc22 commit 4d82292
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 7 deletions.
19 changes: 19 additions & 0 deletions lib/src/commands/LayoutTreeCrawler.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,25 @@ describe('LayoutTreeCrawler', () => {
expect(node.data.options).toEqual({ popGesture: true });
});

it('Components: injects options from original component class static property', () => {
when(mockedStore.getComponentClassForName('theComponentName')).thenReturn(
() =>
class extends React.Component {
static options = {
popGesture: true
};
}
);
const node = {
id: 'testId',
type: LayoutType.Component,
data: { name: 'theComponentName', options: {} },
children: []
};
uut.crawl(node);
expect(node.data.options).toEqual({ popGesture: true });
});

it('Components: crawl does not cache options', () => {
when(mockedStore.getComponentClassForName('theComponentName')).thenReturn(
() =>
Expand Down
15 changes: 8 additions & 7 deletions lib/src/commands/LayoutTreeCrawler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,16 +46,17 @@ export class LayoutTreeCrawler {
return (component as ComponentWithOptions).options !== undefined;
}

private applyStaticOptions(node: LayoutNode) {
node.data.options = _.merge({}, this.staticOptionsIfPossible(node), node.data.options);
}

private staticOptionsIfPossible(node: LayoutNode) {
const foundReactGenerator = this.store.getComponentClassForName(node.data.name!);
const reactComponent = foundReactGenerator ? foundReactGenerator() : undefined;
return reactComponent && this.isComponentWithOptions(reactComponent)
? reactComponent.options(node.data.passProps || {})
: {};
}

private applyStaticOptions(node: LayoutNode) {
node.data.options = _.merge({}, this.staticOptionsIfPossible(node), node.data.options);
if (reactComponent && this.isComponentWithOptions(reactComponent)) {
return _.isFunction(reactComponent.options) ? reactComponent.options(node.data.passProps || {}) : reactComponent.options;
}
return {};
}

private assertComponentDataName(component: LayoutNode) {
Expand Down

0 comments on commit 4d82292

Please sign in to comment.