您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

Notification.js 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. // @flow
  2. import Flag from '@atlaskit/flag';
  3. import EditorInfoIcon from '@atlaskit/icon/glyph/editor/info';
  4. import ErrorIcon from '@atlaskit/icon/glyph/error';
  5. import WarningIcon from '@atlaskit/icon/glyph/warning';
  6. import { colors } from '@atlaskit/theme';
  7. import React from 'react';
  8. import { translate } from '../../../base/i18n';
  9. import { NOTIFICATION_TYPE } from '../../constants';
  10. import AbstractNotification, {
  11. type Props
  12. } from '../AbstractNotification';
  13. declare var interfaceConfig: Object;
  14. /**
  15. * Secondary colors for notification icons.
  16. *
  17. * @type {{error, info, normal, success, warning}}
  18. */
  19. const ICON_COLOR = {
  20. error: colors.R400,
  21. info: colors.N500,
  22. normal: colors.N0,
  23. success: colors.G400,
  24. warning: colors.Y200
  25. };
  26. /**
  27. * Implements a React {@link Component} to display a notification.
  28. *
  29. * @extends Component
  30. */
  31. class Notification extends AbstractNotification<Props> {
  32. /**
  33. * Implements React's {@link Component#render()}.
  34. *
  35. * @inheritdoc
  36. * @returns {ReactElement}
  37. */
  38. render() {
  39. const {
  40. appearance,
  41. hideErrorSupportLink,
  42. isDismissAllowed,
  43. onDismissed,
  44. t,
  45. title,
  46. titleArguments,
  47. titleKey,
  48. uid
  49. } = this.props;
  50. return (
  51. <Flag
  52. actions = { this._mapAppearanceToButtons(hideErrorSupportLink) }
  53. appearance = { appearance }
  54. description = { this._renderDescription() }
  55. icon = { this._mapAppearanceToIcon() }
  56. id = { uid }
  57. isDismissAllowed = { isDismissAllowed }
  58. onDismissed = { onDismissed }
  59. testId = { titleKey }
  60. title = { title || t(titleKey, titleArguments) } />
  61. );
  62. }
  63. _getDescription: () => Array<string>
  64. _getDescriptionKey: () => string
  65. _onDismissed: () => void;
  66. /**
  67. * Creates a {@code ReactElement} for displaying the contents of the
  68. * notification.
  69. *
  70. * @private
  71. * @returns {ReactElement}
  72. */
  73. _renderDescription() {
  74. const description = this._getDescription();
  75. // the id is used for testing the UI
  76. return (
  77. <div data-testid = { this._getDescriptionKey() } >
  78. { description }
  79. </div>
  80. );
  81. }
  82. /**
  83. * Opens the support page.
  84. *
  85. * @returns {void}
  86. * @private
  87. */
  88. _onOpenSupportLink() {
  89. window.open(interfaceConfig.SUPPORT_URL, '_blank', 'noopener');
  90. }
  91. /**
  92. * Creates action button configurations for the notification based on
  93. * notification appearance.
  94. *
  95. * @param {boolean} hideErrorSupportLink - Indicates if the support link
  96. * should be hidden in the error messages.
  97. * @private
  98. * @returns {Object[]}
  99. */
  100. _mapAppearanceToButtons(hideErrorSupportLink) {
  101. switch (this.props.appearance) {
  102. case NOTIFICATION_TYPE.ERROR: {
  103. const buttons = [
  104. {
  105. content: this.props.t('dialog.dismiss'),
  106. onClick: this._onDismissed
  107. }
  108. ];
  109. if (!hideErrorSupportLink) {
  110. buttons.push({
  111. content: this.props.t('dialog.contactSupport'),
  112. onClick: this._onOpenSupportLink
  113. });
  114. }
  115. return buttons;
  116. }
  117. case NOTIFICATION_TYPE.WARNING:
  118. return [
  119. {
  120. content: this.props.t('dialog.Ok'),
  121. onClick: this._onDismissed
  122. }
  123. ];
  124. default:
  125. if (this.props.customActionNameKey && this.props.customActionHandler) {
  126. return [
  127. {
  128. content: this.props.t(this.props.customActionNameKey),
  129. onClick: () => {
  130. if (this.props.customActionHandler()) {
  131. this._onDismissed();
  132. }
  133. }
  134. }
  135. ];
  136. }
  137. return [];
  138. }
  139. }
  140. /**
  141. * Creates an icon component depending on the configured notification
  142. * appearance.
  143. *
  144. * @private
  145. * @returns {ReactElement}
  146. */
  147. _mapAppearanceToIcon() {
  148. const appearance = this.props.appearance;
  149. const secIconColor = ICON_COLOR[this.props.appearance];
  150. const iconSize = 'medium';
  151. switch (appearance) {
  152. case NOTIFICATION_TYPE.ERROR:
  153. return (
  154. <ErrorIcon
  155. label = { appearance }
  156. secondaryColor = { secIconColor }
  157. size = { iconSize } />
  158. );
  159. case NOTIFICATION_TYPE.WARNING:
  160. return (
  161. <WarningIcon
  162. label = { appearance }
  163. secondaryColor = { secIconColor }
  164. size = { iconSize } />
  165. );
  166. default:
  167. return (
  168. <EditorInfoIcon
  169. label = { appearance }
  170. secondaryColor = { secIconColor }
  171. size = { iconSize } />
  172. );
  173. }
  174. }
  175. }
  176. export default translate(Notification);