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

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

相关推荐
万少6 分钟前
万少的 Claude Code 入门教程
前端·人工智能·后端
এ慕ོ冬℘゜15 分钟前
JS 前端基础高频面试题
开发语言·前端·javascript
放下华子我只抽RuiKe518 分钟前
React 从入门到生产(八):测试与部署
前端·javascript·深度学习·react.js·前端框架·ecmascript·集成学习
蜡笔小电芯25 分钟前
【Electron】第2章—BrowserWindow 与 Electron 窗口机制
前端·javascript·electron
zhangxingchao29 分钟前
AI 大模型面试核心二:微调、RAG、MCP、Agent 与工程落地
前端·人工智能·后端
ZC跨境爬虫30 分钟前
跟着 MDN 学CSS day_15:(掌握CSS背景与边框的创造性用法)
前端·css·ui·html·tensorflow
zhangxingchao31 分钟前
AI 大模型面试核心三: RAG、Agent 到 Prompt Engineering 的工程化理解
前端·人工智能·后端
Hilaku33 分钟前
从 15MB 减到 800KB,一行 ffmpeg 解决3D 渲染卡顿问题
前端·javascript·程序员
彦为君34 分钟前
JavaSE-11-ByteBuffer(NIO核心组件)
java·开发语言·前端·数据库·后端·spring·nio
丁劲犇38 分钟前
使用TraeAI开发Web页面测试MSYS2 ucrt64 Qt MCP服务器
服务器·前端·c++·qt·mcp