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 分钟前
p5.js 掌握圆锥体 cone
前端·数据可视化·canvas
mazhenxiao9 分钟前
qiankunjs 微前端框架笔记
前端
无羡仙16 分钟前
事件流与事件委托:用冒泡机制优化前端性能
前端·javascript
秃头小傻蛋16 分钟前
Vue 项目中条件加载组件导致 CSS 样式丢失问题解决方案
前端·vue.js
CodeTransfer16 分钟前
今天给大家搬运的是利用发布-订阅模式对代码进行解耦
前端·javascript
阿邱吖18 分钟前
form.item接管受控组件
前端
韩劳模20 分钟前
基于vue-pdf实现PDF多页预览
前端
鹏多多20 分钟前
js中eval的用法风险与替代方案全面解析
前端·javascript
KGDragon20 分钟前
还在为 SVG 烦恼?我写了个 CLI 工具,一键打包,性能拉满!(已开源)
前端·svg
LovelyAqaurius20 分钟前
JavaScript中的ArrayBuffer详解
前端