温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

react封装全局弹框的方法是什么

发布时间:2021-10-15 11:08:33 阅读:224 作者:iii 栏目:开发技术
开发者测试专用服务器限时活动,0元免费领,库存有限,领完即止! 点击查看>>

这篇文章主要讲解了“react封装全局弹框的方法是什么”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“react封装全局弹框的方法是什么”吧!

弹框效果图

react封装全局弹框的方法是什么

文件布局

react封装全局弹框的方法是什么

index.js

/* eslint-disable react/no-render-return-value */
import React, { Component } from 'react'
import { is, fromJS } from 'immutable'
import ReactDOM from 'react-dom'
import './alert.less'

const close = require('../images/guanbi.png')
const line = require('../images/line.png')

const defaultState = {
  alertStatusfalse,
  alertTipnull,
  alertTitle'详情',
  closeAlert() => {}
}

class Toptips extends Component {
  state = {
    ...defaultState
  }

  // css动画组件设置为目标组件
  FirstChild = props => {
    const childrenArray = React.Children.toArray(props.children)
    return childrenArray[0] || null
  }

  // 关闭弹框
  confirm = () => {
    const that = this
    console.log(that)
    this.setState(
      {
        alertStatusfalse
      },
      () => {
        that.state.closeAlert()
      }
    )
  }

  open = data => {
    const options = data || {}
    options.alertStatus = true
    this.setState({
      ...defaultState,
      ...options
    })
  }

  close = () => {
    const that = this
    that.state.closeAlert()
    this.setState({
      ...defaultState
    })
  }

  shouldComponentUpdate = (nextProps, nextState) => {
    return (
      !is(fromJS(this.props), fromJS(nextProps)) ||
      !is(fromJS(this.state), fromJS(nextState))
    )
  }

  render() {
    const { alertStatus, alertTip, alertTitle } = this.state
    console.log(alertTip, alertTitle)
    return (
        <div
          className="alert-con"
          style={alertStatus ? { display: 'block' } : { display: 'none' }}
        >
          <div className="alert-context">
            <div className="alert-content-title">{alertTitle}</div>
            <img className="alert-content-line" src={line} alt="line" />
            <div className="alert-content-detail">{alertTip}</div>
            <img
              role="presentation"
              onClick={() => {
                this.confirm()
              }}
              className="alert-close"
              src={close}
              alt="关闭"
            />
          </div>
        </div>
    )
  }
}

const div = document.createElement('div')
const props = {}
document.body.appendChild(div)

const Box = ReactDOM.render(React.createElement(Toptips, props), div)

export default Box

less

.alert-con {
  position: fixed;
  top0;
  left0;
  width100%;
  height100%;
  backgroundrgba(2552552550.3);
  z-index222;
}
.alert-context {
  // background-color#fff;
  // border-radius16px;
  position: relative;
  // height500px;
  height90%;
  width750px;
  margin40px auto 0;
  backgroundurl(../images/alertBJ.png) no-repeat center;
  background-size100% 100%;
  .alert-close{
    width30px;
    height30px;
    position: absolute;
    right30px;
    top30px;
  }
  .alert-content-title{
    width100%;
    height80px;
    line-height80px;
    color#fff;
    text-align: center;
    font-size36px;
    font-weight: bolder;
    // backgroundurl(../images/line.png) no-repeat left bottom;
  }
  .alert-content-line{
    width100%;
    height20px;
    margin-top: -44px;
    margin-left: -6px;
  }
  .alert-detais-list{
    width102%;
    height100%;
    overflow-y: auto;
    padding20px 60px;
    .alert-detais-list-C{
      p{
        &:nth-child(1){
          font-size14px;
          line-height20px;
          color#FFFFFF;
          letter-spacing1.4px;
        }
        &:nth-child(2){
          line-height24px;
          font-size18px;
          color#FFFFFF;
        }
      }
    }
  }
  .alert-content-detail{
    // height100%;
    heightcalc(100% - 100px);
    /* overflow-y: auto; */
    overflow: hidden;
    width98%;
    margin-top: -26px;
  }
  .alert-details-pdf{
    width102%;
    height100%;
    overflow-y: auto;
    padding20px 60px;
    .alert-details-button{
      display: flex;
      flex-direction: row;
      justify-content: flex-end;
      margin-bottom10px;
      p{
        color:#fff;
        line-height35px;
        font-size16px;
        margin-right20px;
      }
      a{
        line-height35px;
        font-size16px;
        margin-right20px;
      }
    }
  }
.cameraWrap{
  width100%;
  height102%;
  box-sizing: border-box;
  padding12px 4px 0 14px;
}
}

用法

alertTitle 弹框标题
alertTip 弹框内容,样式自己自定义
closeAlert 关闭时候返回信息,可要可不要,根据自己需求。

import Toptips from "./Toptips"
Toptips.open({
      alertTitle'批示详情',
      alertTip: that.htms(val),
      closeAlertfunction () {
        console.log("关闭了...");
      }
    });
  htms = val => {
    return (<div className="alert-detais-list">
      <div className="alert-detais-list-C">
        <p>批示内容:</p>
        <p>{val.fdTitle}</p>
      </div>
      <div className="alert-detais-list-C">
        <p>批示详述:</p>
        <p>{val.fdTitle}</p>
      </div>
      <div className="alert-detais-list-C">
        <p>措施及结果:</p>
        <p>{val.fdContent}</p>
      </div>
      <div className="alert-detais-list-C">
        <p>进度详情:</p>
        <p></p>
      </div>
    </div>)
  }

感谢各位的阅读,以上就是“react封装全局弹框的方法是什么”的内容了,经过本文的学习后,相信大家对react封装全局弹框的方法是什么这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是亿速云,小编将为大家推送更多相关知识点的文章,欢迎关注!

亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>

向AI问一下细节
推荐阅读:
  1. js选择弹框
  2. layer弹框

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI

开发者交流群×