ReactNative封装的优雅居中/底部弹出框

2024-06-01 17:08

本文主要是介绍ReactNative封装的优雅居中/底部弹出框,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

/** @Date: 2019-08-30 16:05:37* @Description: 真的不想每次都写个Modal了。* @Author: zhangji* @LastEditors: ZhangJi* @LastEditTime: 2019-09-02 11:16:51*/import React from "react";
import _ from "lodash";
import {View,StyleSheet,Component,Platform,ActivityIndicator,FlatList,Image,TouchableWithoutFeedback,TouchableOpacity,Text,Alert,Modal,RefreshControl,NativeModules,ScrollView,TekLoadingDialog,LayoutAnimation
} = 'react-native';/*** 通用弹出框,包括居中弹出框和底部弹出框* @param {Boolean} isVisible 控制是否可见* @param {?Boolean} isBottomView 可选,控制是否为底部弹出框样式,默认为居中弹出框* @param {?Boolean} isTouchMaskToClose 可选,控制是否点击阴影关闭弹窗,默认开启* @param {?String} title 可选,标题,默认为`提示`,只对居中弹窗生效* @param {?String} confirmText 可选,居中框的确认按钮描述,默认为`确 认`* @param {?JSX} customTitleView 可选,自定义title样式(包括居中和底部弹框),若该属性有值,会覆盖默认样式,当需要自定义按钮点击功能时可以用这个,* @param {?JSX} customBottomView 可选,自定义底部样式(包括居中和底部弹框),若该属性有值,会覆盖默认样式,当需要自定义按钮点击功能时可以用这个,** eg:` <MyModal*         isVisible={this.state.isVisible}*      >*          <Text>测试弹窗</Text>*      </MyModal>`*/
export default class MyModal extends Component {constructor(props) {super(props);this.title = props.title || "提示";this.confirmText = props.confirmText || "确 认";this.customTitleView = props.customTitleView? props.customTitleView: false;this.customBottomView = props.customBottomView? props.customBottomView: false;this.isBottomView = !!JSON.stringify(props.isBottomView)? this.props.isBottomView: false;this.isTouchMaskToClose = !!JSON.stringify(props.isTouchMaskToClose)? this.props.isTouchMaskToClose: true;this.state = {isVisible: this.props.isVisible || false};}setModalVisiable(state) {this.setState({isVisible: state});}componentWillReceiveProps(newProps) {if (!_.isEmpty(this.props, newProps)) {if (this.state.isVisible != newProps.isVisible) {this.setState({isVisible: newProps.isVisible});}}}render() {return (<ModalanimationType="fade"transparent={true}visible={this.state.isVisible}onRequestClose={() => {this.setModalVisiable(false);}}>{this.isBottomView ? (<View style={styles.bottomModalContainer}><TouchableOpacitystyle={styles.bottomMask}onPress={() => {this.setModalVisiable(false);}}/><View style={styles.bottomContent}>{this.props.children}</View>{this.customBottomView ? (this.customBottomView) : (<View style={styles.bottomBtns}><TouchableOpacityonPress={() => {this.setModalVisiable(false);}}><Viewstyle={[styles.bottomBtnsView,{ borderWidth: 0.5, borderColor: "#999" }]}><Textstyle={[styles.bottomBtnsText,{ color: "#333", fontFamily: "PingFangSC-Light" }]}>取消</Text></View></TouchableOpacity><TouchableOpacity onPress={() => {}}><Viewstyle={[styles.bottomBtnsView,{backgroundColor: "#417EFF",borderWidth: 0.5,borderColor: "#417EFF"}]}><Textstyle={[styles.bottomBtnsText,{ color: "#fff", fontWeight: "bold" }]}>确定</Text></View></TouchableOpacity></View>)}</View>) : (<View style={styles.modalContainer}><TouchableWithoutFeedbackonPress={() => {this.isTouchMaskToClose ? this.setModalVisiable(false) : null;}}><View style={styles.mask} /></TouchableWithoutFeedback><View style={styles.container}>{this.customTitleView ? (this.customTitleView) : (<Text style={styles.title}>{this.title}</Text>)}{this.props.children}<Viewstyle={{height: 0.5,width: appConfig.ScreenWidth - 42,backgroundColor: "#E5E5E5"}}/>{this.customBottomView ? (this.customBottomView) : (<TouchableOpacity onPress={() => this.setModalVisiable(false)}><Text style={styles.confirmBtn}>{this.confirmText}</Text></TouchableOpacity>)}</View></View>)}</Modal>);}
}const styles = StyleSheet.create({modalContainer: {flex: 1,justifyContent: "center",alignItems: "center",width: appConfig.ScreenWidth,height: appConfig.ScreenHeight},mask: {width: appConfig.ScreenWidth,height: appConfig.ScreenHeight,backgroundColor: "rgba(0,0,0,.4)",position: "absolute",left: 0,top: 0},container: {width: appConfig.ScreenWidth - 30,backgroundColor: "#FFF",borderTopLeftRadius: 9,borderTopRightRadius: 9,borderBottomLeftRadius: 9,borderBottomRightRadius: 9,justifyContent: "center",alignItems: "center"},title: {textAlign: "center",fontFamily: "PingFangSC-Semibold",fontSize: 16,color: "#333",marginTop: 18},confirmBtn: {color: "#417EFF",fontSize: 17,fontFamily: "PingFangSC-Semibold",marginBottom: 18,marginTop: 14.2,width: appConfig.ScreenWidth - 42,textAlign: "center"},bottomModalContainer: {flex: 1,justifyContent: "flex-end",width: appConfig.ScreenWidth,height: appConfig.ScreenHeight},bottomMask: {flex: 1,width: appConfig.ScreenWidth,marginBottom: -9,backgroundColor: "rgba(0,0,0,.4)"},content: {width: appConfig.ScreenWidth,backgroundColor: "#FFF",borderTopLeftRadius: 9,borderTopRightRadius: 9,paddingTop: 15},bottomBtns: {flexDirection: "row",alignItems: "center",justifyContent: "space-around",backgroundColor: "#fff",marginBottom: appConfig.StatusBarHeight == 44 ? 34 : 0,shadowColor: "#00000033",shadowOffset: { width: 0, height: -9 },shadowOpacity: 0.1,shadowRadius: 6,elevation: 10},bottomBtnsView: {width: 165,height: 42,borderRadius: 100,marginTop: 12,marginBottom: 12,justifyContent: "center",alignItems: "center"},bottomBtnsText: {fontSize: 16},bottomModalContainer: {flex: 1,justifyContent: "flex-end",width: appConfig.ScreenWidth,height: appConfig.ScreenHeight},bottomMask: {flex: 1,width: appConfig.ScreenWidth,backgroundColor: "#33333344",marginBottom: -9},bottomContent: {width: appConfig.ScreenWidth,backgroundColor: "#FFF",borderTopLeftRadius: 9,borderTopRightRadius: 9,paddingTop: 15}
});

这篇关于ReactNative封装的优雅居中/底部弹出框的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1021608

相关文章

一文详解如何在Vue3中封装API请求

《一文详解如何在Vue3中封装API请求》在现代前端开发中,API请求是不可避免的一部分,尤其是与后端交互时,下面我们来看看如何在Vue3项目中封装API请求,让你在实现功能时更加高效吧... 目录为什么要封装API请求1. vue 3项目结构2. 安装axIOS3. 创建API封装模块4. 封装API请求

Java并发编程之如何优雅关闭钩子Shutdown Hook

《Java并发编程之如何优雅关闭钩子ShutdownHook》这篇文章主要为大家详细介绍了Java如何实现优雅关闭钩子ShutdownHook,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起... 目录关闭钩子简介关闭钩子应用场景数据库连接实战演示使用关闭钩子的注意事项开源框架中的关闭钩子机制1.

Spring Boot中JSON数值溢出问题从报错到优雅解决办法

《SpringBoot中JSON数值溢出问题从报错到优雅解决办法》:本文主要介绍SpringBoot中JSON数值溢出问题从报错到优雅的解决办法,通过修改字段类型为Long、添加全局异常处理和... 目录一、问题背景:为什么我的接口突然报错了?二、为什么会发生这个错误?1. Java 数据类型的“容量”限制

Java实现优雅日期处理的方案详解

《Java实现优雅日期处理的方案详解》在我们的日常工作中,需要经常处理各种格式,各种类似的的日期或者时间,下面我们就来看看如何使用java处理这样的日期问题吧,感兴趣的小伙伴可以跟随小编一起学习一下... 目录前言一、日期的坑1.1 日期格式化陷阱1.2 时区转换二、优雅方案的进阶之路2.1 线程安全重构2

使用Python实现一个优雅的异步定时器

《使用Python实现一个优雅的异步定时器》在Python中实现定时器功能是一个常见需求,尤其是在需要周期性执行任务的场景下,本文给大家介绍了基于asyncio和threading模块,可扩展的异步定... 目录需求背景代码1. 单例事件循环的实现2. 事件循环的运行与关闭3. 定时器核心逻辑4. 启动与停

浅析Java中如何优雅地处理null值

《浅析Java中如何优雅地处理null值》这篇文章主要为大家详细介绍了如何结合Lambda表达式和Optional,让Java更优雅地处理null值,感兴趣的小伙伴可以跟随小编一起学习一下... 目录场景 1:不为 null 则执行场景 2:不为 null 则返回,为 null 则返回特定值或抛出异常场景

鸿蒙中Axios数据请求的封装和配置方法

《鸿蒙中Axios数据请求的封装和配置方法》:本文主要介绍鸿蒙中Axios数据请求的封装和配置方法,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录1.配置权限 应用级权限和系统级权限2.配置网络请求的代码3.下载在Entry中 下载AxIOS4.封装Htt

SpringBoot利用@Validated注解优雅实现参数校验

《SpringBoot利用@Validated注解优雅实现参数校验》在开发Web应用时,用户输入的合法性校验是保障系统稳定性的基础,​SpringBoot的@Validated注解提供了一种更优雅的解... 目录​一、为什么需要参数校验二、Validated 的核心用法​1. 基础校验2. php分组校验3

SpringBoot中封装Cors自动配置方式

《SpringBoot中封装Cors自动配置方式》:本文主要介绍SpringBoot中封装Cors自动配置方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录SpringBoot封装Cors自动配置背景实现步骤1. 创建 GlobalCorsProperties

Java导入、导出excel用法步骤保姆级教程(附封装好的工具类)

《Java导入、导出excel用法步骤保姆级教程(附封装好的工具类)》:本文主要介绍Java导入、导出excel的相关资料,讲解了使用Java和ApachePOI库将数据导出为Excel文件,包括... 目录前言一、引入Apache POI依赖二、用法&步骤2.1 创建Excel的元素2.3 样式和字体2.