Modal.js 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. import React from 'react/addons';
  2. import cx from 'classnames';
  3. const Modal = React.createClass({
  4. propTypes: {
  5. data: React.PropTypes.object.isRequired
  6. },
  7. mixins: [React.addons.PureRenderMixin],
  8. componentDidUpdate() {
  9. const isOpen = this.props.data.get('open');
  10. if (isOpen)
  11. document.addEventListener('keydown', this._onKeydown);
  12. else
  13. document.removeEventListener('keydown', this._onKeydown);
  14. },
  15. render() {
  16. const data = this.props.data;
  17. const type = data.get('type');
  18. const callbacks = data.get('callbacks');
  19. return (
  20. <div className={cx({
  21. 'modal-mask': true,
  22. 'hidden': !data.get('open')
  23. })}
  24. onClick={this._hideModal}>
  25. <p>
  26. <strong>Esc: </strong>
  27. <span>{type === 'info' ? 'OK' : 'Decline'}</span>
  28. <br />
  29. <strong>Enter: </strong>
  30. <span>{type === 'info' ? 'OK' : 'Accept'}</span>
  31. </p>
  32. <div className="modal"
  33. onClick={e => e.stopPropagation()}>
  34. <p>{data.get('message')}</p>
  35. {type === 'info' ?
  36. <a className="btn ok"
  37. onClick={callbacks.hide}>
  38. OK
  39. </a> : [
  40. <a key="a"
  41. className="btn"
  42. style={{left: '4em'}}
  43. onClick={callbacks.accept}>
  44. Accept
  45. </a>,
  46. <a key="b"
  47. className="btn btn--red"
  48. style={{right: '4em'}}
  49. onClick={callbacks.decline}>
  50. Decline
  51. </a>
  52. ]}
  53. </div>
  54. </div>
  55. );
  56. },
  57. _onKeydown(e) {
  58. const type = this.props.data.get('type');
  59. const callbacks = this.props.data.get('callbacks');
  60. if (type === 'info') {
  61. if (e.which === 13 || e.which === 27) {
  62. callbacks.hide();
  63. }
  64. } else if (type === 'offer') {
  65. if (e.which === 13) {
  66. callbacks.accept();
  67. } else if (e.which === 27) {
  68. callbacks.decline();
  69. }
  70. }
  71. },
  72. _hideModal() {
  73. this.props.data.get('callbacks').hide();
  74. }
  75. });
  76. export default Modal;