@momo-kits/foundation 0.75.1-beta.6 → 0.75.1-beta.7

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.
@@ -13,11 +13,11 @@ import {Image} from '../Image';
13
13
  import {
14
14
  HeaderBackgroundProps,
15
15
  HeaderBackProps,
16
+ HeaderToolkitProps,
16
17
  TitleCustomProps,
17
18
  } from './types';
18
19
  import {Text} from '../Text';
19
20
  import {Icon} from '../Icon';
20
- import {requestApi} from './utils';
21
21
  import Observer from './Observer';
22
22
  import {PopupNotify} from '../Popup';
23
23
 
@@ -216,12 +216,15 @@ const HeaderRightAction: React.FC<any> = ({children, ...restProps}) => {
216
216
  console.warn(
217
217
  'element type of NavigationButton, Please migrate to use NavigationButton of kits.',
218
218
  );
219
+ return <View />;
219
220
  }
220
221
  };
221
222
  const renderAction = () => {
222
223
  if (Array.isArray(children)) {
223
224
  return children.map((child, index) => {
224
- validType(child);
225
+ if (__DEV__) {
226
+ validType(child);
227
+ }
225
228
  return (
226
229
  <React.Fragment key={index}>
227
230
  {React.cloneElement(child, {...restProps})}
@@ -236,62 +239,80 @@ const HeaderRightAction: React.FC<any> = ({children, ...restProps}) => {
236
239
  return <View style={styles.headerRightButton}>{renderAction()}</View>;
237
240
  };
238
241
 
239
- const HeaderToolkitAction: React.FC<any> = ({tintColor, utilityToolConfig}) => {
240
- const {appCode, hidden, type, onAction} = utilityToolConfig ?? {};
241
- const {theme} = useContext(ApplicationContext);
242
- const [showUtility, setShowUtility] = useState(hidden === false);
242
+ const HeaderToolkitAction: React.FC<any> = ({tintColor}) => {
243
+ const {theme, navigator} = useContext(ApplicationContext);
244
+ const [toolConfig, setToolConfig] = useState<HeaderToolkitProps>();
245
+ const {appCode, hidden, type, onAction} = toolConfig ?? {};
243
246
 
244
247
  let backgroundColor;
245
248
  if (tintColor != Colors.black_01) {
246
249
  backgroundColor = theme.colors.background.surface;
247
250
  }
248
251
 
252
+ useEffect(() => {
253
+ navigator?.getToolkit().then(toolConfig => {
254
+ setToolConfig(toolConfig);
255
+ });
256
+ }, []);
257
+
249
258
  useEffect(() => {
250
259
  let addFavorite: any;
251
260
  let removeFavorite: any;
252
- if (appCode && type === 'addFavorite') {
261
+ if (appCode) {
253
262
  const addSchema = `observerToolkit_addFavorite_${appCode}`;
254
263
  const removeSchema = `observerToolkit_removeFavorite_${appCode}`;
255
- addFavorite = Observer?.getInstance(addSchema);
256
- removeFavorite = Observer?.getInstance(removeSchema);
257
-
258
- addFavorite?.attach((data: any) => {
259
- if (data?.status) {
260
- setShowUtility(false);
261
- }
262
- });
263
- removeFavorite?.attach((data: any) => {
264
- if (data?.status) {
265
- setShowUtility(true);
266
- }
267
- });
264
+ addFavorite = navigator?.requestApi(
265
+ 'observer',
266
+ [addSchema],
267
+ (data: any) => {
268
+ if (data?.status) {
269
+ setToolConfig({
270
+ ...(toolConfig as HeaderToolkitProps),
271
+ hidden: true,
272
+ });
273
+ }
274
+ },
275
+ );
276
+ addFavorite = navigator?.requestApi(
277
+ 'observer',
278
+ [removeSchema],
279
+ (data: any) => {
280
+ if (data?.status) {
281
+ setToolConfig({
282
+ ...(toolConfig as HeaderToolkitProps),
283
+ hidden: false,
284
+ });
285
+ }
286
+ },
287
+ );
268
288
  }
289
+
269
290
  return () => {
270
291
  addFavorite?.();
271
292
  removeFavorite?.();
272
293
  };
273
- }, []);
294
+ }, [appCode]);
274
295
 
275
296
  const onUtilityTool = () => {
276
297
  onAction?.('onUtilityTool');
277
- requestApi(type, [undefined], undefined);
298
+ navigator?.requestApi(type, undefined, undefined);
278
299
  };
279
300
 
280
301
  const onMore = () => {
281
302
  onAction?.('onMore');
282
- requestApi('showToolkit', [utilityToolConfig], undefined);
303
+ navigator?.requestApi('showToolkit', [toolConfig], undefined);
283
304
  };
284
305
 
285
306
  const onClose = () => {
286
307
  onAction?.('onClose');
287
- requestApi('dismiss', [], undefined);
308
+ navigator?.requestApi('dismiss', undefined, undefined);
288
309
  };
289
310
 
290
311
  const renderUtilityTool = () => {
291
- if (showUtility) {
312
+ if (toolConfig?.hidden === false) {
292
313
  return (
293
314
  <NavigationButton
294
- icon={type}
315
+ icon={type as string}
295
316
  tintColor={tintColor}
296
317
  onPress={onUtilityTool}
297
318
  />
@@ -299,33 +320,39 @@ const HeaderToolkitAction: React.FC<any> = ({tintColor, utilityToolConfig}) => {
299
320
  }
300
321
  };
301
322
 
302
- return (
303
- <View style={styles.headerRightButton}>
304
- {renderUtilityTool()}
305
- <View
306
- style={[
307
- styles.toolkitContainer,
308
- {
309
- backgroundColor: backgroundColor ?? '#00000066',
310
- borderWidth: backgroundColor ? 0.5 : 0,
311
- },
312
- ]}>
313
- <TouchableOpacity accessibilityLabel={'Toolkit/More'} onPress={onMore}>
314
- <Icon color={tintColor} source="navigation_more_horiz" size={20} />
315
- </TouchableOpacity>
316
- <View style={[styles.divider, {backgroundColor: tintColor}]} />
317
- <TouchableOpacity
318
- accessibilityLabel={'Toolkit/Close'}
319
- onPress={onClose}>
320
- <Icon
321
- color={tintColor}
322
- source="16_navigation_close_circle"
323
- size={20}
324
- />
325
- </TouchableOpacity>
323
+ if (toolConfig) {
324
+ return (
325
+ <View style={styles.headerRightButton}>
326
+ {renderUtilityTool()}
327
+ <View
328
+ style={[
329
+ styles.toolkitContainer,
330
+ {
331
+ backgroundColor: backgroundColor ?? '#00000066',
332
+ borderWidth: backgroundColor ? 0.5 : 0,
333
+ },
334
+ ]}>
335
+ <TouchableOpacity
336
+ accessibilityLabel={'Toolkit/More'}
337
+ onPress={onMore}>
338
+ <Icon color={tintColor} source="navigation_more_horiz" size={20} />
339
+ </TouchableOpacity>
340
+ <View style={[styles.divider, {backgroundColor: tintColor}]} />
341
+ <TouchableOpacity
342
+ accessibilityLabel={'Toolkit/Close'}
343
+ onPress={onClose}>
344
+ <Icon
345
+ color={tintColor}
346
+ source="16_navigation_close_circle"
347
+ size={20}
348
+ />
349
+ </TouchableOpacity>
350
+ </View>
326
351
  </View>
327
- </View>
328
- );
352
+ );
353
+ }
354
+
355
+ return <View />;
329
356
  };
330
357
 
331
358
  export {
@@ -2,6 +2,7 @@ import {NavigationProp} from '@react-navigation/native';
2
2
  import {NavigationOptions, Theme} from './types';
3
3
  import {HeaderRightAction} from './index';
4
4
  import {getOptions} from './utils';
5
+ import {HeaderToolkitAction} from './Components';
5
6
 
6
7
  class Navigation {
7
8
  instance: NavigationProp<any>;
@@ -15,9 +16,12 @@ class Navigation {
15
16
  verifyParams = (params: NavigationOptions) => {
16
17
  if (params.headerRight) {
17
18
  const headerRight = params.headerRight?.();
18
- if (headerRight.type !== HeaderRightAction) {
19
+ if (
20
+ headerRight?.type !== HeaderRightAction &&
21
+ headerRight?.type !== HeaderToolkitAction
22
+ ) {
19
23
  console.warn(
20
- 'headerRight not return element type of HeaderRightAction, Please migrate to use HeaderRightAction of kits.',
24
+ 'headerRight not return element type of HeaderRightAction | HeaderToolkitAction, Please migrate to use of kits.',
21
25
  );
22
26
  }
23
27
  }
@@ -29,7 +33,9 @@ class Navigation {
29
33
  };
30
34
 
31
35
  setOptions = (params: NavigationOptions) => {
32
- params = this.verifyParams(params);
36
+ if (__DEV__) {
37
+ params = this.verifyParams(params);
38
+ }
33
39
  params = getOptions(params, this.theme);
34
40
  this.instance.setOptions(params);
35
41
  };
@@ -21,9 +21,11 @@ const NavigationContainer: React.FC<NavigationContainerProps> = ({
21
21
  screen,
22
22
  theme,
23
23
  options,
24
+ maxApi,
24
25
  }) => {
25
26
  const navigationRef = React.useRef<NavigationContainerRef>(null);
26
27
  const navigator = useRef(new Navigator(navigationRef));
28
+ navigator.current.maxApi = maxApi;
27
29
 
28
30
  return (
29
31
  <SafeAreaProvider>
@@ -1,10 +1,15 @@
1
1
  import {StackActions} from '@react-navigation/native';
2
- import {BottomSheetParams, ModalParams, ScreenParams} from './types';
3
- import {requestApi} from './utils';
2
+ import {
3
+ BottomSheetParams,
4
+ HeaderToolkitProps,
5
+ ModalParams,
6
+ ScreenParams,
7
+ } from './types';
4
8
 
5
9
  class Navigator {
6
10
  ref?: any;
7
- utilityToolConfig: any;
11
+ toolkitConfig?: HeaderToolkitProps;
12
+ maxApi?: any;
8
13
 
9
14
  constructor(navigation: any) {
10
15
  this.ref = navigation;
@@ -43,17 +48,28 @@ class Navigator {
43
48
  this.ref.current.dispatch(StackActions.popToTop());
44
49
  };
45
50
 
46
- getToolkit = async () => {
47
- if (this.utilityToolConfig) return this.utilityToolConfig;
51
+ requestApi = (func: any, args: any = [], callback: any) => {
52
+ if (this.maxApi) {
53
+ this.maxApi[func]?.(...args, callback);
54
+ } else {
55
+ const instance: any = global;
56
+ instance?.miniAppApi?.dispatch?.(func, ...args, callback);
57
+ }
58
+ };
59
+ getToolkit = async (): Promise<HeaderToolkitProps | undefined> => {
48
60
  return new Promise(resolve => {
49
- requestApi('getToolkit', [null], (result: any) => {
50
- if (result === true) {
51
- requestApi('getToolkitConfig', [null], (config: any) => {
52
- this.utilityToolConfig = {
61
+ if (this.toolkitConfig) {
62
+ resolve(this.toolkitConfig);
63
+ return;
64
+ }
65
+ this.requestApi('getToolkit', [undefined], (result: boolean) => {
66
+ if (result) {
67
+ this.requestApi('getToolkitConfig', [undefined], (config: any) => {
68
+ this.toolkitConfig = {
53
69
  ...config?.utilityToolConfig,
54
70
  appCode: config?.appCode,
55
71
  };
56
- resolve(this.utilityToolConfig);
72
+ resolve(this.toolkitConfig);
57
73
  });
58
74
  } else {
59
75
  resolve(undefined);
@@ -2,7 +2,6 @@ import React, {useContext, useEffect} from 'react';
2
2
  import {ScreenParams} from './types';
3
3
  import Navigation from './Navigation';
4
4
  import {ApplicationContext} from './NavigationContainer';
5
- import {HeaderToolkitAction} from './Components';
6
5
 
7
6
  const StackScreen: React.FC<any> = props => {
8
7
  const {route, navigation} = props;
@@ -17,18 +16,6 @@ const StackScreen: React.FC<any> = props => {
17
16
  delete params.screen;
18
17
 
19
18
  useEffect(() => {
20
- navigator?.getToolkit().then(utilityToolConfig => {
21
- if (utilityToolConfig) {
22
- params.navigation.setOptions({
23
- headerRight: (props: any) => (
24
- <HeaderToolkitAction
25
- {...props}
26
- utilityToolConfig={utilityToolConfig}
27
- />
28
- ),
29
- });
30
- }
31
- });
32
19
  if (options) {
33
20
  params.navigation.setOptions(options);
34
21
  }
@@ -67,6 +67,7 @@ export type NavigationContainerProps = {
67
67
  screen: React.ElementType;
68
68
  options?: NavigationOptions;
69
69
  theme: Theme;
70
+ maxApi?: any;
70
71
  };
71
72
 
72
73
  export interface ScreenContainerProps extends ViewProps {
@@ -135,6 +136,14 @@ export type TitleCustomProps = {
135
136
  content?: React.ReactNode;
136
137
  };
137
138
 
139
+ export type HeaderToolkitProps = {
140
+ [key: string]: any;
141
+ appCode: string;
142
+ hidden: boolean;
143
+ type: string;
144
+ onAction?: (action: string) => void;
145
+ };
146
+
138
147
  export type BottomTabItemProps = {
139
148
  label: string;
140
149
  icon: string;
@@ -3,41 +3,16 @@ import {
3
3
  StackNavigationOptions,
4
4
  TransitionPresets,
5
5
  } from '@react-navigation/stack';
6
- import {DeviceEventEmitter} from 'react-native';
7
6
  import {
8
7
  HeaderBackground,
9
8
  HeaderCustom,
10
9
  HeaderLeft,
11
10
  HeaderTitle,
11
+ HeaderToolkitAction,
12
12
  } from './Components';
13
13
  import {NavigationOptions, Theme} from './types';
14
14
  import {Colors} from '../Consts';
15
15
 
16
- const requestApi = (func: any, args: any = [], callback: any) => {
17
- const instance: any = global;
18
- if (instance?.miniAppApi?.dispatch) {
19
- instance?.miniAppApi?.dispatch?.(func, ...args, callback);
20
- } else {
21
- let listener: any;
22
- const responseId = new Date().getTime();
23
- const responseListener = `KIT_RESPONSE_MESSAGE_${responseId}`;
24
- const TIME_OUT = 5000;
25
- const timeout = setTimeout(() => {
26
- listener?.remove?.();
27
- }, TIME_OUT);
28
- listener = DeviceEventEmitter.addListener(responseListener, result => {
29
- listener?.remove?.();
30
- clearTimeout(timeout);
31
- callback?.(result);
32
- });
33
- DeviceEventEmitter.emit('KITS_REQUEST_MESSAGE', {
34
- func,
35
- args,
36
- responseListener,
37
- });
38
- }
39
- };
40
-
41
16
  const getTintColor = (theme: Theme): any => {
42
17
  if (theme.assets?.headerBackground) {
43
18
  return {
@@ -55,6 +30,7 @@ const getStackOptions = (theme: Theme): StackNavigationOptions => {
55
30
  headerTitle: HeaderTitle,
56
31
  headerBackground: HeaderBackground,
57
32
  headerLeft: (props: any) => <HeaderLeft {...props} />,
33
+ headerRight: (props: any) => <HeaderToolkitAction {...props} />,
58
34
  ...getTintColor(theme),
59
35
  };
60
36
  };
@@ -158,10 +134,4 @@ const getOptions = (params: NavigationOptions, theme: Theme) => {
158
134
  };
159
135
  };
160
136
 
161
- export {
162
- requestApi,
163
- getStackOptions,
164
- getDialogOptions,
165
- getModalOptions,
166
- getOptions,
167
- };
137
+ export {getStackOptions, getDialogOptions, getModalOptions, getOptions};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@momo-kits/foundation",
3
- "version": "0.75.1-beta.6",
3
+ "version": "0.75.1-beta.7",
4
4
  "description": "React Native Component Kits",
5
5
  "main": "index.ts",
6
6
  "scripts": {