React制作页面遮罩

需要安装的组件:react,react-dom,antd

具体代码如下:

遮罩:

import React from 'react';
import ReactDOM from 'react-dom';
import { Button } from 'antd'
import './style.css';

class Dialog extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            show: true
        }
    }

    // 执行回调函数
    okFunc = () => {
        this.closeFunc();
        if (typeof this.props.callbackFunc === "function") {
            this.props.callbackFunc();
        }
    }

    // 关闭弹窗
    closeFunc = () => {
        this.setState({
            show: false
        })
    }

    render() {
        const { title, message, hasCancelBtn = false } = this.props
        return (
            this.state.show ?
                <div className="dialog">
                    <div className="dialog-content">
                        <div className="dialog-title">{title}</div>
                        <div className="dialog-body">{message}</div>
                        <div className="dialog-footer">
                            <Button type="primary" onClick={this.okFunc}>确定</Button>
                            {
                                hasCancelBtn ? <Button type="default" onClick={this.closeFunc}>取消</Button> : null
                            }
                        </div>
                    </div>
                </div> : null
        )
    }
}

const popUp = (title, message, hasCancelBtn, callbackFunc) => {
    let div = document.createElement('div');
    document.body.appendChild(div);
    return ReactDOM.render(
        <Dialog title={title} message={message} hasCancelBtn={hasCancelBtn} callbackFunc={callbackFunc} />, div
    )
}

export default popUp;

样式:

.dialog{
    width: 100%;
    height: 100%;
    z-index: 1000;
    top: 0;
    left: 0;
    background-color: rgba(0, 0, 0, 0.45); /* 0.45 透明度 */
    filter:alpha(opacity=50);
    position:absolute;
}

.dialog-content{
    top: 30%;
    left: 35%;
    z-index: 1001;
    position: absolute;
    background-color: #ffffff;
    width: 550px;
    color: black;
    font-size: large;
    border-radius: 10px;
}

.dialog-title{
    height: 60px;
    line-height: 60px;
    text-align: left;
    padding-left: 35px;
    border-bottom: 1px solid gray;
    border-radius: 10px 10px 0 0;
    background-color: #c9dcd1;
}

.dialog-body{
    height: 120px;
    line-height: 120px;
    text-align: center;
    border-bottom: 1px solid gray;
}

.dialog-footer{
    height: 60px;
    line-height: 60px;
    padding-right: 35px;
    text-align: right;
    border-radius: 10px 10px 0 0;
}

button{
    margin-left: 20px;
}

测试页面:

import React from 'react';
import {Button} from "antd";
import popUp from "./Dialog";

class Test extends React.Component { 

    callbackFunc = () => {
        alert("回调函数执行了...")
    }

    box = () => {
        popUp("Confirme","消息提示!!!",true,this.callbackFunc)
    }
    render(){
        return(
            <div>
                <Button type="primary" onClick={this.box}>点击</Button>
            </div>
        )
    }
 }

 export default Test;

效果:点击确定后会执行回调函数

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值