rn-try-n-buy-abfrl
Version:
Virtual try-on plugin for ABFRL
292 lines (268 loc) • 6.56 kB
JavaScript
import React, { Component } from "react";
import PropTypes from "prop-types";
import { StyleSheet, Text, Image, View, Animated, TouchableOpacity } from "react-native";
import { getTouchableComponent } from "./utils/touchable";
import SceneUtils from "../../utils/SceneUtils";
class FloatingActionItem extends Component {
constructor(props) {
super(props);
this.animation = new Animated.Value(0);
}
componentDidUpdate(prevProps) {
const { active, animated } = this.props;
if (prevProps.active !== active && animated) {
Animated.spring(this.animation, {
toValue: active ? 1 : 0,
useNativeDriver: false
}).start();
}
}
get distanceToHorizontalEdge() {
const { distanceToEdge } = this.props;
return typeof distanceToEdge === 'number'
? distanceToEdge
: distanceToEdge.horizontal;
}
get distanceToVerticalEdge() {
const { distanceToEdge } = this.props;
return typeof distanceToEdge === 'number'
? distanceToEdge
: distanceToEdge.vertical;
}
handleOnPress = () => {
const { name, onPress } = this.props;
onPress(name);
};
renderText() {
const {
text,
position,
textColor,
textStyle,
textProps,
textContainerStyle,
} = this.props;
if (text) {
return (
<View
key="text"
style={[
styles.textContainer,
styles[`${position}TextContainer`],
textContainerStyle
]}
>
<Text
style={[
styles.text,
{
color: textColor
},
textStyle
]}
{...textProps}
>
{text}
</Text>
{this.props.text === 'GO TO BAG' &&
<View style={styles.countContainer}>
<Text style={styles.countText}>{this.props.cartCount}</Text>
</View>}
</View>
);
}
return null;
}
renderButton() {
const { buttonSize, icon, color, shadow, tintColor } = this.props;
if (icon && icon.uri) {
iconStyle = styles.iconLogo;
} else {
iconStyle = styles.icon;
}
const propStyles = {
tintColor: tintColor,
backgroundColor: color,
width: buttonSize,
height: buttonSize,
borderRadius: buttonSize / 2
};
return (
<View
key="button"
style={styles.button}
>
<Image style={styles.icon} source={icon} />
</View>
);
}
render() {
const {
position,
render,
margin,
name,
animated
} = this.props;
const Touchable = getTouchableComponent(false);
let animatedActionContainerStyle;
if (animated) {
animatedActionContainerStyle = {
marginBottom: this.animation.interpolate({
inputRange: [0, 1],
outputRange: [1, 3]
})
};
} else {
animatedActionContainerStyle = { marginBottom: 6 };
}
const components = [];
components.push(this.renderButton());
components.push(this.renderText());
return (
<Touchable
activeOpacity={0.4}
style={styles.container}
>
<TouchableOpacity
onPress={this.handleOnPress}>
<Animated.View
style={[
styles.actionContainer,
animatedActionContainerStyle,
styles[`${position}ActionContainer`],
{
borderTopLeftRadius: this.props.index === 0 ? 6 : 0,
borderTopRightRadius: this.props.index === 0 ? 6 : 0,
borderBottomLeftRadius: this.props.index === 1 ? 6 : 0,
borderBottomRightRadius: this.props.index === 1 ? 6 : 0
}
]}
>
{components}
</Animated.View>
</TouchableOpacity>
</Touchable>
);
}
}
FloatingActionItem.propTypes = {
tintColor: PropTypes.string,
color: PropTypes.string,
icon: PropTypes.any,
name: PropTypes.string.isRequired,
buttonSize: PropTypes.number,
textContainerStyle: PropTypes.object,
text: PropTypes.string,
textStyle: PropTypes.object,
textProps: PropTypes.object,
textBackground: PropTypes.string,
textColor: PropTypes.string,
shadow: PropTypes.shape({
shadowOpacity: PropTypes.number,
shadowOffset: PropTypes.shape({
width: PropTypes.number,
height: PropTypes.number
}),
shadowColor: PropTypes.string,
shadowRadius: PropTypes.number
}),
// not modified by user
position: PropTypes.oneOf(["left", "right", "center"]),
active: PropTypes.bool,
distanceToEdge: PropTypes.oneOfType([
PropTypes.number,
PropTypes.shape({
vertical: PropTypes.number,
horizontal: PropTypes.number
})
]), // modified by parent property "actionsPaddingTopBottom"
onPress: PropTypes.func,
render: PropTypes.func,
margin: PropTypes.number,
animated: PropTypes.bool
};
FloatingActionItem.defaultProps = {
tintColor: '#fff',
color: SceneUtils.primary_color,
distanceToEdge: 30,
buttonSize: 40,
textColor: SceneUtils.text_color,
textBackground: "#ffffff",
margin: 8,
shadow: {
shadowOpacity: 0.35,
shadowOffset: {
width: 0,
height: 5
},
shadowColor: "#000000",
shadowRadius: 3
}
};
const styles = StyleSheet.create({
container: {
flex: 1,
flexDirection: "column"
},
actionContainer: {
flex: 1,
flexDirection: "row",
alignItems: 'center',
paddingLeft: 0,
paddingRight: 0,
backgroundColor: '#FFF',
width: 220,
marginRight: 30,
height: 48
},
textContainer: {
paddingHorizontal: 5,
borderRadius: 4,
height: 22,
flexDirection: 'row',
flex:1
},
leftTextContainer: {
marginLeft: 14
},
rightTextContainer: {
marginRight: 14
},
text: {
fontSize: 14,
lineHeight: 20
},
button: {
alignItems: "center",
justifyContent: "center",
marginHorizontal: 14
},
iconLogo: {
resizeMode: "cover",
width: 40,
height: 40,
borderRadius: 20
},
icon: {
resizeMode: "contain",
width: 20,
height: 20
},
countContainer: {
width: 24,
height: 24,
backgroundColor: SceneUtils.primary_color,
justifyContent: 'center',
alignItems: 'center',
borderRadius: 12,
position:'absolute',
right:0
},
countText:{
color: 'white',
fontWeight: 'bold',
fontSize: 12
}
});
export default FloatingActionItem;