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

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