mopinion-react-native-sdk
Version:
Collect in-app feedback with Mopinion for React Native. Built fully in React Native for optimal integration with your React Native app.
1 lines • 15.3 kB
JavaScript
var _interopRequireDefault=require("@babel/runtime/helpers/interopRequireDefault");Object.defineProperty(exports,"__esModule",{value:true});exports.default=void 0;var _defineProperty2=_interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));var _classCallCheck2=_interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));var _createClass2=_interopRequireDefault(require("@babel/runtime/helpers/createClass"));var _possibleConstructorReturn2=_interopRequireDefault(require("@babel/runtime/helpers/possibleConstructorReturn"));var _getPrototypeOf2=_interopRequireDefault(require("@babel/runtime/helpers/getPrototypeOf"));var _inherits2=_interopRequireDefault(require("@babel/runtime/helpers/inherits"));var _react=_interopRequireWildcard(require("react"));var _reactNative=require("react-native");var _styles=require("../styles");var _Images=require("./Images");var _jsxRuntime=require("react/jsx-runtime");function _interopRequireWildcard(e,t){if("function"==typeof WeakMap)var r=new WeakMap(),n=new WeakMap();return(_interopRequireWildcard=function _interopRequireWildcard(e,t){if(!t&&e&&e.__esModule)return e;var o,i,f={__proto__:null,default:e};if(null===e||"object"!=typeof e&&"function"!=typeof e)return f;if(o=t?n:r){if(o.has(e))return o.get(e);o.set(e,f);}for(var _t in e)"default"!==_t&&{}.hasOwnProperty.call(e,_t)&&((i=(o=Object.defineProperty)&&Object.getOwnPropertyDescriptor(e,_t))&&(i.get||i.set)?o(f,_t,i):f[_t]=e[_t]);return f;})(e,t);}function _callSuper(t,o,e){return o=(0,_getPrototypeOf2.default)(o),(0,_possibleConstructorReturn2.default)(t,_isNativeReflectConstruct()?Reflect.construct(o,e||[],(0,_getPrototypeOf2.default)(t).constructor):o.apply(t,e));}function _isNativeReflectConstruct(){try{var t=!Boolean.prototype.valueOf.call(Reflect.construct(Boolean,[],function(){}));}catch(t){}return(_isNativeReflectConstruct=function _isNativeReflectConstruct(){return!!t;})();}var Rating=function(_Component){function Rating(props){var _this$props$block,_this$props$block2;var _this;(0,_classCallCheck2.default)(this,Rating);_this=_callSuper(this,Rating,[props]);_this.state={maxStars:((_this$props$block=_this.props.block)==null?void 0:_this$props$block.typeName)==='nps'?11:_this.props.scale,rating:_this.props.rating,values:_this.scoresAsArray(),animations:_this.scoresAsArray().map(function(){return{value:new _reactNative.Animated.Value(0)};}),labelHeight:{}};_this.state.starSize=(_reactNative.Dimensions.get('window').width-20)/(_this.props.includeZero?_this.props.scale+1:_this.props.scale);if(_this.state.starSize>60&&((_this$props$block2=_this.props.block)==null?void 0:_this$props$block2.properties.type)==='stars'){_this.state.starSize=40;}if(_this.state.starSize>40&&_this.state.starSize<65){_this.state.starHeight=_this.state.starSize;}else{_this.state.starHeight=_this.props.block.properties.type==='emoji'?55:_this.props.block.properties.type==='stars'?40:45;}return _this;}(0,_inherits2.default)(Rating,_Component);return(0,_createClass2.default)(Rating,[{key:"componentDidMount",value:function componentDidMount(){var _this2=this;this._panResponder=_reactNative.PanResponder.create({onStartShouldSetPanResponder:function onStartShouldSetPanResponder(evt,gestureState){return true;},onStartShouldSetPanResponderCapture:function onStartShouldSetPanResponderCapture(evt,gestureState){return true;},onMoveShouldSetPanResponder:function onMoveShouldSetPanResponder(evt,gestureState){return true;},onMoveShouldSetPanResponderCapture:function onMoveShouldSetPanResponderCapture(evt,gestureState){return true;},onPanResponderGrant:function onPanResponderGrant(evt,gestureState){_this2._updateChangeValue(evt);_this2.props.isSwiping(true);},onPanResponderMove:function onPanResponderMove(evt,gestureState){_this2._updateChangeValue(evt);},onPanResponderRelease:function onPanResponderRelease(evt,gestureState){_this2._updateChangeValue(evt,true);_this2.props.isSwiping(false);},onPanResponderTerminate:function onPanResponderTerminate(evt,gestureState){_this2.props.isSwiping(false);},onPanResponderTerminationRequest:function onPanResponderTerminationRequest(evt,gestureState){return true;},onShouldBlockNativeResponder:function onShouldBlockNativeResponder(evt,gestureState){return true;}});}},{key:"scoresAsArray",value:function scoresAsArray(){var _this3=this;var range=function range(start,end){return Array.from({length:end-start},function(v,k){return k+start;});};var start=this.props.includeZero?0:1;var scores=function scores(){if(_this3.props.reverseScore){return range(start,_this3.props.scale+1).reverse();}else{return range(start,_this3.props.scale+1);}};return scores();}},{key:"_label",value:function _label(labelText,i){var _this4=this;var theme=this.props.theme;var labelContainerStyle={pointerEvents:'none',backgroundColor:theme.tooltipBgColor,borderRadius:_styles.baseTheme.borderRadius,overflow:'hidden',paddingHorizontal:(0,_styles.spacing)(0.5),paddingVertical:(0,_styles.spacing)(0.25),justifyContent:'center',alignItems:'center',alignSelf:'center',position:'absolute',overflow:'visible',bottom:Number.isNaN(-this.state.labelHeight[i])?0:-this.state.labelHeight[i],transform:[{translateY:this.state.animations[i].value.interpolate({inputRange:[0,1],outputRange:[0,10]})}],opacity:this.state.animations[i].value.interpolate({inputRange:[0,1],outputRange:[0,1]})};var labelTextStyle=[{fontSize:12,color:theme.tooltipTextColor,textAlign:'center',fontWeight:'600'}];return(0,_jsxRuntime.jsx)(_reactNative.Animated.View,{style:labelContainerStyle,onLayout:function onLayout(event){var height=event.nativeEvent.layout.height;_this4.setState(function(prevState){return Object.assign({},prevState,{labelHeight:Object.assign({},prevState.labelHeight,(0,_defineProperty2.default)({},i,height))});});},children:(0,_jsxRuntime.jsx)(_reactNative.Animated.Text,{style:labelTextStyle,children:labelText})});}},{key:"getScores",value:function getScores(){var _this5=this;var _this$props=this.props,block=_this$props.block,reverseScore=_this$props.reverseScore,theme=_this$props.theme;return this.state.values.map(function(score,i){var baseStyle={transform:[{scale:_this5.state.animations[i].value.interpolate({inputRange:[0,0.5,1],outputRange:[1,0.667,1]})}],zIndex:_this5.state.animations[i].value.interpolate({inputRange:[0,1],outputRange:[1,10]}),overflow:'visible'};var textStyle=[styles.text,{color:_this5.state.animations[i].value.interpolate({inputRange:[0,1],outputRange:[theme.textColorSecondary,block.typeName==='ces'?reverseScore?[].concat(theme.redToGreen).reverse()[i]:theme.redToGreen[i]:_this5.props.theme.selectedControlTextColor]})}];if(block.properties.type==='numeric'||block.typeName==='nps'){var barStyle={width:_this5.state.starSize,height:_this5.state.starHeight,alignItems:'center',justifyContent:'center',backgroundColor:_this5.state.animations[i].value.interpolate({inputRange:[0,1],outputRange:[_this5.props.theme.formBgColor,_this5.props.theme.selectedControlColor]}),borderColor:_this5.state.animations[i].value.interpolate({inputRange:[0,1],outputRange:[theme.borderColor,_this5.props.theme.selectedControlColor]}),borderTopWidth:1,borderBottomWidth:1,borderLeftWidth:i==0?1:0,borderRightWidth:_this5.state.values.length-1===i?1:0,borderTopLeftRadius:i==0?_styles.baseTheme.borderRadius:0,borderBottomLeftRadius:i==0?_styles.baseTheme.borderRadius:0,borderRightWidth:1,borderTopRightRadius:_this5.state.values.length-1===i?_styles.baseTheme.borderRadius:0,borderBottomRightRadius:_this5.state.values.length-1===i?_styles.baseTheme.borderRadius:0};return(0,_jsxRuntime.jsx)(_reactNative.Animated.View,{style:[baseStyle,barStyle],children:(0,_jsxRuntime.jsx)(_reactNative.Animated.Text,{style:textStyle,children:score})},i);}else if(block.properties.type==='stars'){var starStyle={width:_this5.state.starSize,height:_this5.state.starHeight,alignItems:'center',justifyContent:'center',zIndex:10};var starSize=_this5.state.starSize>_this5.state.starHeight?_this5.state.starHeight:_this5.state.starSize;var inactiveStar={opacity:_this5.state.animations[i].value.interpolate({inputRange:[0,1],outputRange:[1,0]}),width:starSize,height:starSize,transform:[{scale:0.9}],tintColor:theme.uiComponentsGrey};var activeStar={opacity:_this5.state.animations[i].value.interpolate({inputRange:[0,1],outputRange:[0,1]}),zIndex:10,width:starSize,height:starSize,position:'absolute',transform:[{scale:0.9}],tintColor:theme.selectedStarColor};var label=block.properties.showCaptions?_this5._label(block.properties.elements[i+1].label,i):null;return(0,_jsxRuntime.jsxs)(_reactNative.View,{children:[(0,_jsxRuntime.jsxs)(_reactNative.Animated.View,{style:[baseStyle,starStyle],children:[(0,_jsxRuntime.jsx)(_reactNative.Animated.Image,{source:{uri:_this5.props.starIcon},style:inactiveStar}),(0,_jsxRuntime.jsx)(_reactNative.Animated.Image,{source:{uri:_this5.props.starIcon},style:activeStar})]}),label]},i);}else if(block.properties.type==='emoji'){var emojiStyle={width:_this5.state.starSize,height:_this5.state.starHeight,alignItems:'center',justifyContent:'center',zIndex:10};var emojiSize=_this5.state.starSize>_this5.state.starHeight?_this5.state.starHeight:_this5.state.starSize;var inActiveEmoji={opacity:_this5.state.animations[i].value.interpolate({inputRange:[0,1],outputRange:[1,0]}),width:emojiSize,height:emojiSize,transform:[{scale:0.8}],tintColor:_this5.props.theme.uiComponentsGrey};var activeEmoji={opacity:_this5.state.animations[i].value.interpolate({inputRange:[0,1],outputRange:[0,1]}),zIndex:10,width:emojiSize,height:emojiSize,position:'absolute',tintColor:theme.redToGreen[i]};var emoji=_this5.props.emojis[i];var _label2=block.properties.showCaptions?_this5._label(block.properties.emoji[i+1].label,i):null;return(0,_jsxRuntime.jsxs)(_reactNative.View,{children:[(0,_jsxRuntime.jsxs)(_reactNative.Animated.View,{style:[baseStyle,emojiStyle],children:[(0,_jsxRuntime.jsx)(_reactNative.Animated.Image,{source:{uri:emoji.img},style:[inActiveEmoji]}),(0,_jsxRuntime.jsx)(_reactNative.Animated.Image,{source:{uri:emoji.img},style:[activeEmoji]})]}),_label2]},i);}else if(block.properties.type=='bar'){var _barStyle={width:_this5.state.starSize,height:_this5.state.starHeight,alignItems:'center',justifyContent:'center',backgroundColor:_this5.state.animations[i].value.interpolate({inputRange:[0,1],outputRange:[theme.formBgColor,theme.redToGreen[i]]}),borderTopLeftRadius:i==0?2:0,borderBottomLeftRadius:i==0?2:0,borderTopRightRadius:_this5.state.values.length-1===i?2:0,borderBottomRightRadius:_this5.state.values.length-1===i?2:0,borderColor:_this5.state.animations[i].value.interpolate({inputRange:[0,1],outputRange:[theme.borderColor,theme.redToGreen[i]]}),borderTopWidth:1,borderBottomWidth:1,borderLeftWidth:i==0?1:0,borderRightWidth:1};var _textStyle={color:_this5.state.animations[i].value.interpolate({inputRange:[0,1],outputRange:[theme.textColorSecondary,theme.selectedControlTextColor]}),fontSize:theme.typography.base.fontSize};return(0,_jsxRuntime.jsx)(_reactNative.Animated.View,{style:[baseStyle,_barStyle],children:(0,_jsxRuntime.jsx)(_reactNative.Animated.Text,{style:_textStyle,children:_this5.props.ces[i]})},i);}else if(block.typeName==='ces'){var rtgColor=reverseScore?[].concat(theme.redToGreen).reverse()[i]:theme.redToGreen[i];var cesStyle={width:_this5.state.starSize,height:_this5.state.starHeight,alignItems:'center',justifyContent:'center',borderBottomColor:_this5.state.animations[i].value.interpolate({inputRange:[0,1],outputRange:[theme.borderColor,rtgColor]}),borderBottomWidth:3};var _label3=block.properties.showCaptions?_this5._label(reverseScore?block.properties.elements[score].label:block.properties.elements[i+1].label,i):null;return(0,_jsxRuntime.jsxs)(_reactNative.View,{children:[(0,_jsxRuntime.jsx)(_reactNative.Animated.View,{style:[baseStyle,cesStyle],children:(0,_jsxRuntime.jsx)(_reactNative.Animated.Text,{style:textStyle,children:reverseScore?[].concat(_this5.props.ces).reverse()[i]:_this5.props.ces[i]})}),_label3]},i);}});}},{key:"render",value:function render(){var _this$_panResponder$p,_this$_panResponder;var block=this.props.block;var wrapperStyle=[styles.wrapper,block.properties.type==='numeric'||block.typeName==='nps'?styles.radius:{}];var containerStyle=[styles.container,block.properties.showCaptions?styles.labelPadding:{}];return(0,_jsxRuntime.jsxs)(_reactNative.View,{style:wrapperStyle,children:[(0,_jsxRuntime.jsx)(_reactNative.View,Object.assign({style:containerStyle},(_this$_panResponder$p=(_this$_panResponder=this._panResponder)==null?void 0:_this$_panResponder.panHandlers)!=null?_this$_panResponder$p:{},{children:this.getScores()})),block.properties.legend&&(0,_jsxRuntime.jsx)(Legend,{legend:block.properties.legend,reverse:block.properties.reverseScore})]});}},{key:"_updateChangeValue",value:function _updateChangeValue(evt,shouldUpdateState){var _this6=this;var starWidth=this.state.starSize;var isRTL=_reactNative.I18nManager.isRTL;var pageX=evt.nativeEvent.pageX;var containerWidth=_reactNative.Dimensions.get('window').width;var elementPosition=isRTL?Math.floor((containerWidth-pageX)/starWidth):Math.ceil(pageX/starWidth)-1;var fitPosition=elementPosition<=0?0:elementPosition>=this.state.maxStars?this.state.maxStars-1:elementPosition;var ratingValue=this.state.values[fitPosition];var cb=function cb(){if(shouldUpdateState){_this6.setState({rating:ratingValue});_this6.props.valueChanged(ratingValue);}};this._animate(fitPosition,cb);}},{key:"_animate",value:function _animate(elementPosition,callback){var _this7=this;this.state.animations.forEach(function(o,i){var to=_this7.props.block.properties.type==='stars'?i<=elementPosition:i===elementPosition;var toLabel=i==elementPosition;_reactNative.Animated.spring(o.value,{toValue:to?1:0,duration:225,useNativeDriver:false}).start(callback);});}}]);}(_react.Component);Rating.defaultProps={maxStars:5,rating:-1,starWidth:30,valueChanged:function valueChanged(index){},emojis:[{label:'Hate',img:_Images.base64Images.emojis.angry},{label:'Dislike',img:_Images.base64Images.emojis.sad},{label:'Neutral',img:_Images.base64Images.emojis.neutral},{label:'Like',img:_Images.base64Images.emojis.like},{label:'Love',img:_Images.base64Images.emojis.love}],starIcon:_Images.base64Images.stars,ces:['--','-','0','+','++'],isSwiping:function isSwiping(){}};function Legend(_ref){var _ref$legend=_ref.legend,legend=_ref$legend===void 0?{}:_ref$legend,reverse=_ref.reverse;var theme=(0,_styles.useTheme)();var left=reverse?legend.legendHigh:legend.legendLow;var right=reverse?legend.legendLow:legend.legendHigh;return(0,_jsxRuntime.jsxs)(_reactNative.View,{style:styles.legend,children:[(0,_jsxRuntime.jsx)(_reactNative.Text,{style:theme.typography.caption,children:left}),(0,_jsxRuntime.jsx)(_reactNative.Text,{style:theme.typography.caption,children:right})]});}var styles=_reactNative.StyleSheet.create({wrapper:{overflow:'visible'},container:{flexDirection:'row',justifyContent:'flex-start',alignItems:'center',zIndex:10},border:{borderRadius:_styles.baseTheme.borderRadius,borderColor:'rgba(0,0,0,.12)',borderWidth:1,overflow:'hidden'},radius:{borderRadius:2},text:{fontSize:_styles.baseTheme.typography.base.fontSize,fontWeight:600},labelPadding:{paddingBottom:15},legend:{flex:1,flexDirection:'row',justifyContent:'space-between'}});var _default=exports.default=(0,_styles.withTheme)(Rating);