forked from mastermoo/react-native-action-button
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathActionButtonItem.js
More file actions
152 lines (137 loc) · 4.36 KB
/
ActionButtonItem.js
File metadata and controls
152 lines (137 loc) · 4.36 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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
import React, { Component } from 'react';
import { StyleSheet, Text, View, Animated, TouchableOpacity, Dimensions } from 'react-native';
const { width } = Dimensions.get('window');
const alignItemsMap = {
center: "center",
left: "flex-start",
right: "flex-end"
}
export default class ActionButtonItem extends Component {
constructor(props) {
super(props);
this.state = {
spaceBetween: this.props.spaceBetween || 15,
alignItems: alignItemsMap[this.props.position]
};
if (!props.children || Array.isArray(props.children)) {
throw new Error("ActionButtonItem must have a Child component.");
}
}
render() {
const translateXMap = {
center: 0,
left: (this.props.parentSize - this.props.size) / 2,
right: -(this.props.parentSize - this.props.size) / 2,
}
const translateX = translateXMap[this.props.position];
const margin = (this.props.spacing < 12) ? 0 : (this.props.spacing - 12);
return (
<Animated.View
pointerEvents="box-none"
style={[styles.actionButtonWrap, {
height: this.props.size + margin + 12,
alignItems: this.state.alignItems,
marginBottom: this.props.verticalOrientation === 'up' ? margin : 0,
marginTop: this.props.verticalOrientation === 'down' ? margin : 0,
opacity: this.props.anim,
transform: [
{ translateX },
{
translateY: this.props.anim.interpolate({
inputRange: [0, 1],
outputRange: [
this.props.verticalOrientation === 'down' ? -40 : 40,
0
]
}),
}
],
}
]}
>
<TouchableOpacity
style={{ flex:1 }}
activeOpacity={this.props.activeOpacity || 0.85}
onPress={this.props.onPress}
>
<View
style={[styles.actionButton, !this.props.hideShadow && styles.shadow, this.props.style, {
width: this.props.size,
height: this.props.size,
borderRadius: this.props.size / 2,
backgroundColor: this.props.buttonColor || this.props.btnColor,
marginBottom: this.props.verticalOrientation === 'up' ? 12 : 0,
marginTop: this.props.verticalOrientation === 'down' ? 12 : 0,
}]}
>
{this.props.children}
</View>
</TouchableOpacity>
{this.props.title && (
<TouchableOpacity
style={[this.getTextStyles(), this.props.textContainerStyle, !this.props.hideShadow && styles.shadow]}
activeOpacity={this.props.activeOpacity || 0.85}
onPress={this.props.onPress}
>
<Text style={[styles.actionText, this.props.textStyle, { color: this.props.titleColor || '#444' }]}>
{this.props.title}
</Text>
</TouchableOpacity>
)}
</Animated.View>
);
}
getTextStyles() {
// to align the center of the label with the center of the button,
// offset = (half the size of the btn) - (half the size of the label)
let offsetTop = this.props.size >= 28 ? (this.props.size / 2) - 14 : 0;
let positionStyles = {
right: this.props.size + this.state.spaceBetween,
top: offsetTop
}
let bgStyle = { backgroundColor : 'white' };
if (this.props.titleBgColor) bgStyle = {
backgroundColor:this.props.titleBgColor
}
if (this.props.position == 'left') positionStyles = {
left: this.props.size + this.state.spaceBetween,
top: offsetTop
}
if (this.props.position == 'center') positionStyles = {
right: this.props.size/2 + width/2 + this.state.spaceBetween,
top: offsetTop
}
return [styles.actionTextView, positionStyles, bgStyle];
}
}
const styles = StyleSheet.create({
actionButtonWrap: {
width
},
actionButton: {
justifyContent: 'center',
alignItems: 'center',
flexDirection: 'row',
},
shadow: {
shadowOpacity: 0.3,
shadowOffset: {
width: 0, height: 0,
},
shadowColor: '#666',
shadowRadius: 1,
elevation: 6,
},
actionTextView: {
position: 'absolute',
paddingVertical: 4,
paddingHorizontal: 10,
borderRadius: 3,
borderWidth: StyleSheet.hairlineWidth,
borderColor: '#eee',
},
actionText: {
flex: 1,
fontSize: 14,
}
});