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 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. import { ToggleStateless } from '@atlaskit/toggle';
  2. import PropTypes from 'prop-types';
  3. import React, { Component } from 'react';
  4. import { translate } from '../../base/i18n';
  5. import { default as Notification } from './Notification';
  6. import { NOTIFICATION_TYPE } from '../constants';
  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. ...Notification.propTypes,
  20. /**
  21. * Any additional text to display at the end of the notification message
  22. * body.
  23. */
  24. additionalMessage: PropTypes.string,
  25. /**
  26. * Optional callback to invoke when the notification is dismissed. The
  27. * current value of the toggle element will be passed in.
  28. */
  29. onToggleSubmit: PropTypes.func,
  30. /**
  31. * Whether or not the toggle element should be displayed.
  32. */
  33. showToggle: PropTypes.bool,
  34. /**
  35. * Translation key for a message to display at the top of the
  36. * notification body.
  37. */
  38. subtitleKey: PropTypes.string,
  39. /*
  40. * The translation key to be used as a label describing what setting the
  41. * toggle will change.
  42. */
  43. toggleLabelKey: PropTypes.string
  44. };
  45. /**
  46. * Initializes a new {@code NotificationWithToggle} instance.
  47. *
  48. * @param {Object} props - The read-only properties with which the new
  49. * instance is to be initialized.
  50. */
  51. constructor(props) {
  52. super(props);
  53. this.state = {
  54. /**
  55. * Whether or not the toggle element is active/checked/selected.
  56. *
  57. * @type {boolean}
  58. */
  59. isToggleChecked: false
  60. };
  61. // Bind event handlers so they are only bound once for every instance.
  62. this._onDismissed = this._onDismissed.bind(this);
  63. this._onToggleChange = this._onToggleChange.bind(this);
  64. }
  65. /**
  66. * Implements React's {@link Component#render()}.
  67. *
  68. * @inheritdoc
  69. * @returns {ReactElement}
  70. */
  71. render() {
  72. return (
  73. <Notification
  74. appearance = { NOTIFICATION_TYPE.WARNING }
  75. { ...this.props }
  76. description = { this._renderDescription() } />
  77. );
  78. }
  79. /**
  80. * Calls back into {@code FlagGroup} to dismiss the notification. Optionally
  81. * will execute a passed in onToggleSubmit callback with the current state
  82. * of the toggle element.
  83. *
  84. * @private
  85. * @returns {void}
  86. */
  87. _onDismissed() {
  88. const { onDismissed, onToggleSubmit, showToggle, uid } = this.props;
  89. if (showToggle && onToggleSubmit) {
  90. onToggleSubmit(this.state.isToggleChecked);
  91. }
  92. onDismissed(uid);
  93. }
  94. /**
  95. * Updates the current known state of the toggle selection.
  96. *
  97. * @param {Object} event - The DOM event from changing the toggle selection.
  98. * @private
  99. * @returns {void}
  100. */
  101. _onToggleChange(event) {
  102. this.setState({
  103. isToggleChecked: event.target.checked
  104. });
  105. }
  106. /**
  107. * Creates a React Element for displaying the notification message as well
  108. * as a toggle.
  109. *
  110. * @private
  111. * @returns {ReactElement}
  112. */
  113. _renderDescription() {
  114. const {
  115. additionalMessage,
  116. descriptionKey,
  117. showToggle,
  118. subtitleKey,
  119. t,
  120. toggleLabelKey
  121. } = this.props;
  122. return (
  123. <div className = 'notification-with-toggle'>
  124. <div>{ t(subtitleKey) }</div>
  125. { descriptionKey ? <div>{ t(descriptionKey) }</div> : null }
  126. { additionalMessage ? <div>{ additionalMessage }</div>
  127. : null }
  128. { showToggle
  129. ? <div>
  130. { t(toggleLabelKey) }
  131. <ToggleStateless
  132. isChecked
  133. = { this.state.isToggleChecked }
  134. onChange = { this._onToggleChange } />
  135. </div>
  136. : null }
  137. </div>
  138. );
  139. }
  140. }
  141. export default translate(NotificationWithToggle);