Modal.js 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. componentDidMount() {
  9. document.addEventListener('keydown', this._onKeydown);
  10. },
  11. componentWillUnmount() {
  12. document.removeEventListener('keydown', this._onKeydown);
  13. },
  14. render() {
  15. let data = this.props.data;
  16. let type = data.get('type');
  17. let callbacks = data.get('callbacks');
  18. return (
  19. <div className={'modal-mask' + (data.get('open') ? '' : ' hidden')}>
  20. <p>
  21. <strong>Esc: </strong>
  22. <span>{type === 'info' ? 'OK' : 'Decline'}</span>
  23. <br />
  24. <strong>Enter: </strong>
  25. <span>{type === 'info' ? 'OK' : 'Accept'}</span>
  26. </p>
  27. <div className="modal">
  28. <p>{data.get('message')}</p>
  29. {type === 'info' ?
  30. <button type="button"
  31. className="btn ok"
  32. onClick={e => {
  33. e.preventDefault();
  34. callbacks.hide();
  35. }}>
  36. OK
  37. </button> : [
  38. <button key="a"
  39. type="button"
  40. className="btn btn--red"
  41. style={{left: '4em'}}
  42. onClick={e => {
  43. e.preventDefault();
  44. callbacks.decline();
  45. }}>
  46. Decline
  47. </button>,
  48. <button key="b"
  49. type="button"
  50. className="btn"
  51. style={{right: '4em'}}
  52. onClick={e => {
  53. e.preventDefault();
  54. callbacks.accept();
  55. }}>
  56. Accept
  57. </button>
  58. ]}
  59. </div>
  60. </div>
  61. );
  62. },
  63. _onKeydown(e) {
  64. let type = this.props.data.get('type');
  65. let callbacks = this.props.data.get('callbacks');
  66. if (type === 'info') {
  67. if (e.which === 13 || e.which === 27) {
  68. callbacks.hide();
  69. }
  70. } else if (type === 'offer') {
  71. if (e.which === 13) {
  72. callbacks.accept();
  73. } else if (e.which === 27) {
  74. callbacks.decline();
  75. }
  76. }
  77. }
  78. });
  79. module.exports = Modal;