React encapsulates the global bullet box method

React encapsulates the global bullet box method

This article example shares the specific code of react encapsulation global pop-up box for your reference. The specific content is as follows

Pop-up effect diagram

File Layout

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: 'Details',
  closeAlert: () => {}
}

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

  //css animation component is set to the target component FirstChild = props => {
    const childrenArray = React.Children.toArray(props.children)
    return childrenArray[0] || null
  }

  // Close the pop-up box 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="Close"
            />
          </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: bold;
    // 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;
}
}

usage

  • alertTitle pop-up title
  • alertTip pop-up content, custom style
  • closeAlert returns information when closing, which is optional, depending on your needs.
import Toptips from "./Toptips"
Toptips.open({
      alertTitle: 'Comment details',
      alertTip: that.htms(val),
      closeAlert: function () {
        console.log("Closed...");
      }
    });
  htms = val => {
    return (<div className="alert-detais-list">
      <div className="alert-detais-list-C">
        <p>Comment content:</p>
        <p>{val.fdTitle}</p>
      </div>
      <div className="alert-detais-list-C">
        <p>Comment details:</p>
        <p>{val.fdTitle}</p>
      </div>
      <div className="alert-detais-list-C">
        <p>Measures and Results:</p>
        <p>{val.fdContent}</p>
      </div>
      <div className="alert-detais-list-C">
        <p>Progress details:</p>
        <p></p>
      </div>
    </div>)
  }

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

You may also be interested in:
  • React uses antd form assignment to modify the operation of the pop-up box
  • React Native implements sample code for progress bar pop-up

<<:  In-depth understanding of Mysql logical architecture

>>:  Convert XHTML CSS pages to printer pages

Recommend

Implementation of the Pycharm installation tutorial on Ubuntu 18.04

Method 1: Download Pycharm and install Download a...

How to update, package, and upload Docker containers to Alibaba Cloud

This time, we will try to package the running con...

Vue uses OSS to upload pictures or attachments

Use OSS to upload pictures or attachments in vue ...

Summary of some common techniques in front-end development

1. How to display the date on the right in the art...

Detailed explanation of the working principle and solution of Js modularization

Table of contents 1. Modular concept 2. Modulariz...

Implementation of nginx proxy port 80 to port 443

The nginx.conf configuration file is as follows u...

Installation of CUDA10.0 and problems in Ubuntu

The correspondence between tensorflow version and...

How to use Docker Swarm to build WordPress

cause I once set up WordPress on Vultr, but for w...

Complete steps for uninstalling MySQL database

The process of completely uninstalling the MySQL ...

Use Xshell to connect to the Linux virtual machine on VMware (graphic steps)

Preface: I recently started to study the construc...

Research on the Input Button Function of Type File

<br />When uploading on some websites, a [Se...

Node.js makes a simple crawler case tutorial

Preparation First, you need to download nodejs, w...

How to deploy Vue project under nginx

Today I will use the server nginx, and I also nee...