Modal.js 2.1 KB

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