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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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. * Callback invoked when the user clicks to dismiss the notification.
  47. */
  48. onDismissed: Function,
  49. /**
  50. * Invoked to obtain translated strings.
  51. */
  52. t: Function,
  53. /**
  54. * The text to display at the top of the notification. If not passed in,
  55. * the passed in titleKey will be used.
  56. */
  57. title: string,
  58. /**
  59. * The translation arguments that may be necessary for the title.
  60. */
  61. titleArguments: Object,
  62. /**
  63. * The translation key to display as the title of the notification if
  64. * no title is provided.
  65. */
  66. titleKey: string,
  67. /**
  68. * The unique identifier for the notification.
  69. */
  70. uid: number
  71. };
  72. /**
  73. * Abstract class for {@code Notification} component.
  74. *
  75. * @extends Component
  76. */
  77. export default class AbstractNotification<P: Props> extends Component<P> {
  78. /**
  79. * Default values for {@code Notification} component's properties.
  80. *
  81. * @static
  82. */
  83. static defaultProps = {
  84. appearance: NOTIFICATION_TYPE.NORMAL,
  85. isDismissAllowed: true
  86. };
  87. /**
  88. * Initializes a new {@code Notification} instance.
  89. *
  90. * @param {Object} props - The read-only properties with which the new
  91. * instance is to be initialized.
  92. */
  93. constructor(props: P) {
  94. super(props);
  95. // Bind event handler so it is only bound once for every instance.
  96. this._onDismissed = this._onDismissed.bind(this);
  97. }
  98. _getDescription: () => Array<string>
  99. /**
  100. * Returns the description array to be displayed.
  101. *
  102. * @protected
  103. * @returns {Array<string>}
  104. */
  105. _getDescription() {
  106. const {
  107. description,
  108. descriptionArguments,
  109. descriptionKey,
  110. t
  111. } = this.props;
  112. const descriptionArray = [];
  113. descriptionKey
  114. && descriptionArray.push(t(descriptionKey, descriptionArguments));
  115. description && descriptionArray.push(description);
  116. return descriptionArray;
  117. }
  118. _onDismissed: () => void;
  119. /**
  120. * Callback to dismiss the notification.
  121. *
  122. * @private
  123. * @returns {void}
  124. */
  125. _onDismissed() {
  126. this.props.onDismissed(this.props.uid);
  127. }
  128. }