-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Expand file tree
/
Copy pathComponentScreen.tsx
More file actions
107 lines (99 loc) · 3.65 KB
/
ComponentScreen.tsx
File metadata and controls
107 lines (99 loc) · 3.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
import React, { Component } from 'react';
import { View, Text, TouchableOpacity, Image, ImageURISource } from 'react-native';
import { Navigation, ImageResource } from 'src';
import { ComponentProps } from '../ComponentProps';
import { VISIBLE_SCREEN_TEST_ID } from '../constants';
import { LayoutStore } from '../Stores/LayoutStore';
import { connect } from '../connect';
import { TopBar } from './TopBar';
import { events } from '../Stores/EventsStore';
import _ from 'lodash';
import { switchTabByIndex } from '../actions/layoutActions';
function isURISource(src: ImageResource | undefined): src is ImageURISource {
return !!src && typeof src === 'object' && 'uri' in src;
}
export const ComponentScreen = connect(
class extends Component<ComponentProps> {
constructor(props: ComponentProps) {
super(props);
}
componentDidMount() {
this.props.layoutNode.componentDidMount();
}
isVisible(): boolean {
const isVisible = LayoutStore.isVisibleLayout(this.props.layoutNode);
return isVisible;
}
renderTabBar() {
const bottomTabs = this.props.layoutNode.getBottomTabs();
if (!bottomTabs) return null;
const bottomTabsOptions = bottomTabs.resolveOptions().bottomTabs;
if (bottomTabsOptions?.visible === false) return null;
const buttons = bottomTabs!.children!.map((child, i) => {
const bottomTabOptions = child.resolveOptions().bottomTab;
const icon =
(bottomTabs as any).selectedIndex === i
? bottomTabOptions?.selectedIcon
: bottomTabOptions?.icon;
const iconURI = isURISource(icon) ? icon.uri : undefined;
return (
<View key={`tab-${i}`}>
<TouchableOpacity
style={{ padding: 10 }}
testID={bottomTabOptions?.testID}
onPress={() => {
events.invokeBottomTabPressed({
tabIndex: i,
});
if (_.defaultTo(bottomTabOptions?.selectTabOnPress, true))
switchTabByIndex(this.props.layoutNode.getBottomTabs(), i);
}}
>
<View style={{ justifyContent: 'center', alignItems: 'center' }}>
<Text>{bottomTabOptions?.badge}</Text>
{iconURI && (
<Image
style={{ width: 18, height: 18, marginBottom: 5 }}
source={{ uri: iconURI }}
/>
)}
<Text style={{ fontSize: 12 }}>{bottomTabOptions?.text || ''}</Text>
</View>
</TouchableOpacity>
</View>
);
});
return (
<View
testID={bottomTabsOptions?.testID}
style={{
flexDirection: 'row',
justifyContent: 'center',
width: '100%',
backgroundColor: '#F0F2F5',
}}
>
{buttons}
</View>
);
}
render() {
const Component = Navigation.mock.store.getWrappedComponent(this.props.layoutNode.data.name);
if (!Component)
throw new Error(`${this.props.layoutNode.data.name} has not been registered.`);
return (
<View testID={this.isVisible() ? VISIBLE_SCREEN_TEST_ID : undefined}>
{this.props.layoutNode.getStack() && (
<TopBar
layoutNode={this.props.layoutNode}
topBarOptions={this.props.layoutNode.resolveOptions().topBar}
backButtonOptions={this.props.layoutNode.resolveOptions().topBar?.backButton}
/>
)}
<Component componentId={this.props.layoutNode.nodeId} />
{this.renderTabBar()}
</View>
);
}
}
);