Browse Source

support for static options as a function with passProps optional argument

Daniel Zlotin 6 years ago
parent
commit
cf7e817f96
2 changed files with 27 additions and 1 deletions
  1. 26
    0
      lib/src/commands/LayoutTreeCrawler.test.ts
  2. 1
    1
      lib/src/commands/LayoutTreeCrawler.ts

+ 26
- 0
lib/src/commands/LayoutTreeCrawler.test.ts View File

@@ -67,6 +67,32 @@ describe('LayoutTreeCrawler', () => {
67 67
     expect(node.data.options).toEqual(theStyle);
68 68
   });
69 69
 
70
+  it('Components: passes passProps to the static options function to be used by the user', () => {
71
+    const MyComponent = class {
72
+      static options(passProps) {
73
+        return { foo: passProps.bar.baz.value };
74
+      }
75
+    };
76
+
77
+    const node: any = { type: LayoutType.Component, data: { name: 'theComponentName', passProps: { bar: { baz: { value: 'hello' } } } } };
78
+    store.setOriginalComponentClassForName('theComponentName', MyComponent);
79
+    uut.crawl(node);
80
+    expect(node.data.options).toEqual({ foo: 'hello' });
81
+  });
82
+
83
+  it('Components: passProps in the static options is optional', () => {
84
+    const MyComponent = class {
85
+      static options(passProps) {
86
+        return { foo: passProps };
87
+      }
88
+    };
89
+
90
+    const node: any = { type: LayoutType.Component, data: { name: 'theComponentName' } };
91
+    store.setOriginalComponentClassForName('theComponentName', MyComponent);
92
+    uut.crawl(node);
93
+    expect(node.data.options).toEqual({ foo: {} });
94
+  });
95
+
70 96
   it('Components: merges options from component class static property with passed options, favoring passed options', () => {
71 97
     const theStyle = {
72 98
       bazz: 123,

+ 1
- 1
lib/src/commands/LayoutTreeCrawler.ts View File

@@ -52,7 +52,7 @@ export class LayoutTreeCrawler {
52 52
 
53 53
   _applyStaticOptions(node) {
54 54
     const clazz = this.store.getOriginalComponentClassForName(node.data.name) || {};
55
-    const staticOptions = _.cloneDeep(clazz.options) || {};
55
+    const staticOptions = _.isFunction(clazz.options) ? clazz.options(node.data.passProps || {}) : (_.cloneDeep(clazz.options) || {});
56 56
     const passedOptions = node.data.options || {};
57 57
     node.data.options = _.merge({}, staticOptions, passedOptions);
58 58
   }