2016-03-22 00:25:23 +01:00
|
|
|
import React from 'react';
|
2016-03-30 05:38:34 +02:00
|
|
|
import SilverStripeComponent from 'silverstripe-component';
|
2016-03-22 00:25:23 +01:00
|
|
|
|
|
|
|
class FormActionComponent extends SilverStripeComponent {
|
2016-03-30 23:45:54 +02:00
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
|
|
|
|
|
|
|
this.handleClick = this.handleClick.bind(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
2016-04-01 04:20:25 +02:00
|
|
|
const props = {
|
|
|
|
type: this.props.type,
|
|
|
|
className: this.getButtonClasses(),
|
|
|
|
disabled: this.props.disabled,
|
2016-04-12 13:45:19 +02:00
|
|
|
onClick: this.handleClick,
|
2016-04-01 04:20:25 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
if (typeof this.props.id !== 'undefined') {
|
|
|
|
props.id = this.props.id;
|
|
|
|
}
|
|
|
|
|
2016-03-30 23:45:54 +02:00
|
|
|
return (
|
2016-04-01 04:20:25 +02:00
|
|
|
<button {...props}>
|
2016-03-30 23:45:54 +02:00
|
|
|
{this.getLoadingIcon()}
|
|
|
|
{this.props.label}
|
|
|
|
</button>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the necessary button classes based on the given props
|
|
|
|
*
|
|
|
|
* @returns string
|
|
|
|
*/
|
|
|
|
getButtonClasses() {
|
2016-04-01 04:20:25 +02:00
|
|
|
const buttonClasses = ['btn'];
|
2016-03-30 23:45:54 +02:00
|
|
|
|
|
|
|
// Add 'type' class
|
2016-04-14 04:01:50 +02:00
|
|
|
buttonClasses.push(`btn-${this.props.bootstrapButtonStyle}`);
|
2016-03-30 23:45:54 +02:00
|
|
|
|
|
|
|
// If there is no text
|
|
|
|
if (typeof this.props.label === 'undefined') {
|
2016-04-01 04:20:25 +02:00
|
|
|
buttonClasses.push('no-text');
|
2016-03-29 07:07:54 +02:00
|
|
|
}
|
2016-03-22 00:25:23 +01:00
|
|
|
|
2016-03-30 23:45:54 +02:00
|
|
|
// Add icon class
|
|
|
|
if (typeof this.props.icon !== 'undefined') {
|
2016-04-01 04:20:25 +02:00
|
|
|
buttonClasses.push(`font-icon-${this.props.icon}`);
|
2016-03-22 00:25:23 +01:00
|
|
|
}
|
|
|
|
|
2016-03-30 23:45:54 +02:00
|
|
|
// Add loading class
|
|
|
|
if (this.props.loading === true) {
|
2016-04-01 04:20:25 +02:00
|
|
|
buttonClasses.push('btn--loading');
|
2016-03-29 07:07:54 +02:00
|
|
|
}
|
|
|
|
|
2016-03-30 23:45:54 +02:00
|
|
|
// Add disabled class
|
|
|
|
if (this.props.disabled === true) {
|
2016-04-01 04:20:25 +02:00
|
|
|
buttonClasses.push('disabled');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (typeof this.props.extraClass !== 'undefined') {
|
|
|
|
buttonClasses.push(this.props.extraClass);
|
2016-03-29 07:07:54 +02:00
|
|
|
}
|
|
|
|
|
2016-04-01 04:20:25 +02:00
|
|
|
return buttonClasses.join(' ');
|
2016-03-30 23:45:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns markup for the loading icon
|
|
|
|
*
|
|
|
|
* @returns object|null
|
|
|
|
*/
|
|
|
|
getLoadingIcon() {
|
|
|
|
if (this.props.loading) {
|
|
|
|
return (
|
|
|
|
<div className="btn__loading-icon" >
|
|
|
|
<svg viewBox="0 0 44 12">
|
|
|
|
<circle cx="6" cy="6" r="6" />
|
|
|
|
<circle cx="22" cy="6" r="6" />
|
|
|
|
<circle cx="38" cy="6" r="6" />
|
|
|
|
</svg>
|
|
|
|
</div>
|
|
|
|
);
|
2016-03-29 07:07:54 +02:00
|
|
|
}
|
|
|
|
|
2016-03-30 23:45:54 +02:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Event handler triggered when a user clicks the button.
|
|
|
|
*
|
|
|
|
* @param object event
|
2016-04-14 04:01:50 +02:00
|
|
|
* @return undefined
|
2016-03-30 23:45:54 +02:00
|
|
|
*/
|
|
|
|
handleClick(event) {
|
2016-04-14 04:01:50 +02:00
|
|
|
if (typeof this.props.handleClick === 'undefined') {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-03-30 23:45:54 +02:00
|
|
|
this.props.handleClick(event);
|
|
|
|
}
|
|
|
|
|
2016-03-22 00:25:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
FormActionComponent.propTypes = {
|
2016-04-01 04:20:25 +02:00
|
|
|
id: React.PropTypes.string,
|
2016-04-12 13:45:19 +02:00
|
|
|
handleClick: React.PropTypes.func,
|
2016-03-30 23:45:54 +02:00
|
|
|
label: React.PropTypes.string,
|
|
|
|
type: React.PropTypes.string,
|
|
|
|
loading: React.PropTypes.bool,
|
|
|
|
icon: React.PropTypes.string,
|
|
|
|
disabled: React.PropTypes.bool,
|
2016-04-14 04:01:50 +02:00
|
|
|
bootstrapButtonStyle: React.PropTypes.string,
|
2016-04-01 04:20:25 +02:00
|
|
|
extraClass: React.PropTypes.string,
|
2016-03-22 00:25:23 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
FormActionComponent.defaultProps = {
|
2016-03-30 23:45:54 +02:00
|
|
|
type: 'button',
|
2016-04-14 04:01:50 +02:00
|
|
|
bootstrapButtonStyle: 'secondary',
|
2016-04-01 04:20:25 +02:00
|
|
|
disabled: false,
|
2016-03-22 00:25:23 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
export default FormActionComponent;
|