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

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