优化删除函数组件

This commit is contained in:
何童崇 2021-08-23 09:41:57 +08:00
parent ba263a28f1
commit d1a3c7b9c3
1 changed files with 31 additions and 63 deletions

View File

@ -1,8 +1,16 @@
/* eslint-disable react/jsx-no-duplicate-props */
import React, { useState } from 'react';
import * as ReactDOM from 'react-dom';
import { Modal, Button } from 'antd';
import './index.scss';
InitModal.defaultProps = {
okText: '确认', //
cancelText: '取消', //
className: '', //
inputId: 'copyText', //ID
};
// 使
export default function DelModal(props) {
renderModal({ ...props, type: 'delete' })
@ -14,11 +22,12 @@ export function Confirm(props) {
}
function renderModal(props) {
const type = props.type;
const { type, afterClose } = props;
const div = document.createElement('div');
document.body.appendChild(div);
function destroy() {
afterClose && afterClose();
const unmountResult = ReactDOM.unmountComponentAtNode(div);
if (unmountResult && div.parentNode) {
div.parentNode.removeChild(div);
@ -27,11 +36,22 @@ function renderModal(props) {
function modalType(type) {
if (type === 'delete') {
return <DeleteModal title="删除页面" contentTitle="确定要删除吗?" afterClose={destroy} {...props} />
return <InitModal
title="删除"
contentTitle="确定要删除吗?"
okText="确认删除"
{...props}
afterClose={destroy}
contentTitle={<React.Fragment>
<i className="red-circle iconfont icon-shanchu_tc_icon mr3"></i>
{props.contentTitle}
</React.Fragment>}
/>
} else if (type === 'confirm') {
return <ConfirmModal title="选择" afterClose={destroy} {...props} />
return <InitModal title="选择" afterClose={destroy} {...props} />
} else {
return <ConfirmModal title="选择" afterClose={destroy} {...props} />
return <InitModal title="选择" afterClose={destroy} {...props} />
}
}
@ -46,17 +66,17 @@ function renderModal(props) {
render();
}
//
function DeleteModal({
//
function InitModal({
onCancel,
onOk,
title,
contentTitle,
content,
afterClose,
okText,
cancelText,
afterClose,
className,
}) {
const [visible, setVisible] = useState(true);
@ -77,66 +97,14 @@ function DeleteModal({
onCancel={onCancelModal}
afterClose={afterClose}
title={title}
className="myself-modal"
className={`myself-modal ${className}`}
centered
footer={[
<Button type="default" key="back" onClick={onCancelModal}>
{cancelText||'取消'}
{cancelText}
</Button>,
<Button className="foot-submit" key="submit" onClick={onSuccess}>
{okText||'确认删除'}
</Button>,
]}
>
<div>
<p className="content-title">
<i className="red-circle iconfont icon-shanchu_tc_icon mr3"></i>
{contentTitle}</p>
<p className="content-descibe">{content}</p>
</div>
</Modal>
)
}
//
function ConfirmModal({
onCancel,
onOk,
title,
contentTitle,
content,
okText,
cancelText,
afterClose,
}) {
const [visible, setVisible] = useState(true);
function onCancelModal() {
setVisible(false);
onCancel && onCancel()
}
function onSuccess() {
setVisible(false);
onOk && onOk();
}
return (
<Modal
visible={visible}
onCancel={onCancelModal}
afterClose={afterClose}
title={title}
className="myself-modal"
centered
footer={[
<Button type="default" key="back" onClick={onCancelModal}>
{cancelText||'取消'}
</Button>,
<Button className="foot-submit" key="submit" onClick={onSuccess}>
{okText||'确认'}
{okText}
</Button>,
]}
>