Created
October 13, 2019 11:20
-
-
Save dedoyle/36391e6ca47e22e42b07446b1e3e12d9 to your computer and use it in GitHub Desktop.
confirm方法组件
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React, { Component } from 'react' | |
import confirm from './confirm.js' | |
class App extends Component { | |
render() { | |
return ( | |
<div> | |
<p>lorem lorem lorem lorem lorem lorem lorem</p> | |
<p>lorem lorem lorem lorem lorem lorem lorem</p> | |
<p>lorem lorem lorem lorem lorem lorem lorem</p> | |
<p>lorem lorem lorem lorem lorem lorem lorem</p> | |
</div> | |
) | |
} | |
async componentDidMount(){ | |
let res = await confirm("确定删除吗") | |
console.log('did mount') | |
console.log(res) | |
if(res) { | |
console.log("是") | |
} else { | |
console.log("否") | |
} | |
} | |
} | |
export default App |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React, { Component } from 'react' | |
import PropTypes from 'prop-types' | |
import ReactDOM from 'react-dom' | |
import './index.css' | |
class Modal extends Component { | |
static propTypes = { | |
content: PropTypes.string, | |
onOk: PropTypes.func, | |
onCancel: PropTypes.func | |
} | |
render() { | |
const { | |
content, | |
onOk, | |
onCancel | |
} = this.props | |
return ( | |
<div className="modal-wrapper"> | |
<div className="modal"> | |
<div className="modal-title">提示</div> | |
<div className="modal-content">{content}</div> | |
<div className="modal-operator"> | |
<button className="modal-operator-close" onClick={onCancel}>取消</button> | |
<button className="modal-operator-confirm" onClick={onOk}>确定</button> | |
</div> | |
</div> | |
<div className="mask"></div> | |
</div> | |
) | |
} | |
} | |
let node = null | |
const hideModal = (node) => { | |
ReactDOM.unmountComponentAtNode(node) | |
document.body.removeChild(node) | |
} | |
const confirm = (content) => { | |
return new Promise((resolve, reject) => { | |
node = document.createElement('div') | |
document.body.appendChild(node) | |
let element = ( | |
<Modal | |
content={content} | |
onOk={ | |
() => { | |
resolve(true) | |
hideModal(node) | |
} | |
} | |
onCancel={ | |
() => { | |
resolve(false) | |
hideModal(node) | |
} | |
} | |
/> | |
) | |
ReactDOM.render(element, node) | |
}) | |
} | |
export default confirm |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
.modal { | |
position: fixed; | |
width: 300px; | |
height: 200px; | |
top: 0; | |
left: 0; | |
right: 0; | |
bottom: 0; | |
margin: auto; | |
border-radius: 5px; | |
background: #fff; | |
overflow: hidden; | |
z-index: 9999; | |
box-shadow: inset 0 0 1px 0 #000; | |
} | |
.modal-title { | |
width: 100%; | |
height: 50px; | |
line-height: 50px; | |
padding: 0 10px; | |
} | |
.modal-content { | |
width: 100%; | |
height: 100px; | |
padding: 0 10px; | |
} | |
.modal-operator { | |
width: 100%; | |
height: 50px; | |
} | |
.modal-operator-close, .modal-operator-confirm { | |
width: 50%; | |
border: none; | |
outline: none; | |
height: 50px; | |
line-height: 50px; | |
opacity: 1; | |
border-top: 1px solid #d9d9d9; | |
color: rgba(0,0,0,0.65); | |
background: #fff; | |
cursor: pointer; | |
} | |
.modal-operator-confirm { | |
border-top: 1px solid #1890ff; | |
color: #fff; | |
background: #1890ff; | |
} | |
.modal-operator-close:active, .modal-operator-confirm:active { | |
opacity: .6; | |
transition: opacity .3s; | |
} | |
.mask { | |
position: fixed; | |
top: 0; | |
left: 0; | |
right: 0; | |
bottom: 0; | |
background: rgba(0,0,0,0.45); | |
z-index: 9998; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment