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.

NotificationWithToggle.web.js 5.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. import Flag from '@atlaskit/flag';
  2. import WarningIcon from '@atlaskit/icon/glyph/warning';
  3. import { ToggleStateless } from '@atlaskit/toggle';
  4. import PropTypes from 'prop-types';
  5. import React, { Component } from 'react';
  6. import { translate } from '../../base/i18n';
  7. /**
  8. * React {@code Component} for displaying a notification with a toggle element.
  9. *
  10. * @extends Component
  11. */
  12. class NotificationWithToggle extends Component {
  13. /**
  14. * {@code NotificationWithToggle} component's property types.
  15. *
  16. * @static
  17. */
  18. static propTypes = {
  19. /**
  20. * Any additional text to display at the end of the notification message
  21. * body.
  22. */
  23. additionalMessage: PropTypes.string,
  24. /**
  25. * Whether or not the dismiss button should be displayed. This is passed
  26. * in by {@code FlagGroup}.
  27. */
  28. isDismissAllowed: PropTypes.bool,
  29. /**
  30. * The translation key to be used as the main body of the notification.
  31. */
  32. messageKey: PropTypes.string,
  33. /**
  34. * Callback invoked when the user clicks to dismiss the notification.
  35. * This is passed in by {@code FlagGroup}.
  36. */
  37. onDismissed: PropTypes.func,
  38. /**
  39. * Optional callback to invoke when the notification is dismissed. The
  40. * current value of the toggle element will be passed in.
  41. */
  42. onToggleSubmit: PropTypes.func,
  43. /**
  44. * Whether or not the toggle element should be displayed.
  45. */
  46. showToggle: PropTypes.bool,
  47. /**
  48. * Translation key for a message to display at the top of the
  49. * notification body.
  50. */
  51. subtitleKey: PropTypes.string,
  52. /**
  53. * Invoked to obtain translated strings.
  54. */
  55. t: PropTypes.func,
  56. /**
  57. * The translation key to be used as the title of the notification.
  58. */
  59. titleKey: PropTypes.string,
  60. /*
  61. * The translation key to be used as a label describing what setting the
  62. * toggle will change.
  63. */
  64. toggleLabelKey: PropTypes.string,
  65. /**
  66. * The unique identifier for the notification. Passed back by the
  67. * {@code Flag} component in the onDismissed callback.
  68. */
  69. uid: PropTypes.number
  70. };
  71. /**
  72. * Initializes a new {@code NotificationWithToggle} instance.
  73. *
  74. * @param {Object} props - The read-only properties with which the new
  75. * instance is to be initialized.
  76. */
  77. constructor(props) {
  78. super(props);
  79. this.state = {
  80. /**
  81. * Whether or not the toggle element is active/checked/selected.
  82. *
  83. * @type {boolean}
  84. */
  85. isToggleChecked: false
  86. };
  87. // Bind event handlers so they are only bound once for every instance.
  88. this._onDismissed = this._onDismissed.bind(this);
  89. this._onToggleChange = this._onToggleChange.bind(this);
  90. }
  91. /**
  92. * Implements React's {@link Component#render()}.
  93. *
  94. * @inheritdoc
  95. * @returns {ReactElement}
  96. */
  97. render() {
  98. const {
  99. isDismissAllowed,
  100. t,
  101. titleKey,
  102. uid
  103. } = this.props;
  104. return (
  105. <Flag
  106. actions = { [
  107. {
  108. content: t('dialog.Ok'),
  109. onClick: this._onDismissed
  110. }
  111. ] }
  112. appearance = 'warning'
  113. description = { this._renderDescription() }
  114. icon = { (
  115. <WarningIcon
  116. label = 'Warning'
  117. size = 'medium' />
  118. ) }
  119. id = { uid }
  120. isDismissAllowed = { isDismissAllowed }
  121. onDismissed = { this._onDismissed }
  122. title = { t(titleKey) } />
  123. );
  124. }
  125. /**
  126. * Calls back into {@code FlagGroup} to dismiss the notification. Optionally
  127. * will execute a passed in onToggleSubmit callback with the current state
  128. * of the toggle element.
  129. *
  130. * @private
  131. * @returns {void}
  132. */
  133. _onDismissed() {
  134. const { onDismissed, onToggleSubmit, showToggle, uid } = this.props;
  135. if (showToggle && onToggleSubmit) {
  136. onToggleSubmit(this.state.isToggleChecked);
  137. }
  138. onDismissed(uid);
  139. }
  140. /**
  141. * Updates the current known state of the toggle selection.
  142. *
  143. * @param {Object} event - The DOM event from changing the toggle selection.
  144. * @private
  145. * @returns {void}
  146. */
  147. _onToggleChange(event) {
  148. this.setState({
  149. isToggleChecked: event.target.checked
  150. });
  151. }
  152. /**
  153. * Creates a React Element for displaying the notification message as well
  154. * as a toggle.
  155. *
  156. * @private
  157. * @returns {ReactElement}
  158. */
  159. _renderDescription() {
  160. const {
  161. additionalMessage,
  162. messageKey,
  163. showToggle,
  164. subtitleKey,
  165. t,
  166. toggleLabelKey
  167. } = this.props;
  168. return (
  169. <div className = 'notification-with-toggle'>
  170. <div>{ t(subtitleKey) }</div>
  171. { messageKey ? <div>{ t(messageKey) }</div> : null }
  172. { additionalMessage ? <div>{ additionalMessage }</div>
  173. : null }
  174. { showToggle
  175. ? <div>
  176. { t(toggleLabelKey) }
  177. <ToggleStateless
  178. isChecked
  179. = { this.state.isToggleChecked }
  180. onChange = { this._onToggleChange } />
  181. </div>
  182. : null }
  183. </div>
  184. );
  185. }
  186. }
  187. export default translate(NotificationWithToggle);