优化删除函数组件
This commit is contained in:
parent
ba263a28f1
commit
d1a3c7b9c3
|
@ -1,8 +1,16 @@
|
||||||
|
/* eslint-disable react/jsx-no-duplicate-props */
|
||||||
import React, { useState } from 'react';
|
import React, { useState } from 'react';
|
||||||
import * as ReactDOM from 'react-dom';
|
import * as ReactDOM from 'react-dom';
|
||||||
import { Modal, Button } from 'antd';
|
import { Modal, Button } from 'antd';
|
||||||
import './index.scss';
|
import './index.scss';
|
||||||
|
|
||||||
|
InitModal.defaultProps = {
|
||||||
|
okText: '确认', //确定按钮的文字
|
||||||
|
cancelText: '取消', //取消按钮的文字
|
||||||
|
className: '', //
|
||||||
|
inputId: 'copyText', //要复制的文本的ID
|
||||||
|
};
|
||||||
|
|
||||||
// 使用函数调用删除组件
|
// 使用函数调用删除组件
|
||||||
export default function DelModal(props) {
|
export default function DelModal(props) {
|
||||||
renderModal({ ...props, type: 'delete' })
|
renderModal({ ...props, type: 'delete' })
|
||||||
|
@ -14,11 +22,12 @@ export function Confirm(props) {
|
||||||
}
|
}
|
||||||
|
|
||||||
function renderModal(props) {
|
function renderModal(props) {
|
||||||
const type = props.type;
|
const { type, afterClose } = props;
|
||||||
const div = document.createElement('div');
|
const div = document.createElement('div');
|
||||||
document.body.appendChild(div);
|
document.body.appendChild(div);
|
||||||
|
|
||||||
function destroy() {
|
function destroy() {
|
||||||
|
afterClose && afterClose();
|
||||||
const unmountResult = ReactDOM.unmountComponentAtNode(div);
|
const unmountResult = ReactDOM.unmountComponentAtNode(div);
|
||||||
if (unmountResult && div.parentNode) {
|
if (unmountResult && div.parentNode) {
|
||||||
div.parentNode.removeChild(div);
|
div.parentNode.removeChild(div);
|
||||||
|
@ -27,11 +36,22 @@ function renderModal(props) {
|
||||||
|
|
||||||
function modalType(type) {
|
function modalType(type) {
|
||||||
if (type === 'delete') {
|
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') {
|
} else if (type === 'confirm') {
|
||||||
return <ConfirmModal title="选择" afterClose={destroy} {...props} />
|
return <InitModal title="选择" afterClose={destroy} {...props} />
|
||||||
} else {
|
} else {
|
||||||
return <ConfirmModal title="选择" afterClose={destroy} {...props} />
|
return <InitModal title="选择" afterClose={destroy} {...props} />
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -46,61 +66,8 @@ function renderModal(props) {
|
||||||
render();
|
render();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// 删除组件
|
|
||||||
function DeleteModal({
|
|
||||||
onCancel,
|
|
||||||
onOk,
|
|
||||||
title,
|
|
||||||
contentTitle,
|
|
||||||
content,
|
|
||||||
afterClose,
|
|
||||||
okText,
|
|
||||||
cancelText,
|
|
||||||
}) {
|
|
||||||
|
|
||||||
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||'确认删除'}
|
|
||||||
</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({
|
function InitModal({
|
||||||
onCancel,
|
onCancel,
|
||||||
onOk,
|
onOk,
|
||||||
title,
|
title,
|
||||||
|
@ -109,6 +76,7 @@ function ConfirmModal({
|
||||||
okText,
|
okText,
|
||||||
cancelText,
|
cancelText,
|
||||||
afterClose,
|
afterClose,
|
||||||
|
className,
|
||||||
}) {
|
}) {
|
||||||
|
|
||||||
const [visible, setVisible] = useState(true);
|
const [visible, setVisible] = useState(true);
|
||||||
|
@ -129,14 +97,14 @@ function ConfirmModal({
|
||||||
onCancel={onCancelModal}
|
onCancel={onCancelModal}
|
||||||
afterClose={afterClose}
|
afterClose={afterClose}
|
||||||
title={title}
|
title={title}
|
||||||
className="myself-modal"
|
className={`myself-modal ${className}`}
|
||||||
centered
|
centered
|
||||||
footer={[
|
footer={[
|
||||||
<Button type="default" key="back" onClick={onCancelModal}>
|
<Button type="default" key="back" onClick={onCancelModal}>
|
||||||
{cancelText||'取消'}
|
{cancelText}
|
||||||
</Button>,
|
</Button>,
|
||||||
<Button className="foot-submit" key="submit" onClick={onSuccess}>
|
<Button className="foot-submit" key="submit" onClick={onSuccess}>
|
||||||
{okText||'确认'}
|
{okText}
|
||||||
</Button>,
|
</Button>,
|
||||||
]}
|
]}
|
||||||
>
|
>
|
||||||
|
|
Loading…
Reference in New Issue