-
Notifications
You must be signed in to change notification settings - Fork 570
/
ActionButtonItem.js
217 lines (198 loc) · 5.28 KB
/
ActionButtonItem.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
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
import React, { Component } from "react";
import PropTypes from "prop-types";
import {
StyleSheet,
Text,
View,
Animated,
TouchableNativeFeedback,
TouchableWithoutFeedback,
Dimensions,
} from "react-native";
import {
shadowStyle,
alignItemsMap,
getTouchableComponent,
isAndroid,
touchableBackground,
DEFAULT_ACTIVE_OPACITY
} from "./shared";
const { width: WIDTH } = Dimensions.get("window");
const SHADOW_SPACE = 10;
const TEXT_HEIGHT = 22;
const TextTouchable = isAndroid
? TouchableNativeFeedback
: TouchableWithoutFeedback;
export default class ActionButtonItem extends Component {
static get defaultProps() {
return {
active: true,
spaceBetween: 15,
useNativeFeedback: true,
activeOpacity: DEFAULT_ACTIVE_OPACITY,
fixNativeFeedbackRadius: false,
nativeFeedbackRippleColor: "rgba(255,255,255,0.75)",
numberOfLines: 1,
};
}
static get propTypes() {
return {
active: PropTypes.bool,
useNativeFeedback: PropTypes.bool,
fixNativeFeedbackRadius: PropTypes.bool,
nativeFeedbackRippleColor: PropTypes.string,
activeOpacity: PropTypes.number,
numberOfLines: PropTypes.number,
};
}
render() {
const {
size,
position,
verticalOrientation,
hideShadow,
spacing
} = this.props;
if (!this.props.active) return null;
const animatedViewStyle = {
marginBottom: -SHADOW_SPACE,
alignItems: alignItemsMap[position],
// backgroundColor: this.props.buttonColor,
opacity: this.props.anim,
transform: [
{
translateY: this.props.anim.interpolate({
inputRange: [0, 1],
outputRange: [verticalOrientation === "down" ? -40 : 40, 0]
})
}
]
};
const buttonStyle = {
justifyContent: "center",
alignItems: "center",
width: size,
height: size,
borderRadius: size / 2,
backgroundColor: this.props.buttonColor || this.props.btnColor
};
if (position !== "center")
buttonStyle[position] = (this.props.parentSize - size) / 2;
const Touchable = getTouchableComponent(this.props.useNativeFeedback);
const parentStyle = isAndroid &&
this.props.fixNativeFeedbackRadius
? {
height: size,
marginBottom: spacing,
right: this.props.offsetX,
borderRadius: this.props.size / 2
}
: {
paddingHorizontal: this.props.offsetX,
height: size + SHADOW_SPACE + spacing
};
return (
<Animated.View
pointerEvents="box-none"
style={[animatedViewStyle, parentStyle]}
>
<View>
<Touchable
rejectResponderTermination
testID={this.props.testID}
accessibilityLabel={this.props.accessibilityLabel}
background={touchableBackground(
this.props.nativeFeedbackRippleColor,
this.props.fixNativeFeedbackRadius
)}
activeOpacity={this.props.activeOpacity || DEFAULT_ACTIVE_OPACITY}
onPress={this.props.onPress}
>
<View style={[
buttonStyle,
!hideShadow ? {...shadowStyle, ...this.props.shadowStyle} : null
]}>
{this.props.children}
</View>
</Touchable>
</View>
{this._renderTitle()}
</Animated.View>
);
}
_renderTitle() {
if (!this.props.title) return null;
const {
textContainerStyle,
hideLabelShadow,
offsetX,
parentSize,
size,
position,
spaceBetween,
numberOfLines,
} = this.props;
const offsetTop = Math.max(size / 2 - TEXT_HEIGHT / 2, 0);
const positionStyles = { top: offsetTop };
const hideShadow = hideLabelShadow === undefined
? this.props.hideShadow
: hideLabelShadow;
if (position !== "center") {
positionStyles[position] =
offsetX + (parentSize - size) / 2 + size + spaceBetween;
} else {
positionStyles.right = WIDTH / 2 + size / 2 + spaceBetween;
}
const textStyles = [
styles.textContainer,
positionStyles,
!hideShadow && shadowStyle,
textContainerStyle
];
const title = (
React.isValidElement(this.props.title) ?
this.props.title
: (
<Text
allowFontScaling={false}
style={[styles.text, this.props.textStyle]}
numberOfLines={numberOfLines}
>
{this.props.title}
</Text>
)
)
return (
<TextTouchable
rejectResponderTermination
background={touchableBackground(
this.props.nativeFeedbackRippleColor,
this.props.fixNativeFeedbackRadius
)}
activeOpacity={this.props.activeOpacity || DEFAULT_ACTIVE_OPACITY}
onPress={this.props.onPress}
>
<View style={textStyles}>
{title}
</View>
</TextTouchable>
);
}
}
const styles = StyleSheet.create({
textContainer: {
position: "absolute",
paddingVertical: isAndroid ? 2 : 3,
paddingHorizontal: 8,
borderRadius: 3,
borderWidth: StyleSheet.hairlineWidth,
borderColor: "#eee",
backgroundColor: "white",
height: TEXT_HEIGHT
},
text: {
flex: 1,
fontSize: 12,
color: "#444"
}
});