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

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