2015-07-15 12:30:11 +08:00
|
|
|
import Component from 'flarum/Component';
|
|
|
|
import Button from 'flarum/components/Button';
|
|
|
|
import listItems from 'flarum/helpers/listItems';
|
|
|
|
import extract from 'flarum/utils/extract';
|
2015-04-25 20:58:39 +08:00
|
|
|
|
2015-07-15 12:30:11 +08:00
|
|
|
/**
|
|
|
|
* The `Alert` component represents an alert box, which contains a message,
|
|
|
|
* some controls, and may be dismissible.
|
|
|
|
*
|
|
|
|
* The alert may have the following special props:
|
|
|
|
*
|
|
|
|
* - `type` The type of alert this is. Will be used to give the alert a class
|
2015-07-17 13:17:49 +08:00
|
|
|
* name of `Alert--{type}`.
|
2015-07-15 12:30:11 +08:00
|
|
|
* - `controls` An array of controls to show in the alert.
|
|
|
|
* - `dismissible` Whether or not the alert can be dismissed.
|
|
|
|
* - `ondismiss` A callback to run when the alert is dismissed.
|
|
|
|
*
|
|
|
|
* All other props will be assigned as attributes on the alert element.
|
|
|
|
*/
|
2015-04-25 20:58:39 +08:00
|
|
|
export default class Alert extends Component {
|
|
|
|
view() {
|
2015-07-15 12:30:11 +08:00
|
|
|
const attrs = Object.assign({}, this.props);
|
2015-04-25 20:58:39 +08:00
|
|
|
|
2015-07-15 12:30:11 +08:00
|
|
|
const type = extract(attrs, 'type');
|
2015-07-17 13:17:49 +08:00
|
|
|
attrs.className = 'Alert Alert--' + type + ' ' + (attrs.className || '');
|
2015-04-25 20:58:39 +08:00
|
|
|
|
2015-07-15 12:30:11 +08:00
|
|
|
const children = extract(attrs, 'children');
|
|
|
|
const controls = extract(attrs, 'controls') || [];
|
2015-04-25 20:58:39 +08:00
|
|
|
|
2015-07-15 12:30:11 +08:00
|
|
|
// If the alert is meant to be dismissible (which is the case by default),
|
|
|
|
// then we will create a dismiss button to append as the final control in
|
|
|
|
// the alert.
|
|
|
|
const dismissible = extract(attrs, 'dismissible');
|
|
|
|
const ondismiss = extract(attrs, 'ondismiss');
|
|
|
|
const dismissControl = [];
|
2015-04-25 20:58:39 +08:00
|
|
|
|
2015-07-15 12:30:11 +08:00
|
|
|
if (dismissible || dismissible === undefined) {
|
2015-08-26 15:26:24 +08:00
|
|
|
dismissControl.push(
|
|
|
|
<Button
|
2018-05-09 14:56:30 +08:00
|
|
|
icon="fas fa-times"
|
2015-08-26 15:26:24 +08:00
|
|
|
className="Button Button--link Button--icon Alert-dismiss"
|
|
|
|
onclick={ondismiss}/>
|
|
|
|
);
|
2015-04-25 20:58:39 +08:00
|
|
|
}
|
|
|
|
|
2015-07-15 12:30:11 +08:00
|
|
|
return (
|
|
|
|
<div {...attrs}>
|
2015-07-17 13:17:49 +08:00
|
|
|
<span className="Alert-body">
|
2015-07-15 12:30:11 +08:00
|
|
|
{children}
|
|
|
|
</span>
|
2015-07-17 13:17:49 +08:00
|
|
|
<ul className="Alert-controls">
|
2015-07-15 12:30:11 +08:00
|
|
|
{listItems(controls.concat(dismissControl))}
|
|
|
|
</ul>
|
|
|
|
</div>
|
|
|
|
);
|
2015-04-25 20:58:39 +08:00
|
|
|
}
|
|
|
|
}
|