当前位置: 首页 > news >正文

RN封装的底部向上弹出的弹出层组件

组件代码

import React from 'react';
import { View, StyleSheet, Modal, TouchableOpacity, Text, TouchableWithoutFeedback } from 'react-native';const BottomPopup = ({ visible, onClose, children, leftButtonTitle, rightButtonTitle, onLeftButtonPress, onRightButtonPress }) => {const handleLeftButtonPress = () => {onLeftButtonPress();};const handleRightButtonPress = () => {onRightButtonPress();};return (<ModalanimationType="slide"transparent={true}visible={visible}onRequestClose={onClose}><TouchableWithoutFeedback onPress={onClose}><View style={styles.container}><TouchableWithoutFeedback onPress={() => {}}><View style={styles.popup}><View style={styles.buttonsContainer}><TouchableOpacity onPress={handleLeftButtonPress} style={styles.leftButton}><Text style={styles.leftButtonText}>{leftButtonTitle}</Text></TouchableOpacity><TouchableOpacity onPress={handleRightButtonPress} style={styles.rightButton}><Text style={styles.rightButtonText}>{rightButtonTitle}</Text></TouchableOpacity></View>{children}</View></TouchableWithoutFeedback></View></TouchableWithoutFeedback></Modal>);
};const styles = StyleSheet.create({container: {flex: 1,justifyContent: 'flex-end',backgroundColor: 'rgba(0, 0, 0, 0.5)',},popup: {backgroundColor: '#fff',borderTopLeftRadius: 20,borderTopRightRadius: 20,padding: 20,elevation: 5,},buttonsContainer: {flexDirection: 'row',justifyContent: 'space-between',marginBottom: 10,},leftButton: {padding: 10,},leftButtonText: {color: 'blue',fontSize: 16,},rightButton: {padding: 10,},rightButtonText: {color: 'blue',fontSize: 16,},
});export default BottomPopup;

使用方式

import React, { useState } from 'react';
import { View, Button, Text } from 'react-native';
import BottomPopup from './bbb';const MyComponent = () => {const [isVisible, setIsVisible] = useState(false);const togglePopup = () => {setIsVisible(!isVisible);};const handleCancelButtonPress = () => {setIsVisible(false);console.log('取消按钮被点击');};const handleConfirmButtonPress = () => {setIsVisible(false);console.log('确认按钮被点击');};return (<View style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}><Button title="打开弹出框" onPress={togglePopup} /><BottomPopup visible={isVisible} onClose={togglePopup}leftButtonTitle="取消"onLeftButtonPress={handleCancelButtonPress}rightButtonTitle="确认"onRightButtonPress={handleConfirmButtonPress}><Text>这是弹出框的内容</Text></BottomPopup></View>);
};export default MyComponent;

参数说明

  • visible (boolean): 控制弹出框是否可见。
  • onClose (function): 关闭弹出框的回调函数。
  • leftButtonTitle (string): 左上按钮的标题。
  • onLeftButtonPress (function): 左上按钮点击时触发的回调函数。
  • rightButtonTitle (string): 右上按钮的标题。
  • onRightButtonPress (function): 右上按钮点击时触发的回调函数。

效果图(点击确认或者取消或者阴影部分均可以关闭弹出框)
在这里插入图片描述

http://www.lryc.cn/news/329066.html

相关文章:

  • 基于深度学习YOLOv8+PyQt5的水底海底垃圾生物探测器检测识别系统(源码+数据集+配置说明)
  • SpringBoot集成WebSocket实现简单的多人聊天室
  • 如何使用固定公网地址远程访问内网Axure RP生成的网站原型web页面
  • 蓝桥杯习题
  • AMS概念以及面试相关整理
  • Vmware下减小Ubuntu系统占用系统盘大小
  • 面试题-Elasticsearch集群架构和调优手段(超全面)
  • python基础练习题6
  • Chrome 插件各模块使用 Fetch 进行接口请求
  • 内存可见性
  • Android room 在dao中不能使用挂起suspend 否则会报错
  • 【stable diffusion扩散模型】一篇文章讲透
  • 数据链路层之信道:数字通信的桥梁与守护者
  • SQL109 纠错4(组合查询,order by..)
  • Spring Boot + Vue 实现文件导入导出功能
  • vue watch 深度监听
  • Qt源码调试步骤记录
  • 大数据面试英文自我介绍参考(万字长文)
  • 外包干了5天,技术退步明显.......
  • Docket常见的软件部署1
  • Qt源程序编译及错误问题解决
  • 作业练习(python)
  • Wireshark使用相关
  • 相机标定学习记录
  • CSS 滚动条样式修改
  • 谈谈配置中心?
  • 人工智能(pytorch)搭建模型25-基于pytorch搭建FPN特征金字塔网络的应用场景,模型结构介绍
  • JRT业务开发起步
  • 深度解析:国内主流音视频产品的核心功能与市场表现
  • 红黑树介绍及插入操作的实现