-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
77 lines (63 loc) · 2 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
import React from 'react';
import { View, Text, TouchableOpacity } from 'react-native';
import PropTypes from 'prop-types';
class TextWith extends React.Component {
getObjectMargin(){
const { objectPosition, objectMargin } = this.props;
const objectMarginOptions = {
'left' : { marginLeft: objectMargin },
'right' : { marginRight: objectMargin },
'top' : { marginTop: objectMargin },
'bottom' : { marginBottom: objectMargin },
};
const objectMarginSelection = objectMarginOptions[objectPosition];
return objectMargin ? objectMarginSelection : {};
}
renderText(){
const {
children,
object,
objectPosition,
objectMargin,
textStyle,
containerStyle,
onPress,
...restProps
} = this.props;
const objectMarginStyle = this.getObjectMargin();
return <Text key='text' { ...restProps } style={[ objectMarginStyle, textStyle ]}>{ children }</Text>;
}
render(){
const { object, objectPosition, containerStyle, onPress } = this.props;
const stackDirection = (
objectPosition == 'left' || objectPosition == 'right'
) ? 'row' : 'column';
let components = [];
if(object){ components.push(React.cloneElement(object, {key: 'object'})); }
components.push(this.renderText());
if(objectPosition == 'right'
|| objectPosition == 'bottom'){
components.reverse(); }
return (
<TouchableOpacity onPress={onPress} disabled={ onPress ? false : true }>
<View style={[{ alignItems: 'center' }, containerStyle, { flexDirection: stackDirection }]}>
{ components }
</View>
</TouchableOpacity>
);
}
}
TextWith.defaultProps = {
objectPosition: 'left',
objectMargin: 4,
onPress: null,
};
TextWith.propTypes = {
children: PropTypes.any,
object: PropTypes.object,
objectPosition: PropTypes.oneOf(['left', 'right', 'top', 'bottom']),
objectMargin: PropTypes.number,
textStyle: PropTypes.any,
containerStyle: PropTypes.any,
};
export default TextWith;