react封装全局弹框的方法

 更新时间:2021年10月15日 16:35:55   作者:冰丫头  
这篇文章主要为大家详细介绍了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 = {
  alertStatus: false,
  alertTip: null,
  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(
      {
        alertStatus: false
      },
      () => {
        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;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: rgba(255, 255, 255, 0.3);
  z-index: 222;
}
.alert-context {
  // background-color: #fff;
  // border-radius: 16px;
  position: relative;
  // height: 500px;
  height: 90%;
  width: 750px;
  margin: 40px auto 0;
  background: url(../images/alertBJ.png) no-repeat center;
  background-size: 100% 100%;
  .alert-close{
    width: 30px;
    height: 30px;
    position: absolute;
    right: 30px;
    top: 30px;
  }
  .alert-content-title{
    width: 100%;
    height: 80px;
    line-height: 80px;
    color: #fff;
    text-align: center;
    font-size: 36px;
    font-weight: bolder;
    // background: url(../images/line.png) no-repeat left bottom;
  }
  .alert-content-line{
    width: 100%;
    height: 20px;
    margin-top: -44px;
    margin-left: -6px;
  }
  .alert-detais-list{
    width: 102%;
    height: 100%;
    overflow-y: auto;
    padding: 20px 60px;
    .alert-detais-list-C{
      p{
        &:nth-child(1){
          font-size: 14px;
          line-height: 20px;
          color: #FFFFFF;
          letter-spacing: 1.4px;
        }
        &:nth-child(2){
          line-height: 24px;
          font-size: 18px;
          color: #FFFFFF;
        }
      }
    }
  }
  .alert-content-detail{
    // height: 100%;
    height: calc(100% - 100px);
    /* overflow-y: auto; */
    overflow: hidden;
    width: 98%;
    margin-top: -26px;
  }
  .alert-details-pdf{
    width: 102%;
    height: 100%;
    overflow-y: auto;
    padding: 20px 60px;
    .alert-details-button{
      display: flex;
      flex-direction: row;
      justify-content: flex-end;
      margin-bottom: 10px;
      p{
        color:#fff;
        line-height: 35px;
        font-size: 16px;
        margin-right: 20px;
      }
      a{
        line-height: 35px;
        font-size: 16px;
        margin-right: 20px;
      }
    }
  }
.cameraWrap{
  width: 100%;
  height: 102%;
  box-sizing: border-box;
  padding: 12px 4px 0 14px;
}
}

用法

  • alertTitle 弹框标题
  • alertTip 弹框内容,样式自己自定义
  • closeAlert 关闭时候返回信息,可要可不要,根据自己需求。
import Toptips from "./Toptips"
Toptips.open({
      alertTitle: '批示详情',
      alertTip: that.htms(val),
      closeAlert: function () {
        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通过hook实现封装表格常用功能

    React通过hook实现封装表格常用功能

    这篇文章主要为大家详细介绍了React通过hook封装表格常用功能的使用,文中的示例代码讲解详细,具有一定的借鉴价值,有需要的小伙伴可以参考下
    2023-12-12
  • 关于React动态修改元素样式的三种方式

    关于React动态修改元素样式的三种方式

    这篇文章主要介绍了关于React动态修改元素样式的三种方式,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-08-08
  • react-native 实现购物车滑动删除效果的示例代码

    react-native 实现购物车滑动删除效果的示例代码

    这篇文章主要介绍了react-native 实现购物车滑动删除效果的示例代码,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2021-01-01
  • React简便获取经纬度信息的方法详解

    React简便获取经纬度信息的方法详解

    在现代的Web应用程序中,获取用户的地理位置信息是一项常见的需求,本文我们将介绍如何在React应用程序中简便地获取用户的经纬度信息,需要的可以参考下
    2023-11-11
  • React中useEffect Hook常见问题及解决

    React中useEffect Hook常见问题及解决

    React的useEffect Hook它允许我们在函数组件中执行副作用操作,但在使用过程中可能会遇到一些常见的错误,本文就来介绍一下常见问题及解决,感兴趣的可以了解一下
    2023-10-10
  • React native ListView 增加顶部下拉刷新和底下点击刷新示例

    React native ListView 增加顶部下拉刷新和底下点击刷新示例

    这篇文章主要介绍了React native ListView 增加顶部下拉刷新和底下点击刷新示例,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-04-04
  • React如何以Hook的方式使用Echarts

    React如何以Hook的方式使用Echarts

    这篇文章主要介绍了React如何以Hook的方式使用Echarts问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2024-03-03
  • React commit源码分析详解

    React commit源码分析详解

    前两章讲到了,react 在 render 阶段的 completeUnitWork 执行完毕后,就执行 commitRoot 进入到了 commit 阶段,本章将讲解 commit 阶段执行过程源码
    2022-11-11
  • React的事件处理你了解吗

    React的事件处理你了解吗

    这篇文章主要为大家详细介绍了React的事件处理,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下,希望能够给你带来帮助
    2022-03-03
  • react.js使用webpack搭配环境的入门教程

    react.js使用webpack搭配环境的入门教程

    本文主要介绍了react 使用webpack搭配环境的入门教程,具有一定的参考价值,感兴趣的小伙伴们可以参考一下。
    2017-08-08

最新评论