-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathImageButton.js
73 lines (68 loc) · 1.79 KB
/
ImageButton.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
/*
* @File : ImageButton
* @Author : lslin
* @Date : 2017-7-5 15:55:9
* @Last Modified : 2017-7-5 15:55:9
*/
import React, { PureComponent, PropTypes } from 'react';
import {
View,
Image,
Text,
StyleSheet,
ViewPropTypes,
TouchableOpacity,
} from 'react-native';
export default class ImageButton extends PureComponent {
static propTypes = {
...TouchableOpacity.propTypes,
style: View.propTypes.style,
selected: PropTypes.bool,
textStyle: Text.propTypes.style,
selectedTextStyle: Text.propTypes.style,
image: PropTypes.number.isRequired,
selectedImage: PropTypes.number.isRequired,
title: PropTypes.string,
onSelect: PropTypes.func,
disabled: PropTypes.bool,
};
_onSelect = (el) => {
if (!this.props.disabled && this.props.onSelect) this.props.onSelect(el);
}
render() {
const self = this;
let selected = this.props.selected
return (
<TouchableOpacity
testID={this.props.testID}
style={[styles.container, this.props.style]}
onPress={(el) => self._onSelect(el)}
accessibilityTraits="button"
accessibilityComponentType="button">
<Image style={styles.image} source={selected ? this.props.selectedImage : this.props.image}>
<Text style={[styles.text, selected ? this.props.selectedTextStyle : this.props.textStyle || {}]}>{this.props.title}</Text>
</Image>
</TouchableOpacity>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: 'gray',
},
image: {
height: 50,
width:80,
resizeMode: 'contain',
justifyContent: 'center',
alignItems: 'center',
},
text: {
fontSize: 16,
textAlign: 'center',
alignItems: 'center',
},
});