@momo-kits/foundation 0.92.1-rc.10 → 0.92.1-rc.12

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.
@@ -0,0 +1,479 @@
1
+ import {BottomTabBarProps} from '@react-navigation/bottom-tabs';
2
+ import {LabelPosition} from '@react-navigation/bottom-tabs/src/types';
3
+ import BottomTabBarHeightCallbackContext from '@react-navigation/bottom-tabs/src/utils/BottomTabBarHeightCallbackContext';
4
+ import BottomTabBarItem from '@react-navigation/bottom-tabs/src/views/BottomTabItem';
5
+ import {
6
+ CommonActions,
7
+ NavigationContext,
8
+ NavigationRouteContext,
9
+ ParamListBase,
10
+ TabNavigationState,
11
+ useLinkBuilder,
12
+ useTheme,
13
+ } from '@react-navigation/native';
14
+ import React, {useCallback} from 'react';
15
+ import {
16
+ Animated,
17
+ LayoutChangeEvent,
18
+ Platform,
19
+ StyleProp,
20
+ StyleSheet,
21
+ TouchableOpacity,
22
+ useWindowDimensions,
23
+ View,
24
+ ViewStyle,
25
+ } from 'react-native';
26
+ import {EdgeInsets, useSafeAreaInsets} from 'react-native-safe-area-context';
27
+ import {Radius} from '../../Consts';
28
+ import {Icon} from '../../Icon';
29
+ import {Text} from '../../Text';
30
+ import {FloatingButtonProps} from '../types';
31
+ import {getAccessibilityID} from '../utils';
32
+
33
+ type Props = BottomTabBarProps & {
34
+ activeTintColor?: string;
35
+ inactiveTintColor?: string;
36
+ floatingButton?: FloatingButtonProps;
37
+ };
38
+
39
+ type Options = {
40
+ state: TabNavigationState<ParamListBase>;
41
+ layout: {height: number; width: number};
42
+ dimensions: {height: number; width: number};
43
+ tabStyle: StyleProp<ViewStyle>;
44
+ labelPosition: LabelPosition | undefined;
45
+ adaptive: boolean | undefined;
46
+ };
47
+
48
+ const DEFAULT_MAX_TAB_ITEM_WIDTH = 125;
49
+ const DEFAULT_TABBAR_HEIGHT = 49;
50
+ const COMPACT_TABBAR_HEIGHT = 32;
51
+
52
+ const shouldUseHorizontalLabels = ({
53
+ state,
54
+ layout,
55
+ dimensions,
56
+ adaptive = true,
57
+ labelPosition,
58
+ tabStyle,
59
+ }: Options) => {
60
+ if (labelPosition) {
61
+ return labelPosition === 'beside-icon';
62
+ }
63
+
64
+ if (!adaptive) {
65
+ return false;
66
+ }
67
+
68
+ if (layout.width >= 768) {
69
+ // Screen size matches a tablet
70
+ let maxTabItemWidth = DEFAULT_MAX_TAB_ITEM_WIDTH;
71
+
72
+ const flattenedStyle = StyleSheet.flatten(tabStyle);
73
+
74
+ if (flattenedStyle) {
75
+ if (typeof flattenedStyle.width === 'number') {
76
+ maxTabItemWidth = flattenedStyle.width;
77
+ } else if (typeof flattenedStyle.maxWidth === 'number') {
78
+ maxTabItemWidth = flattenedStyle.maxWidth;
79
+ }
80
+ }
81
+
82
+ return state.routes.length * maxTabItemWidth <= layout.width;
83
+ } else {
84
+ return dimensions.width > dimensions.height;
85
+ }
86
+ };
87
+ const getPaddingBottom = (insets: EdgeInsets) =>
88
+ Math.max(insets.bottom - Platform.select({ios: 4, default: 0}), 0);
89
+
90
+ export const getTabBarHeight = ({
91
+ dimensions,
92
+ insets,
93
+ style,
94
+ ...rest
95
+ }: Options & {
96
+ insets: EdgeInsets;
97
+ style: Animated.WithAnimatedValue<StyleProp<ViewStyle>>;
98
+ }) => {
99
+ // @ts-ignore
100
+ const customHeight = StyleSheet.flatten(style)?.height;
101
+
102
+ if (typeof customHeight === 'number') {
103
+ return customHeight;
104
+ }
105
+
106
+ const isLandscape = dimensions.width > dimensions.height;
107
+ const horizontalLabels = shouldUseHorizontalLabels({dimensions, ...rest});
108
+ const paddingBottom = getPaddingBottom(insets);
109
+
110
+ if (
111
+ Platform.OS === 'ios' &&
112
+ !Platform.isPad &&
113
+ isLandscape &&
114
+ horizontalLabels
115
+ ) {
116
+ return COMPACT_TABBAR_HEIGHT + paddingBottom;
117
+ }
118
+
119
+ return DEFAULT_TABBAR_HEIGHT + paddingBottom;
120
+ };
121
+
122
+ export default function BottomTabBar({
123
+ state,
124
+ navigation,
125
+ descriptors,
126
+ activeBackgroundColor,
127
+ activeTintColor,
128
+ adaptive,
129
+ allowFontScaling,
130
+ inactiveBackgroundColor,
131
+ inactiveTintColor,
132
+ keyboardHidesTabBar = false,
133
+ labelPosition,
134
+ labelStyle,
135
+ iconStyle,
136
+ safeAreaInsets,
137
+ showLabel,
138
+ style,
139
+ tabStyle,
140
+ floatingButton,
141
+ }: Props) {
142
+ const {routes} = state;
143
+ const dimensions = useWindowDimensions();
144
+ const isFloating = floatingButton?.icon && state.routes.length == 4;
145
+ const itemWidth = isFloating
146
+ ? dimensions.width / 5
147
+ : dimensions.width / state.routes.length;
148
+
149
+ const [layout, setLayout] = React.useState({
150
+ height: 0,
151
+ width: dimensions.width,
152
+ });
153
+ const buildLink = useLinkBuilder();
154
+ const {colors} = useTheme();
155
+
156
+ const hasHorizontalLabels = shouldUseHorizontalLabels({
157
+ state,
158
+ dimensions,
159
+ layout,
160
+ adaptive,
161
+ labelPosition,
162
+ tabStyle,
163
+ });
164
+ const focusedRoute = state.routes[state.index] as any;
165
+ const focusedDescriptor = descriptors[focusedRoute?.key] as any;
166
+ const focusedOptions = focusedDescriptor?.options;
167
+ const shouldShowTabBar =
168
+ focusedOptions?.tabBarVisible !== false && !keyboardHidesTabBar;
169
+ const [visible] = React.useState(
170
+ () => new Animated.Value(shouldShowTabBar ? 1 : 0),
171
+ );
172
+
173
+ const indicatorAnimated = React.useRef(new Animated.Value(0)).current;
174
+
175
+ const visibilityAnimationConfigRef = React.useRef(
176
+ focusedOptions?.tabBarVisibilityAnimationConfig,
177
+ );
178
+ const onHeightChange = React.useContext(BottomTabBarHeightCallbackContext);
179
+ const [isTabBarHidden, setIsTabBarHidden] = React.useState(!shouldShowTabBar);
180
+ React.useEffect(() => {
181
+ visibilityAnimationConfigRef.current =
182
+ focusedOptions?.tabBarVisibilityAnimationConfig;
183
+ });
184
+
185
+ const handleLayout = (e: LayoutChangeEvent) => {
186
+ const {height, width} = e.nativeEvent.layout;
187
+
188
+ onHeightChange?.(height);
189
+
190
+ setLayout(layout => {
191
+ if (height === layout.height && width === layout.width) {
192
+ return layout;
193
+ } else {
194
+ return {
195
+ height,
196
+ width,
197
+ };
198
+ }
199
+ });
200
+ };
201
+
202
+ React.useEffect(() => {
203
+ const visibilityAnimationConfig = visibilityAnimationConfigRef.current;
204
+
205
+ if (shouldShowTabBar) {
206
+ const animation =
207
+ visibilityAnimationConfig?.show?.animation === 'spring'
208
+ ? Animated.spring
209
+ : Animated.timing;
210
+
211
+ animation(visible, {
212
+ toValue: 1,
213
+ useNativeDriver: true,
214
+ duration: 250,
215
+ ...visibilityAnimationConfig?.show?.config,
216
+ }).start(({finished}) => {
217
+ if (finished) {
218
+ setIsTabBarHidden(false);
219
+ }
220
+ });
221
+ } else {
222
+ setIsTabBarHidden(true);
223
+
224
+ const animation =
225
+ visibilityAnimationConfig?.hide?.animation === 'spring'
226
+ ? Animated.spring
227
+ : Animated.timing;
228
+
229
+ animation(visible, {
230
+ toValue: 0,
231
+ useNativeDriver: true,
232
+ duration: 200,
233
+ ...visibilityAnimationConfig?.hide?.config,
234
+ }).start();
235
+ }
236
+ }, [visible, shouldShowTabBar]);
237
+ const defaultInsets = useSafeAreaInsets();
238
+
239
+ const insets = {
240
+ top: safeAreaInsets?.top ?? defaultInsets.top,
241
+ right: safeAreaInsets?.right ?? defaultInsets.right,
242
+ bottom: safeAreaInsets?.bottom ?? defaultInsets.bottom,
243
+ left: safeAreaInsets?.left ?? defaultInsets.left,
244
+ };
245
+ const paddingBottom = getPaddingBottom(insets);
246
+
247
+ const leftArr = routes.slice(0, 2);
248
+ const rightArr = routes.slice(2, routes.length);
249
+ const focusedTab = state?.routes[state.index]?.name || routes[0]?.params;
250
+
251
+ const renderTabItem = useCallback(
252
+ (data: any[]) => {
253
+ return data?.map((route, index) => {
254
+ const focused = route.params.name === focusedTab;
255
+ const {options} = descriptors[route?.key] as any;
256
+ const onPress = () => {
257
+ const event = navigation.emit({
258
+ type: 'tabPress',
259
+ target: route.key,
260
+ canPreventDefault: true,
261
+ });
262
+
263
+ if (!focused && !event.defaultPrevented) {
264
+ navigation.dispatch({
265
+ ...CommonActions.navigate(route.name),
266
+ target: state.key,
267
+ });
268
+ }
269
+ };
270
+
271
+ const onLongPress = () => {
272
+ navigation.emit({
273
+ type: 'tabLongPress',
274
+ target: route.key,
275
+ });
276
+ };
277
+
278
+ const label =
279
+ options.tabBarLabel !== undefined
280
+ ? options.tabBarLabel
281
+ : options.title !== undefined
282
+ ? options.title
283
+ : route.name;
284
+
285
+ const accessibilityLabel =
286
+ options.tabBarAccessibilityLabel !== undefined
287
+ ? options.tabBarAccessibilityLabel
288
+ : typeof label === 'string'
289
+ ? `${label}, tab, ${index + 1} of ${routes.length}`
290
+ : undefined;
291
+
292
+ return (
293
+ <NavigationContext.Provider
294
+ key={route.key}
295
+ value={(descriptors as any)[route.key].navigation}>
296
+ <NavigationRouteContext.Provider value={route}>
297
+ <BottomTabBarItem
298
+ route={route}
299
+ focused={focused}
300
+ horizontal={hasHorizontalLabels}
301
+ onPress={onPress}
302
+ onLongPress={onLongPress}
303
+ accessibilityLabel={accessibilityLabel}
304
+ to={buildLink(route.name, route.params)}
305
+ testID={options.tabBarTestID}
306
+ allowFontScaling={allowFontScaling}
307
+ activeTintColor={activeTintColor}
308
+ inactiveTintColor={inactiveTintColor}
309
+ activeBackgroundColor={activeBackgroundColor}
310
+ inactiveBackgroundColor={inactiveBackgroundColor}
311
+ button={options.tabBarButton}
312
+ icon={options.tabBarIcon}
313
+ badge={options.tabBarBadge}
314
+ badgeStyle={options.tabBarBadgeStyle}
315
+ label={label}
316
+ showLabel={showLabel}
317
+ labelStyle={labelStyle}
318
+ iconStyle={iconStyle}
319
+ style={tabStyle}
320
+ {...getAccessibilityID(`btn_tabbar_${route.name}`)}
321
+ />
322
+ </NavigationRouteContext.Provider>
323
+ </NavigationContext.Provider>
324
+ );
325
+ });
326
+ },
327
+ [JSON.stringify(routes), state, descriptors, focusedTab],
328
+ );
329
+
330
+ const renderCenterComponent = useCallback(() => {
331
+ const renderFloatingButton = () => {
332
+ if (typeof floatingButton?.icon == 'string') {
333
+ return <Icon source={floatingButton?.icon} size={32} color={'white'} />;
334
+ } else if (floatingButton?.icon) {
335
+ return floatingButton?.icon;
336
+ } else {
337
+ return null;
338
+ }
339
+ };
340
+ if (floatingButton?.icon && state.routes.length !== 4) {
341
+ console.warn(
342
+ "Can't render floating button with icon when routes length is not 4",
343
+ );
344
+ return null;
345
+ }
346
+ return (
347
+ <View style={{flex: 1}}>
348
+ <TouchableOpacity
349
+ onPress={floatingButton?.onPress}
350
+ style={styles.floatingContainer}>
351
+ <View
352
+ style={{
353
+ ...styles.floatingContent,
354
+ backgroundColor: activeTintColor ?? colors.primary,
355
+ }}>
356
+ {renderFloatingButton()}
357
+ </View>
358
+ <View
359
+ style={{
360
+ top: -6,
361
+ backgroundColor: activeTintColor ?? colors.primary,
362
+ borderRadius: Radius.XL,
363
+ paddingHorizontal: 6,
364
+ }}>
365
+ <Text
366
+ numberOfLines={1}
367
+ typography={'description_xs_regular'}
368
+ color={'white'}>
369
+ {floatingButton?.label}
370
+ </Text>
371
+ </View>
372
+ </TouchableOpacity>
373
+ </View>
374
+ );
375
+ }, [floatingButton]);
376
+
377
+ React.useEffect(() => {
378
+ Animated.timing(indicatorAnimated, {
379
+ toValue:
380
+ isFloating && state.index > 1
381
+ ? itemWidth * (state.index + 1)
382
+ : itemWidth * state.index,
383
+ useNativeDriver: false,
384
+ duration: 200,
385
+ }).start();
386
+ }, [state.index, itemWidth, indicatorAnimated, isFloating]);
387
+
388
+ return (
389
+ <Animated.View
390
+ style={[
391
+ styles.tabBar,
392
+ {
393
+ backgroundColor: colors.card,
394
+ borderTopColor: colors.border,
395
+ },
396
+ {
397
+ transform: [
398
+ {
399
+ translateY: visible.interpolate({
400
+ inputRange: [0, 1],
401
+ outputRange: [
402
+ layout.height + paddingBottom + StyleSheet.hairlineWidth,
403
+ 0,
404
+ ],
405
+ }),
406
+ },
407
+ ],
408
+ // Absolutely position the tab bar so that the content is below it
409
+ // This is needed to avoid gap at bottom when the tab bar is hidden
410
+ position: isTabBarHidden ? 'absolute' : (null as any),
411
+ },
412
+ {
413
+ paddingBottom: paddingBottom - 0,
414
+ paddingHorizontal: Math.max(insets.left, insets.right),
415
+ },
416
+ style,
417
+ ]}
418
+ pointerEvents={isTabBarHidden ? 'none' : 'auto'}
419
+ onLayout={handleLayout}>
420
+ <View style={styles.content}>
421
+ {isFloating && floatingButton?.container}
422
+ <Animated.View
423
+ style={{
424
+ width: itemWidth,
425
+ position: 'absolute',
426
+ top: 0,
427
+ transform: [{translateX: indicatorAnimated}],
428
+ }}>
429
+ <View
430
+ style={[
431
+ styles.indicator,
432
+ {backgroundColor: activeTintColor ?? colors.primary},
433
+ ]}
434
+ />
435
+ </Animated.View>
436
+ {isFloating ? renderTabItem(leftArr) : renderTabItem(routes)}
437
+ {isFloating && renderCenterComponent()}
438
+ {isFloating && renderTabItem(rightArr)}
439
+ </View>
440
+ </Animated.View>
441
+ );
442
+ }
443
+
444
+ const styles = StyleSheet.create({
445
+ content: {
446
+ flex: 1,
447
+ flexDirection: 'row',
448
+ },
449
+ tabBar: {
450
+ left: 0,
451
+ right: 0,
452
+ bottom: 0,
453
+ borderTopWidth: StyleSheet.hairlineWidth,
454
+ elevation: 8,
455
+ },
456
+ floatingContainer: {
457
+ position: 'absolute',
458
+ alignItems: 'center',
459
+ left: 0,
460
+ right: 0,
461
+ bottom: Platform.OS === 'ios' ? -6 : 0,
462
+ },
463
+ floatingContent: {
464
+ alignItems: 'center',
465
+ justifyContent: 'center',
466
+ height: 48,
467
+ width: 48,
468
+
469
+ borderRadius: 100,
470
+ top: -12,
471
+ },
472
+ indicator: {
473
+ height: 2,
474
+ width: 44,
475
+ alignSelf: 'center',
476
+ borderBottomLeftRadius: 1,
477
+ borderBottomRightRadius: 1,
478
+ },
479
+ });
@@ -1,15 +1,16 @@
1
- import React, {useContext, useEffect} from 'react';
2
- import {Platform} from 'react-native';
3
1
  import {createBottomTabNavigator} from '@react-navigation/bottom-tabs';
2
+ import {NavigationContainer} from '@react-navigation/native';
4
3
  import {createStackNavigator} from '@react-navigation/stack';
4
+ import React, {useContext, useEffect} from 'react';
5
+ import {Platform} from 'react-native';
5
6
  import {useSafeAreaInsets} from 'react-native-safe-area-context';
6
- import {NavigationContainer} from '@react-navigation/native';
7
- import {BottomTabProps, NavigationScreenProps} from '../types';
7
+ import {Icon} from '../../Icon';
8
+ import {Text} from '../../Text';
9
+ import {ApplicationContext} from '../index';
8
10
  import StackScreen from '../StackScreen';
11
+ import {BottomTabProps, NavigationScreenProps} from '../types';
9
12
  import {getAccessibilityID, getStackOptions} from '../utils';
10
- import {ApplicationContext} from '../index';
11
- import {Text} from '../../Text';
12
- import {Icon} from '../../Icon';
13
+ import BottomTabBar from './BottomTabBar';
13
14
 
14
15
  const Tab = createBottomTabNavigator();
15
16
  const Stack = createStackNavigator();
@@ -47,6 +48,7 @@ const BottomTab: React.FC<BottomTabProps> = ({
47
48
  listeners,
48
49
  navigation,
49
50
  initialRouteName,
51
+ floatingButton,
50
52
  }) => {
51
53
  const {theme} = useContext(ApplicationContext);
52
54
  const insets = useSafeAreaInsets();
@@ -61,9 +63,15 @@ const BottomTab: React.FC<BottomTabProps> = ({
61
63
 
62
64
  return (
63
65
  <Tab.Navigator
64
- {...getAccessibilityID('tbr_main_bottom')}
65
66
  initialRouteName={initialRouteName}
66
67
  backBehavior={'firstRoute'}
68
+ tabBar={props => (
69
+ <BottomTabBar
70
+ {...getAccessibilityID('tbr_main_bottom')}
71
+ {...props}
72
+ floatingButton={floatingButton}
73
+ />
74
+ )}
67
75
  tabBarOptions={{
68
76
  style: {
69
77
  height: 68 + Math.min(insets.bottom, 21),
@@ -76,7 +84,6 @@ const BottomTab: React.FC<BottomTabProps> = ({
76
84
  {tabs.map(item => {
77
85
  return (
78
86
  <Tab.Screen
79
- {...getAccessibilityID(`btn_tabbar_${item.name}`)}
80
87
  key={item.name}
81
88
  name={item.name}
82
89
  component={TabScreen}
@@ -214,6 +214,7 @@ export type BottomTabProps = {
214
214
  };
215
215
 
216
216
  export type FloatingButtonProps = {
217
+ container?: React.ReactNode;
217
218
  icon: string | React.ReactNode;
218
219
  label: string;
219
220
  onPress: () => void;
package/Input/Input.tsx CHANGED
@@ -133,7 +133,9 @@ const Input = forwardRef(
133
133
  : getAccessibilityID(
134
134
  `${appId}/${code}/${
135
135
  screenInfo.screenName
136
- }/input_${cleanNameForTracking(floatingValue ?? placeholder)}`,
136
+ }/input_${cleanNameForTracking(
137
+ floatingValue ?? placeholder ?? value,
138
+ )}`,
137
139
  );
138
140
 
139
141
  if (disabled) {
package/Radio/index.tsx CHANGED
@@ -54,7 +54,7 @@ const Radio: FC<RadioProps> = ({
54
54
  const accessibilityID = getAccessibilityID?.(
55
55
  `${appId}/${code}/${screenInfo.screenName}/rdo_${cleanNameForTracking(
56
56
  label,
57
- )}_${value}`,
57
+ )}_${selected}`,
58
58
  );
59
59
 
60
60
  return (
package/Title/index.tsx CHANGED
@@ -1,4 +1,4 @@
1
- import React, {FC, useContext} from 'react';
1
+ import React, {FC, useContext, useState} from 'react';
2
2
  import {Text as RNText, TouchableOpacity, View, ViewStyle} from 'react-native';
3
3
  import styles from './styles';
4
4
  import {Icon} from '../Icon';
@@ -24,7 +24,8 @@ const Title: FC<TitleProps> = ({
24
24
  buttonSize = 'small',
25
25
  }) => {
26
26
  const {theme} = useContext(ApplicationContext);
27
-
27
+ const [badgeWidth, setBadgeWidth] = useState(0);
28
+ const [contentWidth, setContentWidth] = useState(0);
28
29
  const styleSheet: {
29
30
  [key: string]: any;
30
31
  } = styles;
@@ -33,6 +34,7 @@ const Title: FC<TitleProps> = ({
33
34
  const numberOfLines = showLeftAction || !!badgeLabel ? 1 : 2;
34
35
  const buttonTypo: Typography =
35
36
  buttonSize === 'small' ? 'action_xs_bold' : 'action_s_bold';
37
+ const flexStyle = showLeftAction && {maxWidth: '70%'};
36
38
 
37
39
  const renderIcon = () => {
38
40
  if (!icon) return null;
@@ -59,16 +61,31 @@ const Title: FC<TitleProps> = ({
59
61
  const renderContent = () => {
60
62
  return (
61
63
  <View style={styles.contentView}>
62
- <View style={[styles.iconLeftView]}>
64
+ <View
65
+ onLayout={e => {
66
+ setContentWidth(e.nativeEvent.layout.width);
67
+ }}
68
+ style={[styles.iconLeftView, flexStyle]}>
63
69
  <RNText
64
70
  numberOfLines={numberOfLines}
65
- style={[styleSheet[typography], styles.title]}>
71
+ style={[
72
+ styleSheet[typography],
73
+ styles.title,
74
+ {maxWidth: contentWidth - badgeWidth},
75
+ ]}>
66
76
  {title}
67
77
  </RNText>
68
-
69
- <View style={{alignItems: 'center'}}>
70
- {badgeLabel && <Badge style={styles.badge} label={badgeLabel} />}
71
- </View>
78
+ {badgeLabel && (
79
+ <View
80
+ onLayout={e => {
81
+ setBadgeWidth(e.nativeEvent.layout.width);
82
+ }}
83
+ style={{
84
+ alignItems: 'center',
85
+ }}>
86
+ <Badge style={styles.badge} label={badgeLabel} />
87
+ </View>
88
+ )}
72
89
  {renderActionLeft()}
73
90
  </View>
74
91
  <Text
package/Title/styles.ts CHANGED
@@ -70,7 +70,6 @@ export default StyleSheet.create({
70
70
  title: {
71
71
  fontWeight: 'bold',
72
72
  marginBottom: Spacing.XS,
73
- flex: 1,
74
73
  },
75
74
  badge: {
76
75
  marginLeft: Spacing.XS,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@momo-kits/foundation",
3
- "version": "0.92.1-rc.10",
3
+ "version": "0.92.1-rc.12",
4
4
  "description": "React Native Component Kits",
5
5
  "main": "index.ts",
6
6
  "scripts": {