hybrid-navigation 2.13.0-beta.3 → 2.13.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (58) hide show
  1. package/android/src/main/java/com/reactnative/hybridnavigation/ReactFragment.java +3 -20
  2. package/lib/Event.d.ts +12 -16
  3. package/lib/Event.js +20 -66
  4. package/lib/Garden.d.ts +49 -6
  5. package/lib/Garden.js +56 -32
  6. package/lib/Navigation.d.ts +68 -0
  7. package/lib/Navigation.js +162 -0
  8. package/lib/NavigationComponentWrap.d.ts +19 -0
  9. package/lib/NavigationComponentWrap.js +46 -0
  10. package/lib/Navigator.d.ts +46 -32
  11. package/lib/Navigator.js +110 -169
  12. package/lib/ReactRegistry.d.ts +19 -13
  13. package/lib/ReactRegistry.js +21 -73
  14. package/lib/Route.d.ts +13 -8
  15. package/lib/handler/BarButtonEventHandler.d.ts +10 -0
  16. package/lib/handler/BarButtonEventHandler.js +38 -0
  17. package/lib/handler/DispatchCommandHandler.d.ts +26 -0
  18. package/lib/handler/DispatchCommandHandler.js +28 -0
  19. package/lib/handler/LayoutCommandHandler.d.ts +11 -0
  20. package/lib/handler/LayoutCommandHandler.js +39 -0
  21. package/lib/handler/ResultEventHandler.d.ts +15 -0
  22. package/lib/handler/ResultEventHandler.js +47 -0
  23. package/lib/handler/VisibilityEventHandler.d.ts +13 -0
  24. package/lib/handler/VisibilityEventHandler.js +32 -0
  25. package/lib/hooks.d.ts +4 -0
  26. package/lib/hooks.js +13 -18
  27. package/lib/index.d.ts +8 -1
  28. package/lib/index.js +26 -2
  29. package/lib/router/drawer.d.ts +3 -1
  30. package/lib/router/drawer.js +17 -17
  31. package/lib/router/index.d.ts +19 -15
  32. package/lib/router/index.js +140 -116
  33. package/lib/router/screen.d.ts +5 -1
  34. package/lib/router/screen.js +29 -0
  35. package/lib/router/stack.d.ts +5 -1
  36. package/lib/router/stack.js +59 -46
  37. package/lib/router/tabs.d.ts +3 -1
  38. package/lib/router/tabs.js +18 -28
  39. package/package.json +1 -1
  40. package/src/Event.ts +25 -90
  41. package/src/Garden.ts +60 -38
  42. package/src/Navigation.ts +253 -0
  43. package/src/NavigationComponentWrap.tsx +73 -0
  44. package/src/Navigator.ts +124 -233
  45. package/src/ReactRegistry.tsx +23 -99
  46. package/src/Route.ts +14 -9
  47. package/src/handler/BarButtonEventHandler.ts +50 -0
  48. package/src/handler/DispatchCommandHandler.ts +58 -0
  49. package/src/handler/LayoutCommandHandler.ts +49 -0
  50. package/src/handler/ResultEventHandler.ts +67 -0
  51. package/src/handler/VisibilityEventHandler.ts +44 -0
  52. package/src/hooks.ts +18 -30
  53. package/src/index.ts +38 -4
  54. package/src/router/drawer.ts +20 -17
  55. package/src/router/index.ts +150 -119
  56. package/src/router/screen.ts +34 -1
  57. package/src/router/stack.ts +61 -48
  58. package/src/router/tabs.ts +19 -29
package/src/Event.ts CHANGED
@@ -1,7 +1,8 @@
1
- import { NativeModules, NativeEventEmitter, NativeModule, EventSubscription } from 'react-native'
1
+ import { NativeModules, NativeEventEmitter, NativeModule } from 'react-native'
2
2
  import GardenModule from './GardenModule'
3
+ import type { Visibility } from './Route'
3
4
 
4
- function listenStatusBarHeightChanged(listener: (statusBarHeight: number) => void) {
5
+ function listenStatusBarHeightChange(listener: (statusBarHeight: number) => void) {
5
6
  const GardenEventReceiver = new NativeEventEmitter(GardenModule)
6
7
  const { EVENT_STATUSBAR_FRAME_CHANGE } = GardenModule.getConstants()
7
8
  return GardenEventReceiver.addListener(EVENT_STATUSBAR_FRAME_CHANGE, ({ statusBarHeight }) => {
@@ -58,18 +59,17 @@ function listenComponentResult(
58
59
  })
59
60
  }
60
61
 
61
- function listenComponentVisibility(
62
- componentDidAppear: (sceneId: string) => void,
63
- componentDidDisappear: (sceneId: string) => void,
64
- ) {
62
+ function listenComponentVisibility(listener: (sceneId: string, visibility: Visibility) => void) {
65
63
  const { EVENT_NAVIGATION, KEY_ON, ON_COMPONENT_APPEAR, ON_COMPONENT_DISAPPEAR, KEY_SCENE_ID } =
66
64
  HBDEventEmitter.getConstants()
67
65
 
68
66
  return HBDEventReceiver.addListener(EVENT_NAVIGATION, data => {
69
67
  if (data[KEY_ON] === ON_COMPONENT_APPEAR) {
70
- componentDidAppear(data[KEY_SCENE_ID])
71
- } else if (data[KEY_ON] === ON_COMPONENT_DISAPPEAR) {
72
- componentDidDisappear(data[KEY_SCENE_ID])
68
+ listener(data[KEY_SCENE_ID], 'visible')
69
+ }
70
+
71
+ if (data[KEY_ON] === ON_COMPONENT_DISAPPEAR) {
72
+ listener(data[KEY_SCENE_ID], 'invisible')
73
73
  }
74
74
  })
75
75
  }
@@ -89,7 +89,7 @@ function listenBarButtonItemClick(listener: (sceneId: string, action: string) =>
89
89
  )
90
90
  }
91
91
 
92
- function listenSwitchTab(listener: (sceneId: string, from: number, to: number) => void) {
92
+ function listenTabSwitch(listener: (sceneId: string, from: number, to: number) => void) {
93
93
  const { EVENT_SWITCH_TAB, KEY_INDEX, KEY_SCENE_ID } = HBDEventEmitter.getConstants()
94
94
  return HBDEventReceiver.addListener(EVENT_SWITCH_TAB, event => {
95
95
  const index = event[KEY_INDEX]
@@ -98,91 +98,26 @@ function listenSwitchTab(listener: (sceneId: string, from: number, to: number) =
98
98
  })
99
99
  }
100
100
 
101
- function listenSetRoot(willSetRoot: () => void, didSetRoot: (tag: number) => void) {
102
- const { EVENT_WILL_SET_ROOT, EVENT_DID_SET_ROOT } = HBDEventEmitter.getConstants()
103
-
104
- const subscriptions: EventSubscription[] = []
105
-
106
- subscriptions.push(
107
- HBDEventReceiver.addListener(EVENT_WILL_SET_ROOT, _ => {
108
- willSetRoot()
109
- }),
110
- )
111
-
112
- subscriptions.push(
113
- HBDEventReceiver.addListener(EVENT_DID_SET_ROOT, (event: { tag: number }) => {
114
- didSetRoot(event.tag)
115
- }),
116
- )
117
-
118
- return {
119
- remove: () => {
120
- subscriptions.forEach(subscription => subscription.remove())
121
- },
122
- }
123
- }
124
-
125
- const actions: Record<string, Function> = {}
126
- const keyPairs: Record<string, string[]> = {}
127
-
128
- let actionIdGenerator = 0
129
- let barButtonClickHandlerFactory = () => {
130
- return (sceneId: string, value: Function) => {
131
- console.warn(`barButtonClickHandlerFactory 没有正确绑定`, sceneId, value)
132
- }
133
- }
134
-
135
- function setBarButtonClickHandlerFactory(generator: typeof barButtonClickHandlerFactory) {
136
- barButtonClickHandlerFactory = generator
137
- }
138
-
139
- function bindBarButtonClickEvent(sceneId: string, item: object | null | undefined): object | null {
140
- if (item === null || item === undefined) {
141
- return null
142
- }
143
-
144
- return JSON.parse(
145
- JSON.stringify(item, (key, value) => {
146
- if (key !== 'action' || typeof value !== 'function') {
147
- return value
148
- }
149
-
150
- const actionKey = 'ON_BAR_BUTTON_ITEM_CLICK_' + actionIdGenerator++
151
-
152
- if (!keyPairs[sceneId]) {
153
- keyPairs[sceneId] = []
154
- }
155
- keyPairs[sceneId].push(actionKey)
156
- actions[actionKey] = value
157
-
158
- return actionKey
159
- }),
160
- )
101
+ function listenWillSetRoot(willSetRoot: () => void) {
102
+ const { EVENT_WILL_SET_ROOT } = HBDEventEmitter.getConstants()
103
+ return HBDEventReceiver.addListener(EVENT_WILL_SET_ROOT, _ => {
104
+ willSetRoot()
105
+ })
161
106
  }
162
107
 
163
- function removeBarButtonClickEvent(sceneId: string): void {
164
- const keys = keyPairs[sceneId]
165
- delete keyPairs[sceneId]
166
- if (keys) {
167
- keys.forEach(key => delete actions[key])
168
- }
108
+ function listenDidSetRoot(didSetRoot: (tag: number) => void) {
109
+ const { EVENT_DID_SET_ROOT } = HBDEventEmitter.getConstants()
110
+ return HBDEventReceiver.addListener(EVENT_DID_SET_ROOT, (event: { tag: number }) => {
111
+ didSetRoot(event.tag)
112
+ })
169
113
  }
170
114
 
171
- listenBarButtonItemClick((sceneId: string, action: string) => {
172
- const listeners = actions
173
- if (listeners[action]) {
174
- const handler = barButtonClickHandlerFactory()
175
- handler(sceneId, listeners[action])
176
- }
177
- })
178
-
179
115
  export default {
180
- listenStatusBarHeightChanged,
116
+ listenStatusBarHeightChange,
117
+ listenBarButtonItemClick,
181
118
  listenComponentResult,
182
119
  listenComponentVisibility,
183
- listenSwitchTab,
184
- listenSetRoot,
185
- bindBarButtonClickEvent,
186
- removeBarButtonClickEvent,
187
- setBarButtonClickHandlerFactory,
120
+ listenTabSwitch,
121
+ listenWillSetRoot,
122
+ listenDidSetRoot,
188
123
  }
package/src/Garden.ts CHANGED
@@ -1,7 +1,5 @@
1
- import { Platform } from 'react-native'
2
- import GardenModule from './GardenModule'
3
- import Event from './Event'
4
- import {
1
+ import Navigation from './Navigation'
2
+ import type {
5
3
  BarButtonItem,
6
4
  DefaultOptions,
7
5
  NavigationOption,
@@ -14,68 +12,92 @@ type Nullable<T> = {
14
12
  [P in keyof T]: T[P] extends T[P] | undefined ? T[P] | null : T[P]
15
13
  }
16
14
 
17
- const { STATUSBAR_HEIGHT, TOOLBAR_HEIGHT } = GardenModule.getConstants()
18
-
19
- if (Platform.OS === 'ios') {
20
- Event.listenStatusBarHeightChanged(statusBarHeight => {
21
- Garden.statusBarHeight = statusBarHeight
22
- Garden.topBarHeight = statusBarHeight + TOOLBAR_HEIGHT
23
- })
24
- }
25
-
26
- export class Garden {
27
- static setStyle(style: DefaultOptions = {}) {
28
- GardenModule.setStyle(style)
15
+ export interface Garden {}
16
+
17
+ /**
18
+ * @deprecated use Navigation instead. import Navigation from 'hybrid-navigation'
19
+ */
20
+ export class Garden implements Garden {
21
+ /**
22
+ * @deprecated Use Navigation.setDefaultOptions() instead.
23
+ * @param style
24
+ */
25
+ static setStyle(style: DefaultOptions) {
26
+ Navigation.setDefaultOptions(style)
29
27
  }
30
28
 
31
- static statusBarHeight: number = STATUSBAR_HEIGHT
32
- static toolbarHeight: number = TOOLBAR_HEIGHT
33
- static topBarHeight: number = TOOLBAR_HEIGHT + STATUSBAR_HEIGHT
34
-
35
29
  constructor(public sceneId: string) {}
36
30
 
37
- // --------------- instance method --------------
38
-
31
+ /**
32
+ * @deprecated Use Navigation.setLeftBarButtonItem() instead.
33
+ * @param buttonItem
34
+ */
39
35
  setLeftBarButtonItem(buttonItem: Nullable<BarButtonItem> | null) {
40
- const options = Event.bindBarButtonClickEvent(this.sceneId, buttonItem)
41
- GardenModule.setLeftBarButtonItem(this.sceneId, options)
36
+ Navigation.setLeftBarButtonItem(this.sceneId, buttonItem)
42
37
  }
43
38
 
39
+ /**
40
+ * @deprecated Use Navigation.setRightBarButtonItem() instead.
41
+ * @param buttonItem
42
+ */
44
43
  setRightBarButtonItem(buttonItem: Nullable<BarButtonItem> | null) {
45
- const options = Event.bindBarButtonClickEvent(this.sceneId, buttonItem)
46
- GardenModule.setRightBarButtonItem(this.sceneId, options)
44
+ Navigation.setRightBarButtonItem(this.sceneId, buttonItem)
47
45
  }
48
46
 
47
+ /**
48
+ *
49
+ * @deprecated Use Navigation.setLeftBarButtonItems() instead.
50
+ * @param buttonItems
51
+ */
49
52
  setLeftBarButtonItems(buttonItems: Array<Nullable<BarButtonItem>> | null) {
50
- const options = Event.bindBarButtonClickEvent(this.sceneId, buttonItems)
51
- GardenModule.setLeftBarButtonItems(this.sceneId, options)
53
+ Navigation.setLeftBarButtonItems(this.sceneId, buttonItems)
52
54
  }
53
55
 
56
+ /**
57
+ * @deprecated Use Navigation.setRightBarButtonItems() instead.
58
+ * @param buttonItems
59
+ */
54
60
  setRightBarButtonItems(buttonItems: Array<Nullable<BarButtonItem>> | null) {
55
- const options = Event.bindBarButtonClickEvent(this.sceneId, buttonItems)
56
- GardenModule.setRightBarButtonItems(this.sceneId, options)
61
+ Navigation.setRightBarButtonItems(this.sceneId, buttonItems)
57
62
  }
58
63
 
64
+ /**
65
+ * @deprecated Use Navigation.setTitleItem() instead.
66
+ * @param titleItem
67
+ */
59
68
  setTitleItem(titleItem: TitleItem) {
60
- GardenModule.setTitleItem(this.sceneId, titleItem)
69
+ Navigation.setTitleItem(this.sceneId, titleItem)
61
70
  }
62
71
 
72
+ /**
73
+ * @deprecated Use Navigation.updateOptions() instead.
74
+ * @param options
75
+ */
63
76
  updateOptions(options: NavigationOption) {
64
- GardenModule.updateOptions(this.sceneId, options)
77
+ Navigation.updateOptions(this.sceneId, options)
65
78
  }
66
79
 
80
+ /**
81
+ * @deprecated Use Navigation.updateTabBar() instead.
82
+ * @param options
83
+ */
67
84
  updateTabBar(options: TabBarStyle) {
68
- GardenModule.updateTabBar(this.sceneId, options)
85
+ Navigation.updateTabBar(this.sceneId, options)
69
86
  }
70
87
 
88
+ /**
89
+ * @deprecated Use Navigation.setTabItem() instead.
90
+ * @param item
91
+ */
71
92
  setTabItem(item: TabItemInfo | TabItemInfo[]) {
72
- if (!Array.isArray(item)) {
73
- item = [item]
74
- }
75
- GardenModule.setTabItem(this.sceneId, item)
93
+ Navigation.setTabItem(this.sceneId, item)
76
94
  }
77
95
 
96
+ /**
97
+ * @deprecated Use Navigation.setMenuInteractive() instead.
98
+ * @param enabled
99
+ */
78
100
  setMenuInteractive(enabled: boolean) {
79
- GardenModule.setMenuInteractive(this.sceneId, enabled)
101
+ Navigation.setMenuInteractive(this.sceneId, enabled)
80
102
  }
81
103
  }
@@ -0,0 +1,253 @@
1
+ import type React from 'react'
2
+ import { AppRegistry, ComponentProvider } from 'react-native'
3
+
4
+ import type {
5
+ BarButtonItem,
6
+ DefaultOptions,
7
+ NavigationOption,
8
+ TabBarStyle,
9
+ TabItemInfo,
10
+ TitleItem,
11
+ } from './Options'
12
+ import type { BuildInLayout, Layout, Route, RouteGraph, RouteConfig } from './Route'
13
+
14
+ import BarButtonEventHandler from './handler/BarButtonEventHandler'
15
+ import LayoutCommandHandler from './handler/LayoutCommandHandler'
16
+ import DispatchCommandHandler from './handler/DispatchCommandHandler'
17
+ import ResultEventHandler, { ResultType } from './handler/ResultEventHandler'
18
+ import VisibilityEventHandler, {
19
+ GlobalVisibilityEventListener,
20
+ VisibilityEventListener,
21
+ } from './handler/VisibilityEventHandler'
22
+
23
+ import Event from './Event'
24
+ import GardenModule from './GardenModule'
25
+ import NavigationModule from './NavigationModule'
26
+
27
+ export type { ResultType } from './handler/ResultEventHandler'
28
+ import type { DispatchParams, NavigationInterceptor } from './handler/DispatchCommandHandler'
29
+ export type { DispatchParams, NavigationInterceptor } from './handler/DispatchCommandHandler'
30
+
31
+ type BarButtonClickEventListener = (sceneId: string, value: Function) => void
32
+
33
+ type Nullable<T> = {
34
+ [P in keyof T]: T[P] extends T[P] | undefined ? T[P] | null : T[P]
35
+ }
36
+
37
+ export const { RESULT_CANCEL, RESULT_OK } = NavigationModule.getConstants()
38
+
39
+ export type NavigationSubscription = {
40
+ remove: () => void
41
+ }
42
+
43
+ export type HOC = (WrappedComponent: React.ComponentType<any>) => React.ComponentType<any>
44
+
45
+ export interface Navigation {}
46
+
47
+ export class Navigation implements Navigation {
48
+ private dispatchHandler = new DispatchCommandHandler()
49
+ private resultHandler = new ResultEventHandler()
50
+ private buttonHandler = new BarButtonEventHandler()
51
+ private layoutHandler = new LayoutCommandHandler()
52
+ private visibilityHandler = new VisibilityEventHandler()
53
+
54
+ constructor() {
55
+ this.handleTabSwitch()
56
+ this.layoutHandler.handleRootLayoutChange()
57
+ this.visibilityHandler.handleComponentVisibility()
58
+ this.resultHandler.handleComponentResult()
59
+ }
60
+
61
+ private wrap?: (moduleName: string) => HOC
62
+ private hoc?: HOC
63
+ private registerEnded = false
64
+
65
+ startRegisterComponent(hoc?: HOC) {
66
+ this.hoc = hoc
67
+ this.registerEnded = false
68
+ NavigationModule.startRegisterReactComponent()
69
+ }
70
+
71
+ endRegisterComponent() {
72
+ if (this.registerEnded) {
73
+ console.warn(`Please don't call ReactRegistry#endRegisterComponent multiple times.`)
74
+ return
75
+ }
76
+ this.registerEnded = true
77
+ NavigationModule.endRegisterReactComponent()
78
+ }
79
+
80
+ registerComponent(
81
+ appKey: string,
82
+ getComponentFunc: ComponentProvider,
83
+ routeConfig?: RouteConfig,
84
+ ) {
85
+ if (routeConfig) {
86
+ this.registerRoute(appKey, routeConfig)
87
+ }
88
+
89
+ let WrappedComponent = getComponentFunc()
90
+ if (this.hoc) {
91
+ WrappedComponent = this.hoc(WrappedComponent)
92
+ }
93
+
94
+ // build static options
95
+ let options: object =
96
+ this.bindBarButtonClickEvent('permanent', (WrappedComponent as any).navigationItem) || {}
97
+ NavigationModule.registerReactComponent(appKey, options)
98
+
99
+ let RootComponent = this.wrap!(appKey)(WrappedComponent)
100
+ AppRegistry.registerComponent(appKey, () => RootComponent)
101
+ }
102
+
103
+ private _routeConfigs = new Map<string, RouteConfig>()
104
+
105
+ private registerRoute(moduleName: string, route: RouteConfig) {
106
+ this._routeConfigs.set(moduleName, route)
107
+ }
108
+
109
+ routeConfigs() {
110
+ return this._routeConfigs
111
+ }
112
+
113
+ setNavigationComponentWrap(wrap: (moduleName: string) => HOC) {
114
+ this.wrap = wrap
115
+ }
116
+
117
+ addGlobalVisibilityEventListener(
118
+ listener: GlobalVisibilityEventListener,
119
+ ): NavigationSubscription {
120
+ this.visibilityHandler.addGlobalVisibilityEventListener(listener)
121
+ return {
122
+ remove: () => this.visibilityHandler.removeGlobalVisibilityEventListener(listener),
123
+ }
124
+ }
125
+
126
+ addVisibilityEventListener(
127
+ sceneId: string,
128
+ listener: VisibilityEventListener,
129
+ ): NavigationSubscription {
130
+ this.visibilityHandler.addVisibilityEventListener(sceneId, listener)
131
+ return {
132
+ remove: () => this.visibilityHandler.removeVisibilityEventListener(sceneId, listener),
133
+ }
134
+ }
135
+
136
+ dispatch(sceneId: string, action: string, params: DispatchParams = {}) {
137
+ return this.dispatchHandler.dispatch(sceneId, action, params)
138
+ }
139
+
140
+ private handleTabSwitch() {
141
+ Event.listenTabSwitch((sceneId: string, from: number, to: number) => {
142
+ this.dispatch(sceneId, 'switchTab', { from, to })
143
+ })
144
+ }
145
+
146
+ setInterceptor(interceptor: NavigationInterceptor) {
147
+ this.dispatchHandler.setInterceptor(interceptor)
148
+ }
149
+
150
+ setResult = <T extends ResultType>(
151
+ sceneId: string,
152
+ resultCode: number,
153
+ data: T = null as T,
154
+ ): void => NavigationModule.setResult(sceneId, resultCode, data)
155
+
156
+ result<T>(sceneId: string, resultCode: number) {
157
+ return this.resultHandler.waitResult<T>(sceneId, resultCode)
158
+ }
159
+
160
+ unmount(sceneId: string) {
161
+ this.resultHandler.invalidateResultEventListener(sceneId)
162
+ this.buttonHandler.unbindBarButtonClickEvent(sceneId)
163
+ }
164
+
165
+ setRoot(layout: BuildInLayout | Layout, sticky = false) {
166
+ return this.layoutHandler.setRoot(layout, sticky)
167
+ }
168
+
169
+ setRootLayoutUpdateListener(willSetRoot = () => {}, didSetRoot = () => {}) {
170
+ this.layoutHandler.setRootLayoutUpdateListener(willSetRoot, didSetRoot)
171
+ }
172
+
173
+ currentTab(sceneId: string) {
174
+ return NavigationModule.currentTab(sceneId)
175
+ }
176
+
177
+ findSceneIdByModuleName(moduleName: string) {
178
+ return NavigationModule.findSceneIdByModuleName(moduleName)
179
+ }
180
+
181
+ isStackRoot(sceneId: string) {
182
+ return NavigationModule.isStackRoot(sceneId)
183
+ }
184
+
185
+ signalFirstRenderComplete(sceneId: string) {
186
+ NavigationModule.signalFirstRenderComplete(sceneId)
187
+ }
188
+
189
+ currentRoute(): Promise<Route> {
190
+ return NavigationModule.currentRoute()
191
+ }
192
+
193
+ routeGraph(): Promise<RouteGraph[]> {
194
+ return NavigationModule.routeGraph()
195
+ }
196
+
197
+ setDefaultOptions(options: DefaultOptions) {
198
+ GardenModule.setStyle(options)
199
+ }
200
+
201
+ setLeftBarButtonItem(sceneId: string, buttonItem: Nullable<BarButtonItem> | null) {
202
+ const options = this.bindBarButtonClickEvent(sceneId, buttonItem)
203
+ GardenModule.setLeftBarButtonItem(sceneId, options)
204
+ }
205
+
206
+ setRightBarButtonItem(sceneId: string, buttonItem: Nullable<BarButtonItem> | null) {
207
+ const options = this.bindBarButtonClickEvent(sceneId, buttonItem)
208
+ GardenModule.setRightBarButtonItem(sceneId, options)
209
+ }
210
+
211
+ setLeftBarButtonItems(sceneId: string, buttonItems: Array<Nullable<BarButtonItem>> | null) {
212
+ const options = this.bindBarButtonClickEvent(sceneId, buttonItems)
213
+ GardenModule.setLeftBarButtonItems(sceneId, options)
214
+ }
215
+
216
+ setRightBarButtonItems(sceneId: string, buttonItems: Array<Nullable<BarButtonItem>> | null) {
217
+ const options = this.bindBarButtonClickEvent(sceneId, buttonItems)
218
+ GardenModule.setRightBarButtonItems(sceneId, options)
219
+ }
220
+
221
+ setTitleItem(sceneId: string, titleItem: TitleItem) {
222
+ GardenModule.setTitleItem(sceneId, titleItem)
223
+ }
224
+
225
+ updateOptions(sceneId: string, options: NavigationOption) {
226
+ GardenModule.updateOptions(sceneId, options)
227
+ }
228
+
229
+ updateTabBar(sceneId: string, options: TabBarStyle) {
230
+ GardenModule.updateTabBar(sceneId, options)
231
+ }
232
+
233
+ setTabItem(sceneId: string, item: TabItemInfo | TabItemInfo[]) {
234
+ if (!Array.isArray(item)) {
235
+ item = [item]
236
+ }
237
+ GardenModule.setTabItem(sceneId, item)
238
+ }
239
+
240
+ setMenuInteractive(sceneId: string, enabled: boolean) {
241
+ GardenModule.setMenuInteractive(sceneId, enabled)
242
+ }
243
+
244
+ setBarButtonClickEventListener(listener: BarButtonClickEventListener) {
245
+ this.buttonHandler.setBarButtonClickEventListener(listener)
246
+ }
247
+
248
+ private bindBarButtonClickEvent(sceneId: string, item: object | null | undefined): object | null {
249
+ return this.buttonHandler.bindBarButtonClickEvent(sceneId, item)
250
+ }
251
+ }
252
+
253
+ export default new Navigation()
@@ -0,0 +1,73 @@
1
+ import React, { useEffect } from 'react'
2
+
3
+ import type { Garden } from './Garden'
4
+ import type { NavigationItem } from './Options'
5
+
6
+ import { Navigator, NavigationContext } from './Navigator'
7
+
8
+ export interface NavigationProps {
9
+ navigator: Navigator
10
+ garden: Garden
11
+ sceneId: string
12
+ }
13
+
14
+ /**
15
+ * @deprecated Use NavigationProps instead.
16
+ */
17
+ export type InjectedProps = NavigationProps
18
+
19
+ interface NativeProps {
20
+ sceneId: string
21
+ }
22
+
23
+ function getDisplayName(WrappedComponent: React.ComponentType<any>) {
24
+ return WrappedComponent.displayName || WrappedComponent.name || 'Component'
25
+ }
26
+
27
+ export function withNavigation(moduleName: string) {
28
+ return function (WrappedComponent: React.ComponentType<any>) {
29
+ const FC = React.forwardRef((props: NativeProps, ref: React.Ref<React.ComponentType<any>>) => {
30
+ const { sceneId } = props
31
+
32
+ const navigator = Navigator.of(sceneId)
33
+ if (navigator.moduleName === undefined) {
34
+ navigator.moduleName = moduleName
35
+ }
36
+
37
+ const garden = navigator.garden
38
+
39
+ useEffect(() => {
40
+ navigator.signalFirstRenderComplete()
41
+ return () => {
42
+ navigator.unmount()
43
+ }
44
+ }, [navigator])
45
+
46
+ const injected = {
47
+ garden,
48
+ navigator,
49
+ }
50
+
51
+ return (
52
+ <NavigationContext.Provider value={navigator}>
53
+ <WrappedComponent ref={ref} {...props} {...injected} />
54
+ </NavigationContext.Provider>
55
+ )
56
+ })
57
+
58
+ FC.displayName = `withNavigation(${getDisplayName(WrappedComponent)})`
59
+ return FC
60
+ }
61
+ }
62
+
63
+ export function withNavigationItem(item: NavigationItem) {
64
+ return function (WrappedComponent: React.ComponentType<any>): React.ComponentType<any> {
65
+ let navigationItem = (WrappedComponent as any).navigationItem
66
+ if (navigationItem) {
67
+ ;(WrappedComponent as any).navigationItem = { ...navigationItem, ...item }
68
+ } else {
69
+ ;(WrappedComponent as any).navigationItem = item
70
+ }
71
+ return WrappedComponent
72
+ }
73
+ }