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.

AbstractNotification.js 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. // @flow
  2. import { Component } from 'react';
  3. import { NOTIFICATION_TYPE } from '../constants';
  4. export type Props = {
  5. /**
  6. * Display appearance for the component, passed directly to the
  7. * notification.
  8. */
  9. appearance: string,
  10. /**
  11. * The text to display in the body of the notification. If not passed
  12. * in, the passed in descriptionKey will be used.
  13. */
  14. defaultTitleKey: string,
  15. /**
  16. * A description string that can be used in addition to the prop
  17. * descriptionKey.
  18. */
  19. description: string,
  20. /**
  21. * The translation arguments that may be necessary for the description.
  22. */
  23. descriptionArguments: Object,
  24. /**
  25. * The translation key to use as the body of the notification.
  26. */
  27. descriptionKey: string,
  28. /**
  29. * Whether the support link should be hidden in the case of an error
  30. * message.
  31. */
  32. hideErrorSupportLink: boolean,
  33. /**
  34. * Whether or not the dismiss button should be displayed.
  35. */
  36. isDismissAllowed: boolean,
  37. /**
  38. * Callback invoked when the user clicks to dismiss the notification.
  39. */
  40. onDismissed: Function,
  41. /**
  42. * Invoked to obtain translated strings.
  43. */
  44. t: Function,
  45. /**
  46. * The text to display at the top of the notification. If not passed in,
  47. * the passed in titleKey will be used.
  48. */
  49. title: string,
  50. /**
  51. * The translation arguments that may be necessary for the title.
  52. */
  53. titleArguments: Object,
  54. /**
  55. * The translation key to display as the title of the notification if
  56. * no title is provided.
  57. */
  58. titleKey: string,
  59. /**
  60. * The unique identifier for the notification.
  61. */
  62. uid: number
  63. };
  64. /**
  65. * Abstract class for {@code Notification} component.
  66. *
  67. * @extends Component
  68. */
  69. export default class AbstractNotification<P: Props> extends Component<P> {
  70. /**
  71. * Default values for {@code Notification} component's properties.
  72. *
  73. * @static
  74. */
  75. static defaultProps = {
  76. appearance: NOTIFICATION_TYPE.NORMAL,
  77. isDismissAllowed: true
  78. };
  79. /**
  80. * Initializes a new {@code Notification} instance.
  81. *
  82. * @param {Object} props - The read-only properties with which the new
  83. * instance is to be initialized.
  84. */
  85. constructor(props: P) {
  86. super(props);
  87. // Bind event handler so it is only bound once for every instance.
  88. this._onDismissed = this._onDismissed.bind(this);
  89. }
  90. _getDescription: () => Array<string>
  91. /**
  92. * Returns the description array to be displayed.
  93. *
  94. * @protected
  95. * @returns {Array<string>}
  96. */
  97. _getDescription() {
  98. const {
  99. description,
  100. descriptionArguments,
  101. descriptionKey,
  102. t
  103. } = this.props;
  104. const descriptionArray = [];
  105. descriptionKey
  106. && descriptionArray.push(t(descriptionKey, descriptionArguments));
  107. description && descriptionArray.push(description);
  108. return descriptionArray;
  109. }
  110. _onDismissed: () => void;
  111. /**
  112. * Callback to dismiss the notification.
  113. *
  114. * @private
  115. * @returns {void}
  116. */
  117. _onDismissed() {
  118. this.props.onDismissed(this.props.uid);
  119. }
  120. }