HeaderBackButton.js
2.37 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
import * as React from 'react';
import { I18nManager, Image, Text, View, Platform, StyleSheet } from 'react-native';
import TouchableItem from '../TouchableItem';
class HeaderBackButton extends React.PureComponent {
static defaultProps = {
pressColorAndroid: 'rgba(0, 0, 0, .32)',
tintColor: Platform.select({
ios: '#037aff'
}),
truncatedTitle: 'Back'
};
state = {};
_onTextLayout = e => {
if (this.state.initialTextWidth) {
return;
}
this.setState({
initialTextWidth: e.nativeEvent.layout.x + e.nativeEvent.layout.width
});
};
render() {
const {
onPress,
pressColorAndroid,
width,
title,
titleStyle,
tintColor,
truncatedTitle
} = this.props;
const renderTruncated = this.state.initialTextWidth && width ? this.state.initialTextWidth > width : false;
const backButtonTitle = renderTruncated ? truncatedTitle : title;
// eslint-disable-next-line global-require
const asset = require('../assets/back-icon.png');
return <TouchableItem accessibilityComponentType="button" accessibilityLabel={backButtonTitle} accessibilityTraits="button" testID="header-back" delayPressIn={0} onPress={onPress} pressColor={pressColorAndroid} style={styles.container} borderless>
<View style={styles.container}>
<Image style={[styles.icon, !!title && styles.iconWithTitle, !!tintColor && { tintColor }]} source={asset} />
{Platform.OS === 'ios' && typeof backButtonTitle === 'string' && <Text onLayout={this._onTextLayout} style={[styles.title, !!tintColor && { color: tintColor }, titleStyle]} numberOfLines={1}>
{backButtonTitle}
</Text>}
</View>
</TouchableItem>;
}
}
const styles = StyleSheet.create({
container: {
alignItems: 'center',
flexDirection: 'row',
backgroundColor: 'transparent'
},
title: {
fontSize: 17,
paddingRight: 10
},
icon: Platform.OS === 'ios' ? {
height: 21,
width: 13,
marginLeft: 10,
marginRight: 22,
marginVertical: 12,
resizeMode: 'contain',
transform: [{ scaleX: I18nManager.isRTL ? -1 : 1 }]
} : {
height: 24,
width: 24,
margin: 16,
resizeMode: 'contain',
transform: [{ scaleX: I18nManager.isRTL ? -1 : 1 }]
},
iconWithTitle: Platform.OS === 'ios' ? {
marginRight: 5
} : {}
});
export default HeaderBackButton;