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

组件代码

js 复制代码
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();
    onClose();
  };

  const handleRightButtonPress = () => {
    onRightButtonPress();
    onClose();
  };

  return (
    <Modal
      animationType="slide"
      transparent={true}
      visible={visible}
      onRequestClose={onClose}
    >
      <TouchableWithoutFeedback onPress={onClose}>
        <View style={styles.container}>
          <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>
        </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;

使用方式

js 复制代码
import React, { useState } from 'react';
import { View, Button, Text } from 'react-native';
import BottomPopup from './BottomPopup';

const MyComponent = () => {
  const [isVisible, setIsVisible] = useState(false);

  const togglePopup = () => {
    setIsVisible(!isVisible);
  };

  const handleCancelButtonPress = () => {
    console.log('取消按钮被点击');
  };

  const handleConfirmButtonPress = () => {
    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): 右上按钮点击时触发的回调函数。

效果图(点击确认或者取消或者阴影部分均可以关闭弹出框)

相关推荐
梵得儿SHI15 小时前
(第五篇)Spring AI 核心技术攻坚:流式响应与前端集成实现【打字机】效果
前端·webflux·springai·流式响应技术·低延迟ai交互·reactive编程原理·streamapi设计
鹏多多15 小时前
一文搞懂柯里化:函数式编程技巧的解析和实践案例
前端·javascript·vue.js
前端码农一枚15 小时前
前端打包性能优化全攻略
前端
Roc.Chang15 小时前
终极指南:解决 Vue 项目中 “regenerator-runtime/runtime“ 缺失报错
前端·javascript·vue.js·webpack·前端工程
AAA阿giao15 小时前
从树到楼梯:数据结构与算法的奇妙旅程
前端·javascript·数据结构·学习·算法·力扣·
BD_Marathon15 小时前
Vue3组件(SFC)拼接页面
前端·javascript·vue.js
wregjru15 小时前
【C++】2.3 二叉搜索树的实现(附代码)
开发语言·前端·javascript
Hao_Harrision15 小时前
50天50个小项目 (React19 + Tailwindcss V4) ✨ | StickyNavbar(粘性导航栏)
前端·typescript·react·tailwindcss·vite7
LYFlied15 小时前
前端性能优化常见面试问题汇总
前端·面试·性能优化
不爱学习的老登15 小时前
基于CodeServer打造一个属于自己的 LaTeX Web 编辑器
前端·编辑器