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
 package com.reactnativenavigation.react;
1
 package com.reactnativenavigation.react;
2
 
2
 
3
 public class Constants {
3
 public class Constants {
4
+    public static final String BACK_BUTTON_JS_KEY = "backButton";
4
     public static final String BACK_BUTTON_ID = "RNN.back";
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
 
24
 
25
 import org.json.JSONObject;
25
 import org.json.JSONObject;
26
 
26
 
27
+import java.util.HashMap;
27
 import java.util.Map;
28
 import java.util.Map;
28
 
29
 
29
 public class NavigationModule extends ReactContextBaseJavaModule {
30
 public class NavigationModule extends ReactContextBaseJavaModule {
45
 		return NAME;
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
 	@ReactMethod
56
 	@ReactMethod
49
 	public void setRoot(String commandId, ReadableMap rawLayoutTree, Promise promise) {
57
 	public void setRoot(String commandId, ReadableMap rawLayoutTree, Promise promise) {
50
 		final LayoutNode layoutTree = LayoutNodeParser.parse(new JSONObject(rawLayoutTree.toHashMap()));
58
 		final LayoutNode layoutTree = LayoutNodeParser.parse(new JSONObject(rawLayoutTree.toHashMap()));

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

11
 import { Element } from './adapters/Element';
11
 import { Element } from './adapters/Element';
12
 import { ComponentEventsObserver } from './events/ComponentEventsObserver';
12
 import { ComponentEventsObserver } from './events/ComponentEventsObserver';
13
 import { CommandsObserver } from './events/CommandsObserver';
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
 export class Navigation {
19
 export class Navigation {
16
   public readonly Element: React.ComponentType<{ elementId: any; resizeMode: any; }>;
20
   public readonly Element: React.ComponentType<{ elementId: any; resizeMode: any; }>;
21
+  public readonly constants = new Constants(NavigationModule);
17
 
22
 
18
   private readonly store;
23
   private readonly store;
19
   private readonly nativeEventsReceiver;
24
   private readonly nativeEventsReceiver;

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

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

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