Modal组件

长话不多说,接下来让我们来动手实现一个react Modal组件。

我们先来看一下实际效果

Modal的布局

首先,让我们先思考下一个Modal组件的布局是怎么样的。

我们先拿一个基本的Modal样例来分析下。

如上图所示,一个Modal组件可以分为mask、header、body和footer四部分,mask就不用说了,header主要是显示title和关闭按钮,body则是使用者自己传的内容,footer主要是按钮控件。

Modal组件的参数(props)

我们确定了Modal组件的布局之后,我们来思考一下Modal组件可支持传递的参数。

作为一个Modal组件,总要有标题(title)吧?要有用户自定义传入的内容(children),还有一个确定按钮文案(okText)和一个取消按钮文案(cancelText)吧,并且允许用户传入点击确定按钮的回调函数(onOk)和点击取消按钮的回调函数(onCancel)。也需要有一个控制Modal是否显示的标志吧(visible)。所以,大体上有以下7个变量。

Modal的样式

首先,根据Modal组件的布局和参数,我们可以确定react Modal的render函数如下:

我们都知道,Modal会覆盖在其他元素上面,并且主要分为两部分,一部分为mask阴影部分,一部分为主体内容,而且主体部分会覆盖在阴影部分上面。让我们一步步来实现这个效果。

  1. 实现mask效果

    .modal-mask {
    // 让mask铺满整屏
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: black;
    opacity: 0.6;
    // 让mask覆盖在其他元素上面
    z-index: 1000;
    }
  2. 实现主体内容的样式,让其覆盖在其他元素(包括mask)上面,每一部分的作用可以看注释

    .modal-container {
    // 让Modal的主体内容全局居中,通过position: fix以及top和left的50%让主体内容的左上角居中,再通过transform:translate(-50%, -50%)来让主体内容正确居中。
    position: fixed;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%); background: white;
    min-width: 500px;
    border-radius: 4px;
    // 设置主体内容的z-index高于mask的,从而可以覆盖mask
    z-index: 1001;
    }
  3. 接下来是body、footer和header样式的实现,这个就直接贴代码了。

    .modal-title {
    padding: 30px;
    color: black;
    font-size: 20px;
    border-bottom: 1px solid #e8e8e8;
    } .modal-body {
    padding: 30px;
    font-size: 14px;
    border-bottom: 1px solid #e8e8e8;
    } .modal-footer {
    text-align: center;
    padding: 30px;
    display: flex;
    } .modal-footer .btn {
    flex: 1;
    height: 32px;
    text-align: center;
    } .modal-footer .modal-cancel-btn {
    background: white;
    margin-right: 30px;
    border-color: #d9d9d9;
    border-radius: 4px;
    } .modal-footer .modal-confirm-btn {
    background: #1890ff;
    color: white;
    }

Modal的交互逻辑实现

实际上Modal的交互是很简单的,一般的调用方式如下:

由外部传递自定义的body内容以及一些自定义的属性(比如title,点击按钮的回调还有Modal的标题)

  1. 我们先定义Modal组件里的props

  2. 设置一些默认的props,当用户未传入参数的时候,则使用默认的props

  3. 实现render函数,根据用户传入的参数以及默认参数来渲染Modal节点,如果用户传入的visible属性为false(Modal不可见),则返回null,否则,返回Modal节点。

这样,一个简单的react Modal组件就完成了,上面的代码可以在https://github.com/chenjigeng/empty 查看,并且可以直接看到一个demo例子。

效果图如下:

最后再贴一下完整的Modal组件代码

// Modal.tsx
import * as React from 'react';
import './Modal.css'; interface IModalProps {
children: React.ReactChild | React.ReactChildren | React.ReactElement<any>[],
title?: React.ReactChild,
visible: boolean,
onOk?: () => void,
onCancel?: () => void,
okText?: string,
cancelText?: string,
} export default class Modal extends React.Component<IModalProps> { public static defaultProps = {
cancelText: '取消',
okText: '确定',
visible: false,
} public render() {
const { title, visible, okText, cancelText, children, onOk, onCancel } = this.props;
if (!visible) {
return null;
};
return (
<div>
<div className="modal-mask" onClick={onCancel}/>
<div className="modal-container">
<div className="modal-header">
<div className="modal-title">{title}</div>
</div>
<div className="modal-body">
{children}
</div>
<div className="modal-footer">
<button className="modal-cancel-btn btn" onClick={onCancel}>{cancelText}</button>
<button className="modal-confirm-btn btn" onClick={onOk}>{okText}</button>
</div>
</div>
</div>
)
}
}
// Moda.css
.modal-mask {
position: fixed;
top: 0;
left: 0;
right: 0;
bottom: 0;
background: black;
opacity: 0.6;
z-index: 1000;
} .modal-container {
position: fixed;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
background: white;
min-width: 500px;
border-radius: 4px;
z-index: 1001;
} .modal-title {
padding: 30px;
color: black;
font-size: 20px;
border-bottom: 1px solid #e8e8e8;
} .modal-body {
padding: 30px;
font-size: 14px;
border-bottom: 1px solid #e8e8e8;
} .modal-footer {
text-align: center;
padding: 30px;
display: flex;
} .modal-footer .btn {
flex: 1;
height: 32px;
text-align: center;
} .modal-footer .modal-cancel-btn {
background: white;
margin-right: 30px;
border-color: #d9d9d9;
border-radius: 4px;
} .modal-footer .modal-confirm-btn {
background: #1890ff;
color: white;
}

本文地址在->本人博客地址, 欢迎给个 start 或 follow

动手实现react Modal组件的更多相关文章

  1. 实现一个带有动效的 React 弹窗组件

    我们在写一些 UI 组件时,若不考虑动效,就很容易实现,主要就是有无的切换(类似于 Vue 中的 v-if 属性)或者可见性的切换(类似于 Vue 中的 v-show 属性). 1. 没有动效的弹窗 ...

  2. React对话框组件实现

    当下前端届最火的技术之一莫过于React + Redux + webpack的技术结合.最近公司内部也正在转react,这周主要做了个React的modal组件,接下来谈下具体实现过程. 基本的HTM ...

  3. beeshell —— 开源的 React Native 组件库

    介绍 beeshell 是一个 React Native 应用的基础组件库,基于 0.53.3 版本,提供一整套开箱即用的高质量组件,包含 JavaScript(以下简称 JS)组件和复合组件(包含 ...

  4. React实现组件全屏化

    介绍 本文基于React+antd,给大家演示一个完整的全屏demo. 起因是开发今天给我提了一个sql编辑器输入框比较小,不支持放大,不太方便.希望能够全屏显示,联想到自己以后可能也会需要,便研究并 ...

  5. React 函数组件

    React 函数组件 1.定义方式 React 函数组件是指使用函数方法定义的组件. 定义方式:与函数的定义方式相同,需要将内容 return 出来,需要注意的是最外层只有一个标签或者使用<&g ...

  6. 移动web端的react.js组件化方案

     背景: 随着互联网世界的兴起,web前端开发的方式越来越多,出现了很多种场景开发的前端架构体系,也对前端的要求日益增高,早已经不是靠一个JQuery.js来做前端页面的时代了,而今移动端变化最大,近 ...

  7. 利用bootstrap的modal组件自定义alert,confirm和modal对话框

    由于浏览器提供的alert和confirm框体验不好,而且浏览器没有提供一个标准的以对话框的形式显示自定义HTML的弹框函数,所以很多项目都会自定义对话框组件.本篇文章介绍自己在项目中基于bootst ...

  8. Griddle, griddle-react 一个REACT 表格组件

    Griddle, griddle-react 一个REACT 表格组件: http://griddlegriddle.github.io/Griddle/index.html

  9. React Native组件之Text

    React Native组件之Text相当于iOS中的UILabel. 其基本属性如下: /** * Sample React Native App * https://github.com/face ...

随机推荐

  1. java多线程系列15 设计模式 生产者 - 消费者模式

    生产者-消费者 生产者消费者模式是一个非常经典的多线程模式,比如我们用到的Mq就是其中一种具体实现 在该模式中 通常会有2类线程,消费者线程和生产者线程 生产者提交用户请求 消费者负责处理生产者提交的 ...

  2. ABP框架系列之四十七:(SignalR-Integration-SignalR-集成)

    Introduction Abp.Web.SignalR nuget package makes it easily to use SignalR in ASP.NET Boilerplate bas ...

  3. UnionFind问题总结

    UnionFind就是acm中常用的并查集... 并查集常用操作 另外补充一下STL常用操作 相关问题: 547. Friend Circles 纯裸题噢... class Solution { pu ...

  4. LwIP协议栈接口

    协议栈api函数 1.netconn_new      //UDP    TCP struct netconn*netconn_new(enum netconn_type t) 为新连接申请一个连接结 ...

  5. leetcode记录

    2019 1月31: 141交叉链表, 2月: 2/1: 160环形链表 ,              思路记得,但是指针里面逻辑搞错,这里不是用快慢指针而是同时的指针.:复习了141题还是有问题,把 ...

  6. springBoot基础

    开始之前最基础的东东here 官网:http://projects.spring.io/spring-boot/ 基础快速构建:http://start.spring.io/ 松哥的博客:http:/ ...

  7. Nginx+Tomcat反向代理利用certbot实现https

    一.利用Let's Encrypt 免费生成HTTPS证书 1.下载安装certbot(Let's Encrypt ) 2.利用certbot生成证书 3.配置nginx的https证书 安装cerb ...

  8. ubuntu下chrome浏览器安装flash插件(pepperflashplugin-nonfree)

    安装前说明: ubuntu的Google 已经不能使用Adobe Flash了,需要用PepperFlashPlayer来替代 Adobe Flash才行. 安装步骤: 1.安装pepperflash ...

  9. GitHub 轻松提速教程

    通过修改hosts文件来提速,获取github的IP地址 访问:https://www.ipaddress.com/ 网址 依次获取以下三个网址的IP github.com github.global ...

  10. Request模块—数据解析工具

    一.爬虫基本步骤 指定URL信息 发起请求 获取响应数据 对响应数据进行数据解析 持久化存储 二.数据解析 1. 正则表达式 (1) 基本语法 1. 单字符: . : 除换行以外所有字符 [] :[a ...