react-native-popup-dialog
Version:
React Native Popup Dialog for IOS & Android.
1 lines • 8 kB
JavaScript
Object.defineProperty(exports,"__esModule",{value:true});var _jsxFileName='src/components/Dialog.js';var _createClass=function(){function defineProperties(target,props){for(var i=0;i<props.length;i++){var descriptor=props[i];descriptor.enumerable=descriptor.enumerable||false;descriptor.configurable=true;if("value"in descriptor)descriptor.writable=true;Object.defineProperty(target,descriptor.key,descriptor);}}return function(Constructor,protoProps,staticProps){if(protoProps)defineProperties(Constructor.prototype,protoProps);if(staticProps)defineProperties(Constructor,staticProps);return Constructor;};}();var _extends=Object.assign||function(target){for(var i=1;i<arguments.length;i++){var source=arguments[i];for(var key in source){if(Object.prototype.hasOwnProperty.call(source,key)){target[key]=source[key];}}}return target;};var _react=require('react');var _react2=_interopRequireDefault(_react);var _reactNative=require('react-native');var _Overlay=require('./Overlay');var _Overlay2=_interopRequireDefault(_Overlay);var _DialogActionList=require('./DialogActionList');var _DialogActionList2=_interopRequireDefault(_DialogActionList);var _FadeAnimation=require('../animations/FadeAnimation');var _FadeAnimation2=_interopRequireDefault(_FadeAnimation);var _ScaleAnimation=require('../animations/ScaleAnimation');var _ScaleAnimation2=_interopRequireDefault(_ScaleAnimation);var _SlideAnimation=require('../animations/SlideAnimation');var _SlideAnimation2=_interopRequireDefault(_SlideAnimation);function _interopRequireDefault(obj){return obj&&obj.__esModule?obj:{default:obj};}function _classCallCheck(instance,Constructor){if(!(instance instanceof Constructor)){throw new TypeError("Cannot call a class as a function");}}function _possibleConstructorReturn(self,call){if(!self){throw new ReferenceError("this hasn't been initialised - super() hasn't been called");}return call&&(typeof call==="object"||typeof call==="function")?call:self;}function _inherits(subClass,superClass){if(typeof superClass!=="function"&&superClass!==null){throw new TypeError("Super expression must either be null or a function, not "+typeof superClass);}subClass.prototype=Object.create(superClass&&superClass.prototype,{constructor:{value:subClass,enumerable:false,writable:true,configurable:true}});if(superClass)Object.setPrototypeOf?Object.setPrototypeOf(subClass,superClass):subClass.__proto__=superClass;}var babelPluginFlowReactPropTypes_proptype_DialogProps=require('../type').babelPluginFlowReactPropTypes_proptype_DialogProps||require('prop-types').any;var BackHandler=_reactNative.BackHandler||_reactNative.BackAndroid;var DIALOG_OPENING='opening';var DIALOG_OPENED='opened';var DIALOG_CLOSING='closing';var DIALOG_CLOSED='closed';var DEFAULT_ANIMATION_DURATION=150;var HARDWARE_BACK_PRESS_EVENT='hardwareBackPress';var styles=_reactNative.StyleSheet.create({container:_extends({},_reactNative.StyleSheet.absoluteFillObject,{justifyContent:'center',alignItems:'center',zIndex:1000}),dialog:{overflow:'hidden',backgroundColor:'#ffffff'},hidden:{top:-10000,left:0,height:0,width:0},round:{borderRadius:8}});var Dialog=function(_Component){_inherits(Dialog,_Component);function Dialog(props){_classCallCheck(this,Dialog);var _this=_possibleConstructorReturn(this,(Dialog.__proto__||Object.getPrototypeOf(Dialog)).call(this,props));_this.show=function(){var onShow=_this.props.onShow;if(![DIALOG_OPENING,DIALOG_OPENED].includes(_this.state.dialogState)){_this.setDialogState(1,onShow);}};_this.dismiss=function(){var onDismiss=_this.props.onDismiss;if(![DIALOG_CLOSING,DIALOG_CLOSED].includes(_this.state.dialogState)){_this.setDialogState(0,onDismiss);}};_this.state={dialogAnimation:props.dialogAnimation||new _FadeAnimation2.default({animationDuration:props.animationDuration}),dialogState:DIALOG_CLOSED};return _this;}_createClass(Dialog,[{key:'componentDidMount',value:function componentDidMount(){var _props=this.props,visible=_props.visible,onHardwareBackPress=_props.onHardwareBackPress;if(visible){this.show();}BackHandler.addEventListener(HARDWARE_BACK_PRESS_EVENT,onHardwareBackPress);}},{key:'componentDidUpdate',value:function componentDidUpdate(prevProps){if(this.props.visible!==prevProps.visible){if(this.props.visible){this.show();return;}this.dismiss();}}},{key:'componentWillUnmount',value:function componentWillUnmount(){var onHardwareBackPress=this.props.onHardwareBackPress;BackHandler.removeEventListener(HARDWARE_BACK_PRESS_EVENT,onHardwareBackPress);}},{key:'setDialogState',value:function setDialogState(toValue){var _this2=this;var callback=arguments.length>1&&arguments[1]!==undefined?arguments[1]:function(){};var dialogAnimation=this.state.dialogAnimation;var animationDuration=this.props.animationDuration;var dialogState=toValue?DIALOG_OPENING:DIALOG_CLOSING;dialogAnimation.toValue(toValue);this.setState({dialogState:dialogState});setTimeout(function(){dialogState=dialogState===DIALOG_CLOSING?DIALOG_CLOSED:DIALOG_OPENED;_this2.setState({dialogState:dialogState},callback);},animationDuration);}},{key:'render',value:function render(){var _state=this.state,dialogState=_state.dialogState,dialogAnimation=_state.dialogAnimation;var _props2=this.props,rounded=_props2.rounded,dialogTitle=_props2.dialogTitle,children=_props2.children,onTouchOutside=_props2.onTouchOutside,hasOverlay=_props2.hasOverlay,dialogStyle=_props2.dialogStyle,animationDuration=_props2.animationDuration,overlayOpacity=_props2.overlayOpacity,useNativeDriver=_props2.useNativeDriver,overlayBackgroundColor=_props2.overlayBackgroundColor,containerStyle=_props2.containerStyle,actionsBordered=_props2.actionsBordered,actionContainerStyle=_props2.actionContainerStyle,actions=_props2.actions;var overlayVisible=hasOverlay&&[DIALOG_OPENING,DIALOG_OPENED].includes(dialogState);var round=rounded?styles.round:null;var hidden=dialogState===DIALOG_CLOSED&&styles.hidden;var buttons=actions?_react2.default.createElement(_DialogActionList2.default,{style:actionContainerStyle,bordered:actionsBordered,__source:{fileName:_jsxFileName,lineNumber:196}},actions):null;return _react2.default.createElement(_reactNative.View,{style:[styles.container,hidden,containerStyle],__source:{fileName:_jsxFileName,lineNumber:202}},_react2.default.createElement(_Overlay2.default,{pointerEvents:this.pointerEvents,visible:overlayVisible,onPress:onTouchOutside,backgroundColor:overlayBackgroundColor,opacity:overlayOpacity,animationDuration:animationDuration,useNativeDriver:useNativeDriver,__source:{fileName:_jsxFileName,lineNumber:203}}),_react2.default.createElement(_reactNative.Animated.View,{style:[styles.dialog,round,this.dialogSize,dialogStyle,dialogAnimation.animations],__source:{fileName:_jsxFileName,lineNumber:212}},dialogTitle,children,buttons));}},{key:'pointerEvents',get:function get(){var overlayPointerEvents=this.props.overlayPointerEvents;var dialogState=this.state.dialogState;if(overlayPointerEvents){return overlayPointerEvents;}return dialogState===DIALOG_OPENED?'auto':'none';}},{key:'dialogSize',get:function get(){var _Dimensions$get=_reactNative.Dimensions.get('window'),screenWidth=_Dimensions$get.width,screenHeight=_Dimensions$get.height;var _props3=this.props,width=_props3.width,height=_props3.height;if(width&&width>0.0&&width<=1.0){width*=screenWidth;}if(height&&height>0.0&&height<=1.0){height*=screenHeight;}return{width:width,height:height};}}]);return Dialog;}(_react.Component);Dialog.defaultProps={rounded:true,dialogTitle:null,visible:false,containerStyle:null,actionContainerStyle:null,actionsBordered:true,animationDuration:DEFAULT_ANIMATION_DURATION,dialogStyle:null,width:null,height:null,onTouchOutside:function onTouchOutside(){},onHardwareBackPress:function onHardwareBackPress(){return true;},hasOverlay:true,overlayOpacity:0.5,overlayPointerEvents:null,overlayBackgroundColor:'#000',onShow:function onShow(){},onDismiss:function onDismiss(){},actions:null,useNativeDriver:true};Dialog.propTypes=babelPluginFlowReactPropTypes_proptype_DialogProps;exports.default=Dialog;