最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

react實現(xiàn)全局組件確認彈窗

 更新時間:2022年08月23日 14:29:22   作者:云幻★辰  
這篇文章主要為大家詳細介紹了react實現(xiàn)全局組件確認彈窗,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下

本例基于react實現(xiàn)了一個簡單的確認彈窗,可以讓我們在項目中根據(jù)需要隨時調(diào)用

創(chuàng)建全局modal組件

此處我將彈窗模態(tài)框獨立出來,用戶可以通過傳入組件或Element來填充模態(tài)框的內(nèi)容。

import ReactDOM from 'react-dom';
import React, { Fragment } from 'react';
import Button from 'components/common/button';
import Icon from 'components/common/icon';
import css from './index.less';

export interface Props {
? isCancelIcon?: boolean;
? cancelText?: string;
? okText?: string;
? onCancel?: () => void;
? onOk?: () => void;
? footer?: React.ReactNode;
? style?: object;
}

const Modal: React.FC<Props> = React.memo<Props>(({
? isCancelIcon, cancelText, okText, onOk, onCancel, footer, children, style
}) => {

? function renderBtn() {
? ? return (
? ? ? <Fragment>
? ? ? ? <Button?
? ? ? ? ? className={css.btn}
? ? ? ? ? onClick={() => onCancel()}
? ? ? ? >
? ? ? ? ? {cancelText}
? ? ? ? </Button>
? ? ? ? <Button?
? ? ? ? ? className={css.btn}?
? ? ? ? ? onClick={() => onOk()}?
? ? ? ? ? type="primary">
? ? ? ? ? {okText}
? ? ? ? </Button>
? ? ? </Fragment>
? ? );
? }

? return (
? ? <div className={css.masker}>
? ? ? <div className={css.box} style={{ ...style }} >
? ? ? ? {isCancelIcon &&?
? ? ? ? <div?
? ? ? ? ? className={css.cancelIconBox}
? ? ? ? ? onClick={() => onCancel()}
? ? ? ? >
? ? ? ? ? <Icon className={css.cancelIcon} />
? ? ? ? </div>}
? ? ? ? {children}
? ? ? ? <div className={css.btnBox}>
? ? ? ? ? {footer || renderBtn()}
? ? ? ? </div>
? ? ? </div>
? ? </div>
? );
});

Modal.defaultProps = {
? okText: '確定',
? cancelText: '取消',
? onCancel: () => {},
? onOk: () => {},
? isCancelIcon: true
};

export default function ({ content, props }: { content: React.ReactNode; props: Props }) {
??
? const { onOk=() => {}, onCancel=() => {}, ...others } = props;
? /**
? ?* 取消操作,關(guān)閉彈窗
? ?*/
? function handleClose() {
? ? ReactDOM.unmountComponentAtNode(div);
? ? document.body.removeChild(div);
? ? onCancel();
? }
? /**
? ?* 確認操作,關(guān)閉彈窗
? ?*/
? function handleOk() {
? ? ReactDOM.unmountComponentAtNode(div);
? ? document.body.removeChild(div);
? ? onOk();
? }

? let div = document.createElement('div');
? document.body.appendChild(div);
? ReactDOM.render(
? ? <Modal?
? ? ? {...others}
? ? ? onOk={handleOk}?
? ? ? onCancel={handleClose}
? ? >
? ? ? {content}
? ? </Modal>,
? ? div);

? return {
? ? handleOk: () => handleOk(),
? ? handleClose: () => handleClose()
? };
}

less文件

@import '~common/less/index.less';
? .masker{
? ? width: 100vw;
? ? height: 100vh;
? ? background: @mask-color;
? ? position: fixed;
? ? left: 0;
? ? top: 0;
? ? display: flex;
? ? justify-content: center;
? ? flex-direction: column;
? ? align-items: center;
? ? overflow: hidden;
? ? .box{
? ? ? width: 500px;
? ? ? height: 230px;
? ? ? position: relative;
? ? ? background-color: white;
? ? ? position: relative;
? ? ? transition: .2s;
? ? ? animation: showModal .2s ease-in forwards;
? ? ? .cancelIconBox{
? ? ? ? width: 27px;
? ? ? ? height: 27px;
? ? ? ? line-height: 27px;
? ? ? ? text-align: center;
? ? ? ? position: absolute;
? ? ? ? right: 15px;
? ? ? ? top: 22px;
? ? ? ? cursor: pointer;
? ? ? ? .cancelIcon{
? ? ? ? ? .font(17px)
? ? ? ? }
? ? ? }
? ? ? .btnBox{
? ? ? ? width: 100%;
? ? ? ? position: absolute;
? ? ? ? left: 0;
? ? ? ? bottom: 20px;
? ? ? ? padding-right: 10px;
? ? ? ? display: flex;
? ? ? ? flex-flow: row;
? ? ? ? justify-content: flex-end;
? ? ? ? align-items: center;
? ? ? ? ? .btn{
? ? ? ? ? ? margin-right: 10px;
? ? ? ? ? ? .font(12px)
? ? ? ? ? }
? ? ? ? }
? ? }
? }

? @keyframes showModal {
? ? 0%{
? ? ? transform:translate(-100px, 60px) scale(.5) ;
? ? }
? ? 100%{
? ? ? transform:translate(0, 0) scale(1) ?;
? ? }
? }

確認框內(nèi)容組件

此組件用以渲染確認框具體內(nèi)容,項目中可根據(jù)具體情況渲染自己需要的內(nèi)容

tsx文件

import React from 'react';
import classNames from 'classnames';
import Icon from 'components/common/icon';
import modal, { Props as ModalProps } from '../components/modal';
import css from './index.less';

interface Content {
? key: string;
? value: string;
}

export interface Props extends ModalProps {
? title?: string;
? content?: Content[];
}

export default function success({ title, content, ...others }: Props) {

? const node = (
? ? <div className={css.successWrap}>
? ? ? <div className={css.line} />
? ? ? <div className={css.content}>
? ? ? ? <Icon className={css.successIcon} />
? ? ? ? <div className={css.right}>
? ? ? ? ? <span className={css.successTitle}>{title}</span>
? ? ? ? ? {
? ? ? ? ? ? content && content.map((item, index) => {
? ? ? ? ? ? ? return (
? ? ? ? ? ? ? ? <div key={`content_${index}`} className={css.contentList}>
? ? ? ? ? ? ? ? ? <div className={css.key}>{item.key}:</div>
? ? ? ? ? ? ? ? ? <span>{item.value}</span>
? ? ? ? ? ? ? ? </div>
? ? ? ? ? ? ? );
? ? ? ? ? ? })
? ? ? ? ? }
? ? ? ? </div>
? ? ? </div>
? ? </div>
? );

? modal({
? ? content: node,
? ? props: others
? });
}

less文件

@import '~common/less/index.less';
? .successWrap{
? ? .line{
? ? ? width: 100%;
? ? ? height: 8px;
? ? ? background-color: #6EA204;
? ? }
? ? .content{
? ? ? display: flex;
? ? ? flex-flow: row;
? ? ? margin: 30px 0 0 40px;
? ? ? .successIcon{
? ? ? ? .font(72px);
? ? ? }
? ? ? .right{
? ? ? ? display: flex;
? ? ? ? flex-flow: column;
? ? ? ? padding-top: 10px;
? ? ? ? margin-left: 20px;
? ? ? ? .successTitle{
? ? ? ? ? .contentList();
? ? ? ? ? .font(18px);
? ? ? ? ? font-weight:500;
? ? ? ? }
? ? ? ? .contentList{
? ? ? ? ? display: flex;
? ? ? ? ? flex-flow: row;
? ? ? ? ? .font(12px);
? ? ? ? ? font-weight:400;
? ? ? ? ? color:@font-title-color;
? ? ? ? ? margin-bottom: 7px;
? ? ? ? ? .key{
? ? ? ? ? ? min-width: 72px;
? ? ? ? ? }
? ? ? ? }
? ? ? }
? ? }
? }

使用全局確認框

只需要在組件中引入全局組件,然后直接調(diào)用全局組件中的方法即可。

import React from 'react';
import success from 'components/common/confirm/success';
const content = [
? {?
? ? key: '代理商姓名',
? ? value: '東東東'
? },
? {?
? ? key: '聯(lián)系方式',
? ? value: '18978765678'
? },
? {?
? ? key: '代理商賬號',
? ? value: '這是一個測試登錄用戶名'
? },
? {?
? ? key: '初始密碼',
? ? value: '這是一個測試登錄用戶名'
? },
];
export interface Props {}
const Components: React.FC<Props> = () => {

return (
?<Button onClick={() => success({?
?content, title: '代理商錄入成功',?
?okText: '繼續(xù)錄入',?
?cancelText: '返回列表'?
?})}
?>成功狀態(tài)框</Button>
)

Components.defaultProps = {};

export default Components

實現(xiàn)效果

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • React?狀態(tài)管理中的Jotai詳解

    React?狀態(tài)管理中的Jotai詳解

    Jotai是一個簡單、靈活、高效的React狀態(tài)管理庫,通過原子和派生狀態(tài)的設計,使得狀態(tài)管理變得直觀和高效,它適用于小型應用、組件庫和大型項目的局部狀態(tài)管理,且與TypeScript兼容,Jotai的社區(qū)正在壯大,提供了豐富的資源和支持,感興趣的朋友跟隨小編一起看看吧
    2024-11-11
  • React實現(xiàn)評論的添加和刪除

    React實現(xiàn)評論的添加和刪除

    這篇文章主要為大家詳細介紹了React實現(xiàn)評論的添加和刪除,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-10-10
  • react如何獲取state的值并更新使用

    react如何獲取state的值并更新使用

    這篇文章主要介紹了react如何獲取state的值并更新使用,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-08-08
  • JavaScript中rem布局在react中的應用

    JavaScript中rem布局在react中的應用

    這篇文章主要介紹了JavaScript中rem布局在react中的應用 的相關(guān)資料,需要的朋友可以參考下
    2015-12-12
  • React之PureComponent的使用作用

    React之PureComponent的使用作用

    這篇文章主要介紹了React之PureComponent的使用作用,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-07-07
  • react實現(xiàn)點擊選中的li高亮的示例代碼

    react實現(xiàn)點擊選中的li高亮的示例代碼

    本篇文章主要介紹了react實現(xiàn)選中的li高亮的示例代碼,頁面上有很多個li,要實現(xiàn)點擊到哪個就哪個高亮。小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-05-05
  • React中傳遞組件的三種方式小結(jié)

    React中傳遞組件的三種方式小結(jié)

    通過傳遞組件,我們可以將復雜組件內(nèi)部的一部分 UI 交由外部組件來控制渲染,這也是控制反轉(zhuǎn)(Inversion of Control)的一種體現(xiàn),在 React 中,我們可以通過三種方式來傳遞組件,本文就來給大家述說這三種方式,需要的朋友可以參考下
    2023-07-07
  • 在react中使用windicss的問題

    在react中使用windicss的問題

    這篇文章主要介紹了在react中使用windicss的問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-10-10
  • 從零搭建react+ts組件庫(封裝antd)的詳細過程

    從零搭建react+ts組件庫(封裝antd)的詳細過程

    這篇文章主要介紹了從零搭建react+ts組件庫(封裝antd),實際上,代碼開發(fā)過程中,還有很多可以輔助開發(fā)的模塊、流程,本文所搭建的整個項目,我都按照文章一步一步進行了git提交,開發(fā)小伙伴可以邊閱讀文章邊對照git提交一步一步來看
    2022-05-05
  • es6在react中的應用代碼解析

    es6在react中的應用代碼解析

    這篇文章主要介紹了es6在react中的應用代碼解析,非常不錯,具有參考借鑒價值,需要的朋友可以參考下
    2017-11-11

最新評論

大方县| 德安县| 方山县| 栾川县| 从江县| 湘乡市| 温州市| 伊春市| 河北区| 通渭县| 喀喇| 横峰县| 吕梁市| 剑河县| 长垣县| 出国| 宣威市| 洮南市| 华蓥市| 阿鲁科尔沁旗| 外汇| 伊宁市| 上饶县| 福泉市| 浮山县| 霞浦县| 海门市| 清新县| 平湖市| 牟定县| 万安县| 万盛区| 西藏| 施秉县| 隆林| 瑞昌市| 万州区| 贵州省| 梅河口市| 江门市| 射洪县|