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.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. * The string to use as a title instead of {@code titleKey}. If a truthy
  51. * value is specified, it takes precedence over {@code titleKey} i.e.
  52. * the latter is unused.
  53. */
  54. titleString: React.PropTypes.string
  55. }
  56. /**
  57. * Initializes a new Dialog instance.
  58. *
  59. * @param {Object} props - The read-only properties with which the new
  60. * instance is to be initialized.
  61. */
  62. constructor(props) {
  63. super(props);
  64. this._onCancel = this._onCancel.bind(this);
  65. this._onSubmit = this._onSubmit.bind(this);
  66. }
  67. /**
  68. * Dispatches action to hide the dialog.
  69. *
  70. * @returns {void}
  71. */
  72. _onCancel() {
  73. let hide = true;
  74. if (this.props.onCancel) {
  75. hide = this.props.onCancel();
  76. }
  77. if (hide) {
  78. this.props.dispatch(hideDialog());
  79. }
  80. }
  81. /**
  82. * Dispatches the action when submitting the dialog.
  83. *
  84. * @private
  85. * @param {string} value - The submitted value if any.
  86. * @returns {void}
  87. */
  88. _onSubmit(value) {
  89. let hide = true;
  90. if (this.props.onSubmit) {
  91. hide = this.props.onSubmit(value);
  92. }
  93. if (hide) {
  94. this.props.dispatch(hideDialog());
  95. }
  96. }
  97. }