2017-07-31 18:36:41 +00:00
|
|
|
import Flag from '@atlaskit/flag';
|
|
|
|
import WarningIcon from '@atlaskit/icon/glyph/warning';
|
|
|
|
import { ToggleStateless } from '@atlaskit/toggle';
|
2017-09-27 21:23:31 +00:00
|
|
|
import PropTypes from 'prop-types';
|
2017-07-31 18:36:41 +00:00
|
|
|
import React, { Component } from 'react';
|
|
|
|
|
|
|
|
import { translate } from '../../base/i18n';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* React {@code Component} for displaying a notification with a toggle element.
|
|
|
|
*
|
|
|
|
* @extends Component
|
|
|
|
*/
|
|
|
|
class NotificationWithToggle extends Component {
|
|
|
|
/**
|
|
|
|
* {@code NotificationWithToggle} component's property types.
|
|
|
|
*
|
|
|
|
* @static
|
|
|
|
*/
|
|
|
|
static propTypes = {
|
|
|
|
/**
|
|
|
|
* Any additional text to display at the end of the notification message
|
|
|
|
* body.
|
|
|
|
*/
|
2017-09-27 21:23:31 +00:00
|
|
|
additionalMessage: PropTypes.string,
|
2017-07-31 18:36:41 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Whether or not the dismiss button should be displayed. This is passed
|
|
|
|
* in by {@code FlagGroup}.
|
|
|
|
*/
|
2017-09-27 21:23:31 +00:00
|
|
|
isDismissAllowed: PropTypes.bool,
|
2017-07-31 18:36:41 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The translation key to be used as the main body of the notification.
|
|
|
|
*/
|
2017-09-27 21:23:31 +00:00
|
|
|
messageKey: PropTypes.string,
|
2017-07-31 18:36:41 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Callback invoked when the user clicks to dismiss the notification.
|
|
|
|
* This is passed in by {@code FlagGroup}.
|
|
|
|
*/
|
2017-09-27 21:23:31 +00:00
|
|
|
onDismissed: PropTypes.func,
|
2017-07-31 18:36:41 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Optional callback to invoke when the notification is dismissed. The
|
|
|
|
* current value of the toggle element will be passed in.
|
|
|
|
*/
|
2017-09-27 21:23:31 +00:00
|
|
|
onToggleSubmit: PropTypes.func,
|
2017-07-31 18:36:41 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Whether or not the toggle element should be displayed.
|
|
|
|
*/
|
2017-09-27 21:23:31 +00:00
|
|
|
showToggle: PropTypes.bool,
|
2017-07-31 18:36:41 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Translation key for a message to display at the top of the
|
|
|
|
* notification body.
|
|
|
|
*/
|
2017-09-27 21:23:31 +00:00
|
|
|
subtitleKey: PropTypes.string,
|
2017-07-31 18:36:41 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Invoked to obtain translated strings.
|
|
|
|
*/
|
2017-09-27 21:23:31 +00:00
|
|
|
t: PropTypes.func,
|
2017-07-31 18:36:41 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The translation key to be used as the title of the notification.
|
|
|
|
*/
|
2017-09-27 21:23:31 +00:00
|
|
|
titleKey: PropTypes.string,
|
2017-07-31 18:36:41 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* The translation key to be used as a label describing what setting the
|
|
|
|
* toggle will change.
|
|
|
|
*/
|
2017-09-27 21:23:31 +00:00
|
|
|
toggleLabelKey: PropTypes.string,
|
2017-07-31 18:36:41 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The unique identifier for the notification. Passed back by the
|
|
|
|
* {@code Flag} component in the onDismissed callback.
|
|
|
|
*/
|
2017-09-27 21:23:31 +00:00
|
|
|
uid: PropTypes.number
|
2017-07-31 18:36:41 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Initializes a new {@code NotificationWithToggle} instance.
|
|
|
|
*
|
|
|
|
* @param {Object} props - The read-only properties with which the new
|
|
|
|
* instance is to be initialized.
|
|
|
|
*/
|
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
|
|
|
|
|
|
|
this.state = {
|
|
|
|
/**
|
|
|
|
* Whether or not the toggle element is active/checked/selected.
|
|
|
|
*
|
|
|
|
* @type {boolean}
|
|
|
|
*/
|
|
|
|
isToggleChecked: false
|
|
|
|
};
|
|
|
|
|
|
|
|
// Bind event handlers so they are only bound once for every instance.
|
|
|
|
this._onDismissed = this._onDismissed.bind(this);
|
|
|
|
this._onToggleChange = this._onToggleChange.bind(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Implements React's {@link Component#render()}.
|
|
|
|
*
|
|
|
|
* @inheritdoc
|
|
|
|
* @returns {ReactElement}
|
|
|
|
*/
|
|
|
|
render() {
|
|
|
|
const {
|
|
|
|
isDismissAllowed,
|
|
|
|
t,
|
|
|
|
titleKey,
|
|
|
|
uid
|
|
|
|
} = this.props;
|
|
|
|
|
|
|
|
return (
|
|
|
|
<Flag
|
|
|
|
actions = { [
|
|
|
|
{
|
|
|
|
content: t('dialog.Ok'),
|
|
|
|
onClick: this._onDismissed
|
|
|
|
}
|
|
|
|
] }
|
|
|
|
appearance = 'warning'
|
|
|
|
description = { this._renderDescription() }
|
|
|
|
icon = { (
|
|
|
|
<WarningIcon
|
|
|
|
label = 'Warning'
|
|
|
|
size = 'medium' />
|
|
|
|
) }
|
|
|
|
id = { uid }
|
|
|
|
isDismissAllowed = { isDismissAllowed }
|
|
|
|
onDismissed = { this._onDismissed }
|
|
|
|
title = { t(titleKey) } />
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Calls back into {@code FlagGroup} to dismiss the notification. Optionally
|
|
|
|
* will execute a passed in onToggleSubmit callback with the current state
|
|
|
|
* of the toggle element.
|
|
|
|
*
|
|
|
|
* @private
|
|
|
|
* @returns {void}
|
|
|
|
*/
|
|
|
|
_onDismissed() {
|
|
|
|
const { onDismissed, onToggleSubmit, showToggle, uid } = this.props;
|
|
|
|
|
|
|
|
if (showToggle && onToggleSubmit) {
|
|
|
|
onToggleSubmit(this.state.isToggleChecked);
|
|
|
|
}
|
|
|
|
|
|
|
|
onDismissed(uid);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Updates the current known state of the toggle selection.
|
|
|
|
*
|
|
|
|
* @param {Object} event - The DOM event from changing the toggle selection.
|
|
|
|
* @private
|
|
|
|
* @returns {void}
|
|
|
|
*/
|
|
|
|
_onToggleChange(event) {
|
|
|
|
this.setState({
|
|
|
|
isToggleChecked: event.target.checked
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates a React Element for displaying the notification message as well
|
|
|
|
* as a toggle.
|
|
|
|
*
|
|
|
|
* @private
|
|
|
|
* @returns {ReactElement}
|
|
|
|
*/
|
|
|
|
_renderDescription() {
|
|
|
|
const {
|
|
|
|
additionalMessage,
|
|
|
|
messageKey,
|
|
|
|
showToggle,
|
|
|
|
subtitleKey,
|
|
|
|
t,
|
|
|
|
toggleLabelKey
|
|
|
|
} = this.props;
|
|
|
|
|
|
|
|
return (
|
|
|
|
<div className = 'notification-with-toggle'>
|
|
|
|
<div>{ t(subtitleKey) }</div>
|
|
|
|
{ messageKey ? <div>{ t(messageKey) }</div> : null }
|
|
|
|
{ additionalMessage ? <div>{ additionalMessage }</div>
|
|
|
|
: null }
|
|
|
|
{ showToggle
|
|
|
|
? <div>
|
|
|
|
{ t(toggleLabelKey) }
|
|
|
|
<ToggleStateless
|
|
|
|
isChecked
|
|
|
|
= { this.state.isToggleChecked }
|
|
|
|
onChange = { this._onToggleChange } />
|
|
|
|
</div>
|
|
|
|
: null }
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default translate(NotificationWithToggle);
|