Skip to content

Instantly share code, notes, and snippets.

@ayozebarrera
Created December 15, 2015 10:35
Show Gist options
  • Save ayozebarrera/9d5f31ccfdb89490854c to your computer and use it in GitHub Desktop.
Save ayozebarrera/9d5f31ccfdb89490854c to your computer and use it in GitHub Desktop.
A wrapper component that embraces your button passing his click event to the confirm accept buttons.
import React from 'react';
import ReactDOM from 'react-dom';
class ConfirmChild extends React.Component {
constructor() {
super();
this.state = {
isOpen: false
};
this.closeModal = this.closeModal.bind(this);
this.openModal = this.openModal.bind(this);
this.sendModal = this.sendModal.bind(this);
}
getStyles() {
let styles = {
background: {
width: '100%',
height: '100%',
position: 'absolute',
background: 'rgba(0,0,0,.4)',
left: 0,
top: 0,
padding: '10px 0',
boxSizing: 'border-box',
overflow: 'hidden',
zIndex: 999
},
modal: {
width: '80%',
minHeight: 200,
margin: '0 auto',
padding: '50px 15px 95px 15px',
boxShadow: '0 1px 6px rgba(0, 0, 0, 0.12), 0 1px 4px rgba(0, 0, 0, 0.24)',
borderRadius: 2,
background: '#fff',
boxSizing: 'border-box',
overflow: 'hidden',
display: 'flex',
flexDirection: 'column',
position: 'relative',
top: '50%',
transform: 'translateY(-50%)'
},
buttons: {
textAlign: 'center',
flex: 'none',
position: 'absolute',
width: '100%',
bottom: 50
},
buttonCancel: {
margin: '0 5px',
display: 'inline-block',
boxShadow: '0 1px 6px rgba(0, 0, 0, 0.12), 0 1px 4px rgba(0, 0, 0, 0.24)',
borderRadius: 2,
minWidth: 80,
background: '#fff',
padding: '7px 10px',
cursor: 'pointer'
},
buttonSubmit: {
margin: '0 5px',
display: 'inline-block',
boxShadow: '0 1px 6px rgba(0, 0, 0, 0.12), 0 1px 4px rgba(0, 0, 0, 0.24)',
borderRadius: 2,
minWidth: 80,
background: '#00C853',
color: '#fff',
padding: '7px 10px',
cursor: 'pointer'
},
msg: {
textAlign: 'center'
}
};
return styles;
}
openModal() {
const { acceptLabel, cancelLabel, msg } = this.props;
let styles = this.getStyles();
this.modal = document.createElement('div');
this.modal.className = 'modal';
let btnCancel, btnSubmit;
let modal = null;
btnCancel = (<div style={styles.buttonCancel} onClick={this.closeModal}>{cancelLabel}</div>);
btnSubmit = (<div style={styles.buttonSubmit} onClick={this.sendModal}>{acceptLabel}</div>);
modal = (
<div style={styles.background}>
<div style={styles.modal}>
<div style={styles.msg}>{msg}</div>
<div style={styles.buttons}>
{btnCancel}
{btnSubmit}
</div>
</div>
</div>
);
document.body.appendChild(this.modal);
ReactDOM.render(modal, this.modal);
}
closeModal() {
this.modal.parentNode.removeChild(this.modal);
}
sendModal() {
const { children } = this.props;
children.props.onClick();
this.closeModal();
}
componentDidMount() {
document.addEventListener('keydown', this.handleListener, false);
}
componentWillUnmount() {
document.removeEventListener('keydown', this.handleListener, false);
}
handleListener(e) {
var keyCode = e.keyCode;
if(keyCode == 13) {
this.sendModal();
}
}
render() {
const { children } = this.props;
let clone = React.cloneElement(children, {
onClick: this.openModal
});
return (clone);
}
}
ConfirmChild.propTypes = {
acceptLabel: React.PropTypes.string,
cancelLabel: React.PropTypes.string,
msg: React.PropTypes.string
};
ConfirmChild.defaultProps = {
msg: '¿Está seguro de que desea realizar esta acción?',
acceptLabel: 'Aceptar',
cancelLabel: 'Cancelar'
};
export default ConfirmChild;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment