2017-11-16 08:34:50 +08:00
|
|
|
import React from 'react';
|
2017-06-04 10:40:14 +08:00
|
|
|
import PropTypes from 'prop-types';
|
2016-05-20 21:37:19 +08:00
|
|
|
|
|
|
|
const propTypes = {
|
|
|
|
/**
|
|
|
|
* Defines HTML disable Attribute
|
|
|
|
* @defaultValue false
|
|
|
|
*/
|
|
|
|
disabled: PropTypes.bool,
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Defines HTML Tag
|
|
|
|
* @defaultValue 'button'
|
|
|
|
*/
|
|
|
|
tagName: PropTypes.string,
|
|
|
|
|
2016-06-18 00:42:17 +08:00
|
|
|
/**
|
|
|
|
* Defines the button label
|
|
|
|
* @defaultValue undefined
|
|
|
|
*/
|
|
|
|
label: PropTypes.string.isRequired,
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Defines the button click handler
|
|
|
|
* @defaultValue undefined
|
|
|
|
*/
|
2017-11-16 08:34:50 +08:00
|
|
|
onClick: (props, propName, componentName) => {
|
|
|
|
if (!props.onClick && !props.onMouseDown && !props.onMouseUp) {
|
|
|
|
return new Error('One of props \'onClick\' or \'onMouseDown\' or' +
|
|
|
|
` 'onMouseUp' was not specified in '${componentName}'.`);
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
},
|
|
|
|
onMouseDown: (props, propName, componentName) => {
|
|
|
|
if (!props.onClick && !props.onMouseDown && !props.onMouseUp) {
|
|
|
|
return new Error('One of props \'onClick\' or \'onMouseDown\' or' +
|
|
|
|
` 'onMouseUp' was not specified in '${componentName}'.`);
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
},
|
|
|
|
onMouseUp: (props, propName, componentName) => {
|
|
|
|
if (!props.onClick && !props.onMouseDown && !props.onMouseUp) {
|
|
|
|
return new Error('One of props \'onClick\' or \'onMouseDown\' or' +
|
|
|
|
` 'onMouseUp' was not specified in '${componentName}'.`);
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
},
|
|
|
|
|
|
|
|
onKeyPress: PropTypes.func,
|
|
|
|
onKeyDown: PropTypes.func,
|
|
|
|
onKeyUp: PropTypes.func,
|
|
|
|
setRef: PropTypes.func,
|
2016-05-20 21:37:19 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
const defaultProps = {
|
|
|
|
disabled: false,
|
|
|
|
tagName: 'button',
|
2017-11-16 08:34:50 +08:00
|
|
|
onClick: undefined,
|
|
|
|
onMouseDown: undefined,
|
|
|
|
onMouseUp: undefined,
|
|
|
|
onKeyPress: undefined,
|
|
|
|
onKeyDown: undefined,
|
|
|
|
onKeyUp: undefined,
|
|
|
|
setRef: undefined,
|
2016-05-20 21:37:19 +08:00
|
|
|
};
|
|
|
|
|
2016-08-06 02:39:24 +08:00
|
|
|
/**
|
|
|
|
* Event handlers below are used to intercept a parent event handler from
|
|
|
|
* firing when the Button is disabled.
|
|
|
|
* Key press event handlers intercept firing for
|
|
|
|
* keyboard users to comply with ARIA standards.
|
|
|
|
*/
|
|
|
|
|
2017-11-16 08:34:50 +08:00
|
|
|
export default class ButtonBase extends React.Component {
|
2016-05-20 21:37:19 +08:00
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
2016-08-03 06:55:20 +08:00
|
|
|
|
|
|
|
// Bind Mouse Event Handlers
|
|
|
|
this.internalClickHandler = this.internalClickHandler.bind(this);
|
|
|
|
this.internalDoubleClickHandler = this.internalDoubleClickHandler.bind(this);
|
|
|
|
this.internalMouseDownHandler = this.internalMouseDownHandler.bind(this);
|
|
|
|
this.internalMouseUpHandler = this.internalMouseUpHandler.bind(this);
|
|
|
|
|
|
|
|
// Bind Keyboard Event Handlers
|
|
|
|
this.internalKeyPressHandler = this.internalKeyPressHandler.bind(this);
|
|
|
|
this.internalKeyDownHandler = this.internalKeyDownHandler.bind(this);
|
|
|
|
this.internalKeyUpHandler = this.internalKeyUpHandler.bind(this);
|
|
|
|
}
|
|
|
|
|
2016-08-24 03:18:49 +08:00
|
|
|
validateDisabled(eventHandler, ...args) {
|
2016-08-03 06:55:20 +08:00
|
|
|
if (!this.props.disabled && typeof eventHandler === 'function') {
|
|
|
|
return eventHandler(...args);
|
|
|
|
}
|
2017-11-16 08:34:50 +08:00
|
|
|
|
|
|
|
return null;
|
2016-08-03 06:55:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Define Mouse Event Handlers
|
2017-11-16 08:34:50 +08:00
|
|
|
internalClickHandler(...args) {
|
|
|
|
return this.validateDisabled(this.props.onClick, ...args);
|
2016-08-03 06:55:20 +08:00
|
|
|
}
|
|
|
|
|
2017-11-16 08:34:50 +08:00
|
|
|
internalDoubleClickHandler(...args) {
|
|
|
|
return this.validateDisabled(this.props.onDoubleClick, ...args);
|
2016-08-03 06:55:20 +08:00
|
|
|
}
|
|
|
|
|
2017-11-16 08:34:50 +08:00
|
|
|
internalMouseDownHandler(...args) {
|
|
|
|
return this.validateDisabled(this.props.onMouseDown, ...args);
|
2016-08-03 06:55:20 +08:00
|
|
|
}
|
|
|
|
|
2017-11-16 08:34:50 +08:00
|
|
|
internalMouseUpHandler(...args) {
|
|
|
|
return this.validateDisabled(this.props.onMouseUp, ...args);
|
2016-08-03 06:55:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Define Keyboard Event Handlers
|
2017-11-16 08:34:50 +08:00
|
|
|
internalKeyPressHandler(...args) {
|
|
|
|
return this.validateDisabled(this.props.onKeyPress, ...args);
|
2016-08-03 06:55:20 +08:00
|
|
|
}
|
|
|
|
|
2017-11-16 08:34:50 +08:00
|
|
|
internalKeyDownHandler(...args) {
|
|
|
|
return this.validateDisabled(this.props.onKeyDown, ...args);
|
2016-08-03 06:55:20 +08:00
|
|
|
}
|
|
|
|
|
2017-11-16 08:34:50 +08:00
|
|
|
internalKeyUpHandler(...args) {
|
|
|
|
return this.validateDisabled(this.props.onKeyUp, ...args);
|
2016-05-20 21:37:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
2017-06-03 03:25:02 +08:00
|
|
|
const Component = this.props.tagName;
|
2016-05-20 21:37:19 +08:00
|
|
|
|
2016-07-12 04:10:55 +08:00
|
|
|
const remainingProps = Object.assign({}, this.props);
|
|
|
|
delete remainingProps.label;
|
|
|
|
delete remainingProps.tagName;
|
2016-08-03 06:55:20 +08:00
|
|
|
delete remainingProps.disabled;
|
|
|
|
|
|
|
|
// Delete Mouse event handlers
|
|
|
|
delete remainingProps.onClick;
|
|
|
|
delete remainingProps.onDoubleClick;
|
|
|
|
delete remainingProps.onMouseDown;
|
|
|
|
delete remainingProps.onMouseUp;
|
|
|
|
|
|
|
|
// Delete Keyboard event handlers
|
|
|
|
delete remainingProps.onKeyPress;
|
|
|
|
delete remainingProps.onKeyDown;
|
|
|
|
delete remainingProps.onKeyUp;
|
2016-07-12 04:10:55 +08:00
|
|
|
|
2017-11-16 08:34:50 +08:00
|
|
|
// Delete setRef callback if it exists
|
|
|
|
delete remainingProps.setRef;
|
|
|
|
|
2016-05-20 21:37:19 +08:00
|
|
|
return (
|
2016-08-03 06:55:20 +08:00
|
|
|
<Component
|
2017-11-16 08:34:50 +08:00
|
|
|
ref={this.props.setRef}
|
2016-08-03 06:55:20 +08:00
|
|
|
aria-label={this.props.label}
|
|
|
|
aria-disabled={this.props.disabled}
|
|
|
|
|
|
|
|
// Render Mouse event handlers
|
|
|
|
onClick={this.internalClickHandler}
|
|
|
|
onDoubleClick={this.internalDoubleClickHandler}
|
|
|
|
onMouseDown={this.internalMouseDownHandler}
|
|
|
|
onMouseUp={this.internalMouseUpHandler}
|
|
|
|
|
|
|
|
// Render Keyboard event handlers
|
|
|
|
onKeyPress={this.internalKeyPressHandler}
|
|
|
|
onKeyDown={this.internalKeyDownHandler}
|
|
|
|
onKeyUp={this.internalKeyUpHandler}
|
|
|
|
|
|
|
|
{...remainingProps}
|
|
|
|
>
|
2016-05-20 21:37:19 +08:00
|
|
|
{this.props.children}
|
|
|
|
</Component>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ButtonBase.propTypes = propTypes;
|
|
|
|
ButtonBase.defaultProps = defaultProps;
|