@hero-design/rn 7.1.3-alpha3 → 7.1.3-alpha6
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.
- package/es/index.js +328 -128
- package/lib/index.js +328 -126
- package/package.json +1 -1
- package/playground/components/BottomNavigation.tsx +8 -8
- package/playground/components/Button.tsx +67 -0
- package/playground/components/FAB.tsx +4 -1
- package/playground/components/Tabs.tsx +45 -0
- package/playground/index.tsx +10 -4
- package/src/components/Badge/StyledBadge.tsx +3 -5
- package/src/components/Badge/__tests__/__snapshots__/Badge.spec.tsx.snap +52 -20
- package/src/components/Badge/index.tsx +3 -1
- package/src/components/BottomNavigation/StyledBottomNavigation.tsx +7 -0
- package/src/components/BottomNavigation/__tests__/BottomNavigation.spec.tsx +1 -1
- package/src/components/BottomNavigation/index.tsx +14 -18
- package/src/components/Button/IconButton.tsx +62 -0
- package/src/components/Button/__tests__/__snapshots__/index.spec.tsx.snap +43 -0
- package/src/components/Button/__tests__/index.spec.tsx +29 -0
- package/src/components/Button/index.tsx +5 -0
- package/src/components/FAB/ActionGroup/StyledActionGroup.tsx +1 -1
- package/src/components/FAB/ActionGroup/__tests__/__snapshots__/index.spec.tsx.snap +1 -1
- package/src/components/Tabs/StyledTabs.tsx +72 -0
- package/src/components/Tabs/__tests__/Tabs.spec.tsx +79 -0
- package/src/components/Tabs/__tests__/__snapshots__/Tabs.spec.tsx.snap +295 -0
- package/src/components/Tabs/index.tsx +177 -0
- package/src/index.ts +4 -0
- package/src/theme/__tests__/__snapshots__/index.spec.ts.snap +16 -6
- package/src/theme/components/badge.ts +1 -9
- package/src/theme/components/bottomNavigation.ts +5 -1
- package/src/theme/components/fab.ts +2 -2
- package/src/theme/components/tabs.ts +20 -0
- package/src/theme/index.ts +3 -0
- package/src/utils/__tests__/scale.spec.ts +3 -3
- package/src/utils/scale.ts +4 -1
- package/types/playground/components/BottomNavigation.d.ts +2 -2
- package/types/playground/components/Button.d.ts +2 -0
- package/types/playground/components/Tabs.d.ts +2 -0
- package/types/src/components/Badge/StyledBadge.d.ts +2 -2
- package/types/src/components/BottomNavigation/StyledBottomNavigation.d.ts +2 -1
- package/types/src/components/BottomNavigation/index.d.ts +10 -10
- package/types/src/components/Button/IconButton.d.ts +34 -0
- package/types/src/components/Button/__tests__/index.spec.d.ts +1 -0
- package/types/src/components/Button/index.d.ts +4 -0
- package/types/src/components/Tabs/StyledTabs.d.ts +23 -0
- package/types/src/components/Tabs/__tests__/Tabs.spec.d.ts +1 -0
- package/types/src/components/Tabs/index.d.ts +40 -0
- package/types/src/index.d.ts +3 -1
- package/types/src/theme/components/badge.d.ts +0 -6
- package/types/src/theme/components/bottomNavigation.d.ts +3 -0
- package/types/src/theme/components/tabs.d.ts +15 -0
- package/types/src/theme/index.d.ts +2 -0
- package/types/src/utils/scale.d.ts +2 -1
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import { View, FlatList } from 'react-native';
|
|
2
|
+
import styled from 'styled-components-native';
|
|
3
|
+
import Icon from '../Icon';
|
|
4
|
+
import Typography from '../Typography';
|
|
5
|
+
|
|
6
|
+
const TabScreen = styled(View)<{ themeVisibility?: boolean }>`
|
|
7
|
+
flex: 1;
|
|
8
|
+
display: ${({ themeVisibility }) =>
|
|
9
|
+
themeVisibility === false ? 'none' : 'flex'};
|
|
10
|
+
`;
|
|
11
|
+
|
|
12
|
+
const TabContainer = styled(View)`
|
|
13
|
+
flex: 1;
|
|
14
|
+
overflow: hidden;
|
|
15
|
+
`;
|
|
16
|
+
|
|
17
|
+
const ContentWrapper = styled(View)`
|
|
18
|
+
flex: 1;
|
|
19
|
+
`;
|
|
20
|
+
|
|
21
|
+
const HeaderTabWrapper = styled(View)<{
|
|
22
|
+
themeInsets: { top: number; right: number; bottom: number; left: number };
|
|
23
|
+
}>`
|
|
24
|
+
padding-left: ${({ themeInsets }) =>
|
|
25
|
+
Math.max(themeInsets.left, themeInsets.right)}px;
|
|
26
|
+
padding-right: ${({ themeInsets }) =>
|
|
27
|
+
Math.max(themeInsets.left, themeInsets.right)}px;
|
|
28
|
+
background-color: ${({ theme }) => theme.__hd__.tabs.colors.background};
|
|
29
|
+
`;
|
|
30
|
+
|
|
31
|
+
const StyledFlatList = (styled(FlatList)`
|
|
32
|
+
padding-left: ${({ theme }) =>
|
|
33
|
+
theme.__hd__.tabs.space.flatListHorizontalPadding};
|
|
34
|
+
padding-right: ${({ theme }) =>
|
|
35
|
+
theme.__hd__.tabs.space.flatListHorizontalPadding};
|
|
36
|
+
` as React.ComponentType) as new <T>() => FlatList<T>;
|
|
37
|
+
|
|
38
|
+
const HeaderTabItem = styled(View)`
|
|
39
|
+
flex-direction: row;
|
|
40
|
+
align-items: center;
|
|
41
|
+
padding-left: ${({ theme }) => theme.__hd__.tabs.space.itemHorizontalPadding};
|
|
42
|
+
padding-right: ${({ theme }) =>
|
|
43
|
+
theme.__hd__.tabs.space.itemHorizontalPadding};
|
|
44
|
+
padding-top: ${({ theme }) => theme.__hd__.tabs.space.itemVerticalPadding};
|
|
45
|
+
padding-bottom: ${({ theme }) => theme.__hd__.tabs.space.itemVerticalPadding};
|
|
46
|
+
`;
|
|
47
|
+
|
|
48
|
+
const StyledHeaderTabIcon = styled(Icon)<{ themeActive: boolean }>`
|
|
49
|
+
color: ${({ theme, themeActive }) =>
|
|
50
|
+
themeActive
|
|
51
|
+
? theme.__hd__.tabs.colors.active
|
|
52
|
+
: theme.__hd__.tabs.colors.inactive};
|
|
53
|
+
`;
|
|
54
|
+
|
|
55
|
+
const StyledHeaderTabText = styled(Typography.Text)<{ themeActive: boolean }>`
|
|
56
|
+
margin-left: ${({ theme }) => theme.__hd__.tabs.space.titleMarginLeft};
|
|
57
|
+
color: ${({ theme, themeActive }) =>
|
|
58
|
+
themeActive
|
|
59
|
+
? theme.__hd__.tabs.colors.active
|
|
60
|
+
: theme.__hd__.tabs.colors.inactive};
|
|
61
|
+
`;
|
|
62
|
+
|
|
63
|
+
export {
|
|
64
|
+
StyledFlatList,
|
|
65
|
+
HeaderTabItem,
|
|
66
|
+
TabScreen,
|
|
67
|
+
TabContainer,
|
|
68
|
+
HeaderTabWrapper,
|
|
69
|
+
ContentWrapper,
|
|
70
|
+
StyledHeaderTabIcon,
|
|
71
|
+
StyledHeaderTabText,
|
|
72
|
+
};
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import React, { ComponentProps } from 'react';
|
|
2
|
+
import { Text } from 'react-native';
|
|
3
|
+
import { fireEvent } from '@testing-library/react-native';
|
|
4
|
+
import { SafeAreaProvider } from 'react-native-safe-area-context';
|
|
5
|
+
import renderWithTheme from '../../../testHelpers/renderWithTheme';
|
|
6
|
+
import Tabs, { Tab } from '..';
|
|
7
|
+
|
|
8
|
+
const TestTabsComponent = (
|
|
9
|
+
props: Omit<
|
|
10
|
+
ComponentProps<typeof Tabs>,
|
|
11
|
+
'selectedTabKey' | 'onChange' | 'tabs'
|
|
12
|
+
>
|
|
13
|
+
) => {
|
|
14
|
+
const [selectedTabKey, setSelectedTabKey] = React.useState('work');
|
|
15
|
+
const tabs: Tab[] = [
|
|
16
|
+
{
|
|
17
|
+
key: 'work',
|
|
18
|
+
title: 'Work',
|
|
19
|
+
icon: 'suitcase',
|
|
20
|
+
component: <Text>Work Screen</Text>,
|
|
21
|
+
},
|
|
22
|
+
{
|
|
23
|
+
key: 'personal',
|
|
24
|
+
title: 'Personal',
|
|
25
|
+
icon: 'face-smiley',
|
|
26
|
+
component: <Text>Personal Screen</Text>,
|
|
27
|
+
},
|
|
28
|
+
];
|
|
29
|
+
|
|
30
|
+
return (
|
|
31
|
+
<SafeAreaProvider
|
|
32
|
+
initialMetrics={{
|
|
33
|
+
frame: { x: 0, y: 0, width: 0, height: 0 },
|
|
34
|
+
insets: { top: 0, left: 0, right: 0, bottom: 0 },
|
|
35
|
+
}}
|
|
36
|
+
>
|
|
37
|
+
<Tabs
|
|
38
|
+
{...props}
|
|
39
|
+
tabs={tabs}
|
|
40
|
+
selectedTabKey={selectedTabKey}
|
|
41
|
+
onChange={newTabKey => setSelectedTabKey(newTabKey)}
|
|
42
|
+
/>
|
|
43
|
+
</SafeAreaProvider>
|
|
44
|
+
);
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
describe('Tabs', () => {
|
|
48
|
+
it('renders correctly', () => {
|
|
49
|
+
const { getByTestId, getByText, toJSON } = renderWithTheme(
|
|
50
|
+
<TestTabsComponent />
|
|
51
|
+
);
|
|
52
|
+
|
|
53
|
+
expect(toJSON()).toMatchSnapshot();
|
|
54
|
+
expect(getByText('Work')).toBeDefined();
|
|
55
|
+
expect(getByText('Personal')).toBeDefined();
|
|
56
|
+
expect(getByTestId('hero-icon-suitcase')).toBeDefined();
|
|
57
|
+
expect(getByTestId('hero-icon-face-smiley')).toBeDefined();
|
|
58
|
+
fireEvent.press(getByText('Personal'));
|
|
59
|
+
|
|
60
|
+
// All screens are rendered and component is not unmounted when switching screen.
|
|
61
|
+
expect(getByText('Work Screen')).toBeDefined();
|
|
62
|
+
expect(getByText('Personal Screen')).toBeDefined();
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
it('renders correctly with renderActiveTabOnly is true', () => {
|
|
66
|
+
const { getByText, queryByText, getByTestId } = renderWithTheme(
|
|
67
|
+
<TestTabsComponent renderActiveTabOnly />
|
|
68
|
+
);
|
|
69
|
+
|
|
70
|
+
// Can switch tab by clicking icon.
|
|
71
|
+
fireEvent.press(getByTestId('hero-icon-face-smiley'));
|
|
72
|
+
expect(getByText('Personal Screen')).toBeDefined();
|
|
73
|
+
fireEvent.press(getByTestId('hero-icon-suitcase'));
|
|
74
|
+
expect(getByText('Work Screen')).toBeDefined();
|
|
75
|
+
|
|
76
|
+
// Only render selected screen, others are unmounted.
|
|
77
|
+
expect(queryByText('Personal Screen')).toBeNull();
|
|
78
|
+
});
|
|
79
|
+
});
|
|
@@ -0,0 +1,295 @@
|
|
|
1
|
+
// Jest Snapshot v1, https://goo.gl/fbAQLP
|
|
2
|
+
|
|
3
|
+
exports[`Tabs renders correctly 1`] = `
|
|
4
|
+
<RNCSafeAreaProvider
|
|
5
|
+
onInsetsChange={[Function]}
|
|
6
|
+
style={
|
|
7
|
+
Array [
|
|
8
|
+
Object {
|
|
9
|
+
"flex": 1,
|
|
10
|
+
},
|
|
11
|
+
undefined,
|
|
12
|
+
]
|
|
13
|
+
}
|
|
14
|
+
>
|
|
15
|
+
<View
|
|
16
|
+
style={
|
|
17
|
+
Object {
|
|
18
|
+
"flexBasis": 0,
|
|
19
|
+
"flexGrow": 1,
|
|
20
|
+
"flexShrink": 1,
|
|
21
|
+
"overflow": "hidden",
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
>
|
|
25
|
+
<View
|
|
26
|
+
style={
|
|
27
|
+
Object {
|
|
28
|
+
"backgroundColor": "#7622d7",
|
|
29
|
+
"paddingLeft": 0,
|
|
30
|
+
"paddingRight": 0,
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
themeInsets={
|
|
34
|
+
Object {
|
|
35
|
+
"bottom": 0,
|
|
36
|
+
"left": 0,
|
|
37
|
+
"right": 0,
|
|
38
|
+
"top": 0,
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
>
|
|
42
|
+
<RCTScrollView
|
|
43
|
+
data={
|
|
44
|
+
Array [
|
|
45
|
+
Object {
|
|
46
|
+
"component": <Text>
|
|
47
|
+
Work Screen
|
|
48
|
+
</Text>,
|
|
49
|
+
"icon": "suitcase",
|
|
50
|
+
"key": "work",
|
|
51
|
+
"title": "Work",
|
|
52
|
+
},
|
|
53
|
+
Object {
|
|
54
|
+
"component": <Text>
|
|
55
|
+
Personal Screen
|
|
56
|
+
</Text>,
|
|
57
|
+
"icon": "face-smiley",
|
|
58
|
+
"key": "personal",
|
|
59
|
+
"title": "Personal",
|
|
60
|
+
},
|
|
61
|
+
]
|
|
62
|
+
}
|
|
63
|
+
getItem={[Function]}
|
|
64
|
+
getItemCount={[Function]}
|
|
65
|
+
horizontal={true}
|
|
66
|
+
keyExtractor={[Function]}
|
|
67
|
+
onContentSizeChange={[Function]}
|
|
68
|
+
onLayout={[Function]}
|
|
69
|
+
onMomentumScrollBegin={[Function]}
|
|
70
|
+
onMomentumScrollEnd={[Function]}
|
|
71
|
+
onScroll={[Function]}
|
|
72
|
+
onScrollBeginDrag={[Function]}
|
|
73
|
+
onScrollEndDrag={[Function]}
|
|
74
|
+
onScrollToIndexFailed={[Function]}
|
|
75
|
+
removeClippedSubviews={false}
|
|
76
|
+
renderItem={[Function]}
|
|
77
|
+
scrollEventThrottle={50}
|
|
78
|
+
showsHorizontalScrollIndicator={false}
|
|
79
|
+
stickyHeaderIndices={Array []}
|
|
80
|
+
style={
|
|
81
|
+
Object {
|
|
82
|
+
"paddingLeft": 8,
|
|
83
|
+
"paddingRight": 8,
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
viewabilityConfigCallbackPairs={Array []}
|
|
87
|
+
>
|
|
88
|
+
<View>
|
|
89
|
+
<View
|
|
90
|
+
onLayout={[Function]}
|
|
91
|
+
style={
|
|
92
|
+
Array [
|
|
93
|
+
Object {
|
|
94
|
+
"flexDirection": "row",
|
|
95
|
+
},
|
|
96
|
+
null,
|
|
97
|
+
]
|
|
98
|
+
}
|
|
99
|
+
>
|
|
100
|
+
<View
|
|
101
|
+
accessible={true}
|
|
102
|
+
collapsable={false}
|
|
103
|
+
focusable={true}
|
|
104
|
+
nativeID="animatedComponent"
|
|
105
|
+
onClick={[Function]}
|
|
106
|
+
onResponderGrant={[Function]}
|
|
107
|
+
onResponderMove={[Function]}
|
|
108
|
+
onResponderRelease={[Function]}
|
|
109
|
+
onResponderTerminate={[Function]}
|
|
110
|
+
onResponderTerminationRequest={[Function]}
|
|
111
|
+
onStartShouldSetResponder={[Function]}
|
|
112
|
+
style={
|
|
113
|
+
Object {
|
|
114
|
+
"opacity": 1,
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
>
|
|
118
|
+
<View
|
|
119
|
+
style={
|
|
120
|
+
Object {
|
|
121
|
+
"alignItems": "center",
|
|
122
|
+
"flexDirection": "row",
|
|
123
|
+
"paddingBottom": 24,
|
|
124
|
+
"paddingLeft": 16,
|
|
125
|
+
"paddingRight": 16,
|
|
126
|
+
"paddingTop": 24,
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
>
|
|
130
|
+
<HeroIcon
|
|
131
|
+
name="suitcase"
|
|
132
|
+
style={
|
|
133
|
+
Array [
|
|
134
|
+
Object {
|
|
135
|
+
"color": "#292a2b",
|
|
136
|
+
"fontSize": 24,
|
|
137
|
+
},
|
|
138
|
+
Object {
|
|
139
|
+
"color": "#ffffff",
|
|
140
|
+
},
|
|
141
|
+
]
|
|
142
|
+
}
|
|
143
|
+
testID="hero-icon-suitcase"
|
|
144
|
+
themeIntent="text"
|
|
145
|
+
themeSize="medium"
|
|
146
|
+
/>
|
|
147
|
+
<Text
|
|
148
|
+
numberOfLines={1}
|
|
149
|
+
style={
|
|
150
|
+
Array [
|
|
151
|
+
Object {
|
|
152
|
+
"color": "#292a2b",
|
|
153
|
+
"fontFamily": "Be Vietnam Pro SemiBold",
|
|
154
|
+
"fontSize": 14,
|
|
155
|
+
"letterSpacing": 0.42,
|
|
156
|
+
"lineHeight": 22,
|
|
157
|
+
},
|
|
158
|
+
Object {
|
|
159
|
+
"color": "#ffffff",
|
|
160
|
+
"marginLeft": 8,
|
|
161
|
+
},
|
|
162
|
+
]
|
|
163
|
+
}
|
|
164
|
+
themeActive={true}
|
|
165
|
+
themeFontSize="medium"
|
|
166
|
+
themeFontWeight="semi-bold"
|
|
167
|
+
themeIntent="body"
|
|
168
|
+
>
|
|
169
|
+
Work
|
|
170
|
+
</Text>
|
|
171
|
+
</View>
|
|
172
|
+
</View>
|
|
173
|
+
</View>
|
|
174
|
+
<View
|
|
175
|
+
onLayout={[Function]}
|
|
176
|
+
style={
|
|
177
|
+
Array [
|
|
178
|
+
Object {
|
|
179
|
+
"flexDirection": "row",
|
|
180
|
+
},
|
|
181
|
+
null,
|
|
182
|
+
]
|
|
183
|
+
}
|
|
184
|
+
>
|
|
185
|
+
<View
|
|
186
|
+
accessible={true}
|
|
187
|
+
collapsable={false}
|
|
188
|
+
focusable={true}
|
|
189
|
+
nativeID="animatedComponent"
|
|
190
|
+
onClick={[Function]}
|
|
191
|
+
onResponderGrant={[Function]}
|
|
192
|
+
onResponderMove={[Function]}
|
|
193
|
+
onResponderRelease={[Function]}
|
|
194
|
+
onResponderTerminate={[Function]}
|
|
195
|
+
onResponderTerminationRequest={[Function]}
|
|
196
|
+
onStartShouldSetResponder={[Function]}
|
|
197
|
+
style={
|
|
198
|
+
Object {
|
|
199
|
+
"opacity": 1,
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
>
|
|
203
|
+
<View
|
|
204
|
+
style={
|
|
205
|
+
Object {
|
|
206
|
+
"alignItems": "center",
|
|
207
|
+
"flexDirection": "row",
|
|
208
|
+
"paddingBottom": 24,
|
|
209
|
+
"paddingLeft": 16,
|
|
210
|
+
"paddingRight": 16,
|
|
211
|
+
"paddingTop": 24,
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
>
|
|
215
|
+
<HeroIcon
|
|
216
|
+
name="face-smiley"
|
|
217
|
+
style={
|
|
218
|
+
Array [
|
|
219
|
+
Object {
|
|
220
|
+
"color": "#292a2b",
|
|
221
|
+
"fontSize": 24,
|
|
222
|
+
},
|
|
223
|
+
Object {
|
|
224
|
+
"color": "#ffffff50",
|
|
225
|
+
},
|
|
226
|
+
]
|
|
227
|
+
}
|
|
228
|
+
testID="hero-icon-face-smiley"
|
|
229
|
+
themeIntent="text"
|
|
230
|
+
themeSize="medium"
|
|
231
|
+
/>
|
|
232
|
+
<Text
|
|
233
|
+
numberOfLines={1}
|
|
234
|
+
style={
|
|
235
|
+
Array [
|
|
236
|
+
Object {
|
|
237
|
+
"color": "#292a2b",
|
|
238
|
+
"fontFamily": "Be Vietnam Pro SemiBold",
|
|
239
|
+
"fontSize": 14,
|
|
240
|
+
"letterSpacing": 0.42,
|
|
241
|
+
"lineHeight": 22,
|
|
242
|
+
},
|
|
243
|
+
Object {
|
|
244
|
+
"color": "#ffffff50",
|
|
245
|
+
"marginLeft": 8,
|
|
246
|
+
},
|
|
247
|
+
]
|
|
248
|
+
}
|
|
249
|
+
themeActive={false}
|
|
250
|
+
themeFontSize="medium"
|
|
251
|
+
themeFontWeight="semi-bold"
|
|
252
|
+
themeIntent="body"
|
|
253
|
+
>
|
|
254
|
+
Personal
|
|
255
|
+
</Text>
|
|
256
|
+
</View>
|
|
257
|
+
</View>
|
|
258
|
+
</View>
|
|
259
|
+
</View>
|
|
260
|
+
</RCTScrollView>
|
|
261
|
+
</View>
|
|
262
|
+
<View
|
|
263
|
+
style={
|
|
264
|
+
Object {
|
|
265
|
+
"flexBasis": 0,
|
|
266
|
+
"flexGrow": 1,
|
|
267
|
+
"flexShrink": 1,
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
>
|
|
271
|
+
<View
|
|
272
|
+
accessibilityElementsHidden={false}
|
|
273
|
+
collapsable={false}
|
|
274
|
+
importantForAccessibility="auto"
|
|
275
|
+
pointerEvents="auto"
|
|
276
|
+
removeClippedSubviews={false}
|
|
277
|
+
style={
|
|
278
|
+
Object {
|
|
279
|
+
"display": "flex",
|
|
280
|
+
"flexBasis": 0,
|
|
281
|
+
"flexGrow": 1,
|
|
282
|
+
"flexShrink": 1,
|
|
283
|
+
}
|
|
284
|
+
}
|
|
285
|
+
testID="tab-screen-work"
|
|
286
|
+
themeVisibility={true}
|
|
287
|
+
>
|
|
288
|
+
<Text>
|
|
289
|
+
Work Screen
|
|
290
|
+
</Text>
|
|
291
|
+
</View>
|
|
292
|
+
</View>
|
|
293
|
+
</View>
|
|
294
|
+
</RNCSafeAreaProvider>
|
|
295
|
+
`;
|
|
@@ -0,0 +1,177 @@
|
|
|
1
|
+
import React, { ReactNode } from 'react';
|
|
2
|
+
import {
|
|
3
|
+
TouchableOpacity,
|
|
4
|
+
StyleProp,
|
|
5
|
+
ViewStyle,
|
|
6
|
+
ViewProps,
|
|
7
|
+
FlatList,
|
|
8
|
+
} from 'react-native';
|
|
9
|
+
import { useSafeAreaInsets } from 'react-native-safe-area-context';
|
|
10
|
+
import { IconProps } from '../Icon';
|
|
11
|
+
import {
|
|
12
|
+
HeaderTabItem,
|
|
13
|
+
HeaderTabWrapper,
|
|
14
|
+
StyledHeaderTabText,
|
|
15
|
+
StyledHeaderTabIcon,
|
|
16
|
+
TabContainer,
|
|
17
|
+
TabScreen,
|
|
18
|
+
ContentWrapper,
|
|
19
|
+
StyledFlatList,
|
|
20
|
+
} from './StyledTabs';
|
|
21
|
+
import { isIOS } from '../../utils/helpers';
|
|
22
|
+
|
|
23
|
+
type IconType = IconProps['icon'];
|
|
24
|
+
|
|
25
|
+
export type Tab = {
|
|
26
|
+
key: string;
|
|
27
|
+
title: string;
|
|
28
|
+
icon: IconType;
|
|
29
|
+
component: ReactNode;
|
|
30
|
+
testID?: string;
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
interface TabType extends ViewProps {
|
|
34
|
+
/**
|
|
35
|
+
* Callback which is called on tab change, receiving id of upcoming active Tab.
|
|
36
|
+
*/
|
|
37
|
+
onChange: (key: string) => void;
|
|
38
|
+
/**
|
|
39
|
+
* Whether inactive tabs should be removed and unmounted in React.
|
|
40
|
+
* Defaults to `false`.
|
|
41
|
+
*/
|
|
42
|
+
renderActiveTabOnly?: boolean;
|
|
43
|
+
/**
|
|
44
|
+
* Current selected tab key.
|
|
45
|
+
*/
|
|
46
|
+
selectedTabKey: string;
|
|
47
|
+
/**
|
|
48
|
+
* List of Tab to be rendered. Each Tab must have an unique id.
|
|
49
|
+
*/
|
|
50
|
+
tabs: Tab[];
|
|
51
|
+
/**
|
|
52
|
+
* Addditional style.
|
|
53
|
+
*/
|
|
54
|
+
style?: StyleProp<ViewStyle>;
|
|
55
|
+
/**
|
|
56
|
+
* Testing id of the component.
|
|
57
|
+
*/
|
|
58
|
+
testID?: string;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
const Tabs = ({
|
|
62
|
+
onChange,
|
|
63
|
+
renderActiveTabOnly = false,
|
|
64
|
+
selectedTabKey,
|
|
65
|
+
tabs,
|
|
66
|
+
...nativeProps
|
|
67
|
+
}: TabType): JSX.Element => {
|
|
68
|
+
const flatListRef = React.useRef<FlatList>(null);
|
|
69
|
+
const insets = useSafeAreaInsets();
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* List of loaded tabs, tabs will be loaded when navigated to.
|
|
73
|
+
*/
|
|
74
|
+
const [loaded, setLoaded] = React.useState([selectedTabKey]);
|
|
75
|
+
|
|
76
|
+
if (!loaded.includes(selectedTabKey)) {
|
|
77
|
+
// Set the current tab to be loaded if it was not loaded before
|
|
78
|
+
setLoaded(loadedState => [...loadedState, selectedTabKey]);
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
return (
|
|
82
|
+
<TabContainer {...nativeProps}>
|
|
83
|
+
<HeaderTabWrapper themeInsets={insets}>
|
|
84
|
+
<StyledFlatList<Tab>
|
|
85
|
+
ref={flatListRef}
|
|
86
|
+
horizontal
|
|
87
|
+
data={tabs}
|
|
88
|
+
keyExtractor={tab => String(tab.key)}
|
|
89
|
+
showsHorizontalScrollIndicator={false}
|
|
90
|
+
onScrollToIndexFailed={({ index }) => {
|
|
91
|
+
setTimeout(
|
|
92
|
+
() =>
|
|
93
|
+
flatListRef.current?.scrollToIndex({
|
|
94
|
+
index,
|
|
95
|
+
viewPosition: 0.5,
|
|
96
|
+
}),
|
|
97
|
+
100
|
|
98
|
+
);
|
|
99
|
+
}}
|
|
100
|
+
renderItem={({ item: tab, index }) => {
|
|
101
|
+
const { key, icon, title, testID } = tab;
|
|
102
|
+
const active = selectedTabKey === key;
|
|
103
|
+
|
|
104
|
+
return (
|
|
105
|
+
<TouchableOpacity
|
|
106
|
+
key={key}
|
|
107
|
+
onPress={() => {
|
|
108
|
+
if (key !== selectedTabKey) {
|
|
109
|
+
onChange(key);
|
|
110
|
+
flatListRef.current?.scrollToIndex({
|
|
111
|
+
index,
|
|
112
|
+
viewPosition: 0.5,
|
|
113
|
+
});
|
|
114
|
+
}
|
|
115
|
+
}}
|
|
116
|
+
testID={testID}
|
|
117
|
+
>
|
|
118
|
+
<HeaderTabItem>
|
|
119
|
+
<StyledHeaderTabIcon
|
|
120
|
+
icon={icon}
|
|
121
|
+
testID={`hero-icon-${icon}`}
|
|
122
|
+
themeActive={active}
|
|
123
|
+
/>
|
|
124
|
+
<StyledHeaderTabText
|
|
125
|
+
fontWeight="semi-bold"
|
|
126
|
+
numberOfLines={1}
|
|
127
|
+
themeActive={active}
|
|
128
|
+
>
|
|
129
|
+
{title}
|
|
130
|
+
</StyledHeaderTabText>
|
|
131
|
+
</HeaderTabItem>
|
|
132
|
+
</TouchableOpacity>
|
|
133
|
+
);
|
|
134
|
+
}}
|
|
135
|
+
/>
|
|
136
|
+
</HeaderTabWrapper>
|
|
137
|
+
<ContentWrapper>
|
|
138
|
+
{tabs.map(tab => {
|
|
139
|
+
const { key, component } = tab;
|
|
140
|
+
const active = selectedTabKey === key;
|
|
141
|
+
|
|
142
|
+
if (renderActiveTabOnly && !active) {
|
|
143
|
+
return null;
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
if (!loaded.includes(key)) {
|
|
147
|
+
// Don't render a screen if we've never navigated to it
|
|
148
|
+
return null;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
return (
|
|
152
|
+
<TabScreen
|
|
153
|
+
key={key}
|
|
154
|
+
testID={`tab-screen-${selectedTabKey}`}
|
|
155
|
+
pointerEvents={active ? 'auto' : 'none'}
|
|
156
|
+
accessibilityElementsHidden={!active}
|
|
157
|
+
importantForAccessibility={
|
|
158
|
+
active ? 'auto' : 'no-hide-descendants'
|
|
159
|
+
}
|
|
160
|
+
collapsable={false}
|
|
161
|
+
removeClippedSubviews={
|
|
162
|
+
// On iOS, set removeClippedSubviews to true only when not focused
|
|
163
|
+
// This is an workaround for a bug where the clipped view never re-appears.
|
|
164
|
+
isIOS ? selectedTabKey !== key : true
|
|
165
|
+
}
|
|
166
|
+
themeVisibility={active}
|
|
167
|
+
>
|
|
168
|
+
{component}
|
|
169
|
+
</TabScreen>
|
|
170
|
+
);
|
|
171
|
+
})}
|
|
172
|
+
</ContentWrapper>
|
|
173
|
+
</TabContainer>
|
|
174
|
+
);
|
|
175
|
+
};
|
|
176
|
+
|
|
177
|
+
export default Tabs;
|
package/src/index.ts
CHANGED
|
@@ -6,9 +6,11 @@ import Badge from './components/Badge';
|
|
|
6
6
|
import BottomNavigation from './components/BottomNavigation';
|
|
7
7
|
import Card from './components/Card';
|
|
8
8
|
import Divider from './components/Divider';
|
|
9
|
+
import Tabs from './components/Tabs';
|
|
9
10
|
import Icon from './components/Icon';
|
|
10
11
|
import Typography from './components/Typography';
|
|
11
12
|
import FAB from './components/FAB';
|
|
13
|
+
import Button from './components/Button';
|
|
12
14
|
|
|
13
15
|
export {
|
|
14
16
|
theme,
|
|
@@ -21,6 +23,8 @@ export {
|
|
|
21
23
|
Card,
|
|
22
24
|
Divider,
|
|
23
25
|
Icon,
|
|
26
|
+
Tabs,
|
|
24
27
|
Typography,
|
|
25
28
|
FAB,
|
|
29
|
+
Button,
|
|
26
30
|
};
|
|
@@ -17,12 +17,6 @@ Object {
|
|
|
17
17
|
"warning": "#d98a2c",
|
|
18
18
|
"warningBackground": "#fff6eb",
|
|
19
19
|
},
|
|
20
|
-
"fontSizes": Object {
|
|
21
|
-
"default": "12px",
|
|
22
|
-
},
|
|
23
|
-
"fonts": Object {
|
|
24
|
-
"default": "Be Vietnam Pro SemiBold",
|
|
25
|
-
},
|
|
26
20
|
"padding": Object {
|
|
27
21
|
"default": "4px 8px",
|
|
28
22
|
},
|
|
@@ -44,6 +38,9 @@ Object {
|
|
|
44
38
|
"sizes": Object {
|
|
45
39
|
"height": 72,
|
|
46
40
|
},
|
|
41
|
+
"space": Object {
|
|
42
|
+
"titleMarginTop": "4px",
|
|
43
|
+
},
|
|
47
44
|
},
|
|
48
45
|
"card": Object {
|
|
49
46
|
"padding": Object {
|
|
@@ -125,6 +122,19 @@ Object {
|
|
|
125
122
|
"xsmall": 16,
|
|
126
123
|
},
|
|
127
124
|
},
|
|
125
|
+
"tabs": Object {
|
|
126
|
+
"colors": Object {
|
|
127
|
+
"active": "#ffffff",
|
|
128
|
+
"background": "#7622d7",
|
|
129
|
+
"inactive": "#ffffff50",
|
|
130
|
+
},
|
|
131
|
+
"space": Object {
|
|
132
|
+
"flatListHorizontalPadding": "8px",
|
|
133
|
+
"itemHorizontalPadding": "16px",
|
|
134
|
+
"itemVerticalPadding": "24px",
|
|
135
|
+
"titleMarginLeft": "8px",
|
|
136
|
+
},
|
|
137
|
+
},
|
|
128
138
|
"typography": Object {
|
|
129
139
|
"colors": Object {
|
|
130
140
|
"body": "#292a2b",
|
|
@@ -20,19 +20,11 @@ const getBadgeTheme = (theme: GlobalTheme) => {
|
|
|
20
20
|
default: `${theme.space.xsmall}px ${theme.space.small}px`,
|
|
21
21
|
};
|
|
22
22
|
|
|
23
|
-
const fonts = {
|
|
24
|
-
default: theme.fonts.semiBold,
|
|
25
|
-
};
|
|
26
|
-
|
|
27
|
-
const fontSizes = {
|
|
28
|
-
default: `${theme.fontSizes.small}px`,
|
|
29
|
-
};
|
|
30
|
-
|
|
31
23
|
const radii = {
|
|
32
24
|
default: `4px`,
|
|
33
25
|
};
|
|
34
26
|
|
|
35
|
-
return { borderWidths, colors, padding,
|
|
27
|
+
return { borderWidths, colors, padding, radii };
|
|
36
28
|
};
|
|
37
29
|
|
|
38
30
|
export default getBadgeTheme;
|