Procházet zdrojové kódy

merge static memebers from wrapped components #3322

Daniel Zlotin před 6 roky
rodič
revize
c5948d1d75

+ 4
- 0
integration/remx/MyComponent.js Zobrazit soubor

@@ -6,6 +6,10 @@ const { connect } = require('remx');
6 6
 const store = require('./MyStore');
7 7
 
8 8
 class MyComponent extends Component {
9
+  static options = {
10
+    title: 'MyComponent'
11
+  };
12
+
9 13
   render() {
10 14
     if (this.props.renderCountIncrement) {
11 15
       this.props.renderCountIncrement();

+ 8
- 0
integration/remx/remx.test.js Zobrazit soubor

@@ -1,6 +1,7 @@
1 1
 const React = require('react');
2 2
 require('react-native');
3 3
 const renderer = require('react-test-renderer');
4
+const { Navigation } = require('../../lib/dist/index');
4 5
 
5 6
 describe('remx support', () => {
6 7
   let MyConnectedComponent;
@@ -43,4 +44,11 @@ describe('remx support', () => {
43 44
 
44 45
     expect(renderCountIncrement).toHaveBeenCalledTimes(2);
45 46
   });
47
+
48
+  it('support for static members in connected components', () => {
49
+    expect(MyConnectedComponent.options).toEqual({ title: 'MyComponent' });
50
+
51
+    const registeredComponentClass = Navigation.componentRegistry.registerComponent('MyComponentName', () => MyConnectedComponent);
52
+    expect(registeredComponentClass.options).toEqual({ title: 'MyComponent' });
53
+  });
46 54
 });

+ 3
- 2
lib/src/Navigation.ts Zobrazit soubor

@@ -12,6 +12,7 @@ import { Element } from './adapters/Element';
12 12
 import { ComponentEventsObserver } from './events/ComponentEventsObserver';
13 13
 import { CommandsObserver } from './events/CommandsObserver';
14 14
 import { Constants } from './adapters/Constants';
15
+import { ComponentType } from 'react';
15 16
 
16 17
 export class Navigation {
17 18
   public readonly Element: React.ComponentType<{ elementId: any; resizeMode: any; }>;
@@ -48,8 +49,8 @@ export class Navigation {
48 49
    * Every navigation component in your app must be registered with a unique name.
49 50
    * The component itself is a traditional React component extending React.Component.
50 51
    */
51
-  public registerComponent(componentName: string, getComponentClassFunc: ComponentProvider): void {
52
-    this.componentRegistry.registerComponent(componentName, getComponentClassFunc);
52
+  public registerComponent(componentName: string, getComponentClassFunc: ComponentProvider): ComponentType<any> {
53
+    return this.componentRegistry.registerComponent(componentName, getComponentClassFunc);
53 54
   }
54 55
 
55 56
   /**

+ 2
- 1
lib/src/components/ComponentRegistry.test.tsx Zobrazit soubor

@@ -28,9 +28,10 @@ describe('ComponentRegistry', () => {
28 28
 
29 29
   it('registers component component by componentName into AppRegistry', () => {
30 30
     expect(mockRegistry).not.toHaveBeenCalled();
31
-    uut.registerComponent('example.MyComponent.name', () => MyComponent);
31
+    const result = uut.registerComponent('example.MyComponent.name', () => MyComponent);
32 32
     expect(mockRegistry).toHaveBeenCalledTimes(1);
33 33
     expect(mockRegistry.mock.calls[0][0]).toEqual('example.MyComponent.name');
34
+    expect(mockRegistry.mock.calls[0][1]()).toEqual(result);
34 35
   });
35 36
 
36 37
   it('saves the original component into the store', () => {

+ 3
- 1
lib/src/components/ComponentRegistry.ts Zobrazit soubor

@@ -1,5 +1,6 @@
1 1
 import { AppRegistry, ComponentProvider } from 'react-native';
2 2
 import { ComponentWrapper } from './ComponentWrapper';
3
+import { ComponentType } from 'react';
3 4
 
4 5
 export class ComponentRegistry {
5 6
   private store;
@@ -8,10 +9,11 @@ export class ComponentRegistry {
8 9
     this.store = store;
9 10
   }
10 11
 
11
-  registerComponent(componentName: string, getComponentClassFunc: ComponentProvider): void {
12
+  registerComponent(componentName: string, getComponentClassFunc: ComponentProvider): ComponentType<any> {
12 13
     const OriginalComponentClass = getComponentClassFunc();
13 14
     const NavigationComponent = ComponentWrapper.wrap(componentName, OriginalComponentClass, this.store);
14 15
     this.store.setOriginalComponentClassForName(componentName, OriginalComponentClass);
15 16
     AppRegistry.registerComponent(componentName, () => NavigationComponent);
17
+    return NavigationComponent;
16 18
   }
17 19
 }

+ 9
- 0
lib/src/components/ComponentWrapper.test.tsx Zobrazit soubor

@@ -16,6 +16,10 @@ describe('ComponentWrapper', () => {
16 16
   let childRef;
17 17
 
18 18
   class MyComponent extends React.Component {
19
+    static options = {
20
+      title: 'MyComponentTitle'
21
+    };
22
+
19 23
     render() {
20 24
       return <Text>{'Hello, World!'}</Text>;
21 25
     }
@@ -142,6 +146,11 @@ describe('ComponentWrapper', () => {
142 146
     expect(instance.originalComponentRef).toBeFalsy();
143 147
   });
144 148
 
149
+  it(`merges static members from wrapped component`, () => {
150
+    const NavigationComponent = ComponentWrapper.wrap(componentName, MyComponent, store) as any;
151
+    expect(NavigationComponent.options).toEqual({ title: 'MyComponentTitle' });
152
+  });
153
+
145 154
   describe('component lifecycle', () => {
146 155
     const componentDidAppearCallback = jest.fn();
147 156
     const componentDidDisappearCallback = jest.fn();

+ 2
- 0
lib/src/components/ComponentWrapper.tsx Zobrazit soubor

@@ -82,6 +82,8 @@ export class ComponentWrapper {
82 82
 
83 83
     ReactLifecyclesCompat.polyfill(WrappedComponent);
84 84
 
85
+    _.defaults(WrappedComponent, OriginalComponentClass);
86
+
85 87
     return WrappedComponent;
86 88
   }
87 89
 }

+ 0
- 1
package.json Zobrazit soubor

@@ -147,4 +147,3 @@
147 147
     }
148 148
   }
149 149
 }
150
-