forked from Expensify/App
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
127 lines (114 loc) · 4.71 KB
/
index.js
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
import {View} from 'react-native';
import React from 'react';
import _ from 'underscore';
import {withOnyx} from 'react-native-onyx';
import KeyboardAvoidingView from '../KeyboardAvoidingView';
import CONST from '../../CONST';
import KeyboardShortcut from '../../libs/KeyboardShortcut';
import Navigation from '../../libs/Navigation/Navigation';
import onScreenTransitionEnd from '../../libs/onScreenTransitionEnd';
import styles from '../../styles/styles';
import HeaderGap from '../HeaderGap';
import OfflineIndicator from '../OfflineIndicator';
import compose from '../../libs/compose';
import withNavigation from '../withNavigation';
import withWindowDimensions from '../withWindowDimensions';
import ONYXKEYS from '../../ONYXKEYS';
import {withNetwork} from '../OnyxProvider';
import {propTypes, defaultProps} from './propTypes';
import SafeAreaConsumer from '../SafeAreaConsumer';
class ScreenWrapper extends React.Component {
constructor(props) {
super(props);
this.state = {
didScreenTransitionEnd: false,
};
}
componentDidMount() {
const shortcutConfig = CONST.KEYBOARD_SHORTCUTS.ESCAPE;
this.unsubscribeEscapeKey = KeyboardShortcut.subscribe(shortcutConfig.shortcutKey, () => {
if (this.props.modal.willAlertModalBecomeVisible) {
return;
}
Navigation.dismissModal();
}, shortcutConfig.descriptionKey, shortcutConfig.modifiers, true);
this.unsubscribeTransitionEnd = onScreenTransitionEnd(this.props.navigation, () => {
this.setState({didScreenTransitionEnd: true});
this.props.onTransitionEnd();
});
}
/**
* We explicitly want to ignore if props.modal changes, and only want to rerender if
* any of the other props **used for the rendering output** is changed.
* @param {Object} nextProps
* @param {Object} nextState
* @returns {boolean}
*/
shouldComponentUpdate(nextProps, nextState) {
return !_.isEqual(this.state, nextState)
|| !_.isEqual(_.omit(this.props, 'modal'), _.omit(nextProps, 'modal'));
}
componentWillUnmount() {
if (this.unsubscribeEscapeKey) {
this.unsubscribeEscapeKey();
}
if (this.unsubscribeTransitionEnd) {
this.unsubscribeTransitionEnd();
}
}
render() {
return (
<SafeAreaConsumer>
{({
insets, paddingTop, paddingBottom, safeAreaPaddingBottomStyle,
}) => {
const paddingStyle = {};
if (this.props.includePaddingTop) {
paddingStyle.paddingTop = paddingTop;
}
// We always need the safe area padding bottom if we're showing the offline indicator since it is bottom-docked.
if (this.props.includeSafeAreaPaddingBottom || this.props.network.isOffline) {
paddingStyle.paddingBottom = paddingBottom;
}
return (
<View
style={[
...this.props.style,
styles.flex1,
paddingStyle,
]}
>
<KeyboardAvoidingView style={[styles.w100, styles.h100, {maxHeight: this.props.windowHeight}]} behavior={this.props.keyboardAvoidingViewBehavior}>
<HeaderGap />
{// If props.children is a function, call it to provide the insets to the children.
_.isFunction(this.props.children)
? this.props.children({
insets,
safeAreaPaddingBottomStyle,
didScreenTransitionEnd: this.state.didScreenTransitionEnd,
})
: this.props.children
}
{this.props.isSmallScreenWidth && (
<OfflineIndicator />
)}
</KeyboardAvoidingView>
</View>
);
}}
</SafeAreaConsumer>
);
}
}
ScreenWrapper.propTypes = propTypes;
ScreenWrapper.defaultProps = defaultProps;
export default compose(
withNavigation,
withWindowDimensions,
withOnyx({
modal: {
key: ONYXKEYS.MODAL,
},
}),
withNetwork(),
)(ScreenWrapper);