You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

AbstractDialog.js 2.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. import React, { Component } from 'react';
  2. import { hideDialog } from '../actions';
  3. /**
  4. * Abstract dialog to display dialogs.
  5. */
  6. export default class AbstractDialog extends Component {
  7. /**
  8. * Abstract Dialog component's property types.
  9. *
  10. * @static
  11. */
  12. static propTypes = {
  13. /**
  14. * Whether cancel button is disabled. Enabled by default.
  15. */
  16. cancelDisabled: React.PropTypes.bool,
  17. /**
  18. * Optional i18n key to change the cancel button title.
  19. */
  20. cancelTitleKey: React.PropTypes.string,
  21. /**
  22. * Used to show hide the dialog on cancel.
  23. */
  24. dispatch: React.PropTypes.func,
  25. /**
  26. * Is ok button enabled/disabled. Enabled by default.
  27. */
  28. okDisabled: React.PropTypes.bool,
  29. /**
  30. * Optional i18n key to change the ok button title.
  31. */
  32. okTitleKey: React.PropTypes.string,
  33. /**
  34. * The handler for onCancel event.
  35. */
  36. onCancel: React.PropTypes.func,
  37. /**
  38. * The handler for the event when submitting the dialog.
  39. */
  40. onSubmit: React.PropTypes.func,
  41. /**
  42. * Used to obtain translations in children classes.
  43. */
  44. t: React.PropTypes.func,
  45. /**
  46. * Key to use for showing a title.
  47. */
  48. titleKey: React.PropTypes.string
  49. }
  50. /**
  51. * Initializes a new Dialog instance.
  52. *
  53. * @param {Object} props - The read-only properties with which the new
  54. * instance is to be initialized.
  55. */
  56. constructor(props) {
  57. super(props);
  58. this._onCancel = this._onCancel.bind(this);
  59. this._onSubmit = this._onSubmit.bind(this);
  60. }
  61. /**
  62. * Dispatches action to hide the dialog.
  63. *
  64. * @returns {void}
  65. */
  66. _onCancel() {
  67. let hide = true;
  68. if (this.props.onCancel) {
  69. hide = this.props.onCancel();
  70. }
  71. if (hide) {
  72. this.props.dispatch(hideDialog());
  73. }
  74. }
  75. /**
  76. * Dispatches the action when submitting the dialog.
  77. *
  78. * @private
  79. * @param {string} value - The submitted value if any.
  80. * @returns {void}
  81. */
  82. _onSubmit(value) {
  83. let hide = true;
  84. if (this.props.onSubmit) {
  85. hide = this.props.onSubmit(value);
  86. }
  87. if (hide) {
  88. this.props.dispatch(hideDialog());
  89. }
  90. }
  91. }