2015-07-15 12:30:11 +08:00
|
|
|
import Component from 'flarum/Component';
|
|
|
|
import icon from 'flarum/helpers/icon';
|
|
|
|
import extract from 'flarum/utils/extract';
|
2015-07-29 19:30:09 +08:00
|
|
|
import LoadingIndicator from 'flarum/components/LoadingIndicator';
|
2015-07-15 12:30:11 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The `Button` component defines an element which, when clicked, performs an
|
|
|
|
* action. The button may have the following special props:
|
|
|
|
*
|
|
|
|
* - `icon` The name of the icon class. If specified, the button will be given a
|
|
|
|
* 'has-icon' class name.
|
|
|
|
* - `disabled` Whether or not the button is disabled. If truthy, the button
|
|
|
|
* will be given a 'disabled' class name, and any `onclick` handler will be
|
|
|
|
* removed.
|
2015-07-29 19:30:09 +08:00
|
|
|
* - `loading` Whether or not the button should be in a disabled loading state.
|
2015-07-15 12:30:11 +08:00
|
|
|
*
|
|
|
|
* All other props will be assigned as attributes on the button element.
|
|
|
|
*
|
|
|
|
* Note that a Button has no default class names. This is because a Button can
|
|
|
|
* be used to represent any generic clickable control, like a menu item.
|
|
|
|
*/
|
|
|
|
export default class Button extends Component {
|
|
|
|
view() {
|
|
|
|
const attrs = Object.assign({}, this.props);
|
|
|
|
|
|
|
|
delete attrs.children;
|
|
|
|
|
2015-09-15 09:57:31 +08:00
|
|
|
attrs.className = attrs.className || '';
|
|
|
|
attrs.type = attrs.type || 'button';
|
2015-07-15 12:30:11 +08:00
|
|
|
|
|
|
|
const iconName = extract(attrs, 'icon');
|
2015-07-17 13:17:49 +08:00
|
|
|
if (iconName) attrs.className += ' hasIcon';
|
2015-07-15 12:30:11 +08:00
|
|
|
|
2015-07-29 19:30:09 +08:00
|
|
|
const loading = extract(attrs, 'loading');
|
|
|
|
if (attrs.disabled || loading) {
|
|
|
|
attrs.className += ' disabled' + (loading ? ' loading' : '');
|
2015-07-15 12:30:11 +08:00
|
|
|
delete attrs.onclick;
|
|
|
|
}
|
|
|
|
|
2015-07-17 13:17:49 +08:00
|
|
|
return <button {...attrs}>{this.getButtonContent()}</button>;
|
2015-07-15 12:30:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the template for the button's content.
|
|
|
|
*
|
|
|
|
* @return {*}
|
|
|
|
* @protected
|
|
|
|
*/
|
|
|
|
getButtonContent() {
|
|
|
|
const iconName = this.props.icon;
|
|
|
|
|
|
|
|
return [
|
2015-07-31 18:46:47 +08:00
|
|
|
iconName && iconName !== true ? icon(iconName, {className: 'Button-icon'}) : '',
|
2015-07-29 19:30:09 +08:00
|
|
|
this.props.children ? <span className="Button-label">{this.props.children}</span> : '',
|
|
|
|
this.props.loading ? LoadingIndicator.component({size: 'tiny', className: 'LoadingIndicator--inline'}) : ''
|
2015-07-15 12:30:11 +08:00
|
|
|
];
|
|
|
|
}
|
|
|
|
}
|