欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

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

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

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

此處我將彈窗模態(tài)框獨(dú)立出來,用戶可以通過傳入組件或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();
? }
? /**
? ?* 確認(rèn)操作,關(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) ?;
? ? }
? }

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

此組件用以渲染確認(rèn)框具體內(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;
? ? ? ? ? }
? ? ? ? }
? ? ? }
? ? }
? }

使用全局確認(rèn)框

只需要在組件中引入全局組件,然后直接調(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)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • react-redux多個組件數(shù)據(jù)共享的方法

    react-redux多個組件數(shù)據(jù)共享的方法

    這篇文章主要介紹了react-redux多個組件數(shù)據(jù)共享的方法,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-08-08
  • React項目中使用Redux的?react-redux

    React項目中使用Redux的?react-redux

    這篇文章主要介紹了React項目中使用Redux的?react-redux,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價值,需要的小伙伴可以參考一下
    2022-09-09
  • react中如何使用局部樣式

    react中如何使用局部樣式

    這篇文章主要介紹了react中如何使用局部樣式問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2023-10-10
  • React深入分析useEffect源碼

    React深入分析useEffect源碼

    useEffect是react?v16.8新引入的特性。我們可以把useEffect?hook看作是componentDidMount、componentDidUpdate、componentWillUnmounrt三個函數(shù)的組合
    2022-11-11
  • 使用React?Hooks模擬生命周期的實現(xiàn)方法

    使用React?Hooks模擬生命周期的實現(xiàn)方法

    這篇文章主要介紹了使用React?Hooks模擬生命周期,本文舉例說明如何使用 hooks 來模擬比較常見的 class 組件生命周期,需要的朋友可以參考下
    2023-02-02
  • React Router V5:使用HOC組件實現(xiàn)路由攔截功能

    React Router V5:使用HOC組件實現(xiàn)路由攔截功能

    這篇文章主要介紹了React Router V5:使用HOC組件實現(xiàn)路由攔截功能,本文通過實例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2023-03-03
  • 詳解在React中跨組件分發(fā)狀態(tài)的三種方法

    詳解在React中跨組件分發(fā)狀態(tài)的三種方法

    這篇文章主要介紹了詳解在React中跨組件分發(fā)狀態(tài)的三種方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-08-08
  • react hooks實現(xiàn)原理解析

    react hooks實現(xiàn)原理解析

    這篇文章主要介紹了react hooks實現(xiàn)原理,文中給大家介紹了useState dispatch 函數(shù)如何與其使用的 Function Component 進(jìn)行綁定,節(jié)后實例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下
    2022-10-10
  • react表單受控的實現(xiàn)方案

    react表單受控的實現(xiàn)方案

    數(shù)據(jù)的受控控制一直是react里的一個痛點(diǎn),當(dāng)我想要實現(xiàn)一個輸入框的受控控制時,我需要定義一個onChange和value,手動去實現(xiàn)數(shù)據(jù)的綁定,本文小編給大家介紹了react表單受控的實現(xiàn)方案,需要的朋友可以參考下
    2023-12-12
  • React父組件數(shù)據(jù)實時更新了,子組件沒有更新的問題

    React父組件數(shù)據(jù)實時更新了,子組件沒有更新的問題

    這篇文章主要介紹了React父組件數(shù)據(jù)實時更新了,子組件沒有更新的問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-03-03

最新評論