Browse Source

Expose constants to Js from Android

Currently exporting `backButton` id, usage: `Navigation.constants.backButton`
Guy Carmeli 6 years ago
parent
commit
f6712c6699

+ 1
- 0
lib/android/app/src/main/java/com/reactnativenavigation/react/Constants.java View File

@@ -1,5 +1,6 @@
1 1
 package com.reactnativenavigation.react;
2 2
 
3 3
 public class Constants {
4
+    public static final String BACK_BUTTON_JS_KEY = "backButton";
4 5
     public static final String BACK_BUTTON_ID = "RNN.back";
5 6
 }

+ 8
- 0
lib/android/app/src/main/java/com/reactnativenavigation/react/NavigationModule.java View File

@@ -24,6 +24,7 @@ import com.reactnativenavigation.viewcontrollers.externalcomponent.ExternalCompo
24 24
 
25 25
 import org.json.JSONObject;
26 26
 
27
+import java.util.HashMap;
27 28
 import java.util.Map;
28 29
 
29 30
 public class NavigationModule extends ReactContextBaseJavaModule {
@@ -45,6 +46,13 @@ public class NavigationModule extends ReactContextBaseJavaModule {
45 46
 		return NAME;
46 47
 	}
47 48
 
49
+	@Override
50
+    public Map<String, Object> getConstants() {
51
+        final Map<String, Object> constants = new HashMap<>();
52
+        constants.put(Constants.BACK_BUTTON_JS_KEY, Constants.BACK_BUTTON_ID);
53
+        return constants;
54
+    }
55
+
48 56
 	@ReactMethod
49 57
 	public void setRoot(String commandId, ReadableMap rawLayoutTree, Promise promise) {
50 58
 		final LayoutNode layoutTree = LayoutNodeParser.parse(new JSONObject(rawLayoutTree.toHashMap()));

+ 5
- 0
lib/src/Navigation.ts View File

@@ -11,9 +11,14 @@ import { ComponentProvider } from 'react-native';
11 11
 import { Element } from './adapters/Element';
12 12
 import { ComponentEventsObserver } from './events/ComponentEventsObserver';
13 13
 import { CommandsObserver } from './events/CommandsObserver';
14
+import { Constants } from './constants/Constants';
15
+import { NativeModules } from 'react-native';
16
+
17
+const NavigationModule = NativeModules.RNNBridgeModule;
14 18
 
15 19
 export class Navigation {
16 20
   public readonly Element: React.ComponentType<{ elementId: any; resizeMode: any; }>;
21
+  public readonly constants = new Constants(NavigationModule);
17 22
 
18 23
   private readonly store;
19 24
   private readonly nativeEventsReceiver;

+ 17
- 0
lib/src/constants/Constants.test.ts View File

@@ -0,0 +1,17 @@
1
+import { Constants } from './Constants';
2
+
3
+const NavigationModule = {
4
+  backButton: 'backButton'
5
+};
6
+
7
+describe('Constants', () => {
8
+  let uut: Constants;
9
+
10
+  beforeEach(() => {
11
+    uut = new Constants(NavigationModule);
12
+  });
13
+
14
+  it('backButton', () => {
15
+    expect(uut.backButton).toEqual(NavigationModule.backButton);
16
+  });
17
+});

+ 7
- 0
lib/src/constants/Constants.ts View File

@@ -0,0 +1,7 @@
1
+export class Constants {
2
+  public readonly backButton;
3
+
4
+  constructor(navigationModule) {
5
+    this.backButton = navigationModule.backButton;
6
+  }
7
+}