Modal.js 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. 'use strict';
  2. const React = require('react/addons');
  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={'modal-mask' + (data.get('open') ? '' : ' hidden')}>
  21. <p>
  22. <strong>Esc: </strong>
  23. <span>{type === 'info' ? 'OK' : 'Decline'}</span>
  24. <br />
  25. <strong>Enter: </strong>
  26. <span>{type === 'info' ? 'OK' : 'Accept'}</span>
  27. </p>
  28. <div className="modal">
  29. <p>{data.get('message')}</p>
  30. {type === 'info' ?
  31. <a className="btn ok"
  32. onClick={callbacks.hide}>
  33. OK
  34. </a> : [
  35. <a key="a"
  36. className="btn"
  37. style={{left: '4em'}}
  38. onClick={callbacks.accept}>
  39. Accept
  40. </a>,
  41. <a key="b"
  42. className="btn btn--red"
  43. style={{right: '4em'}}
  44. onClick={callbacks.decline}>
  45. Decline
  46. </a>
  47. ]}
  48. </div>
  49. </div>
  50. );
  51. },
  52. _onKeydown(e) {
  53. const type = this.props.data.get('type');
  54. const callbacks = this.props.data.get('callbacks');
  55. if (type === 'info') {
  56. if (e.which === 13 || e.which === 27) {
  57. callbacks.hide();
  58. }
  59. } else if (type === 'offer') {
  60. if (e.which === 13) {
  61. callbacks.accept();
  62. } else if (e.which === 27) {
  63. callbacks.decline();
  64. }
  65. }
  66. }
  67. });
  68. module.exports = Modal;