2016-09-09 18:28:57 +00:00
|
|
|
import React, {Component, PropTypes} from 'react';
|
|
|
|
|
2016-11-07 20:24:38 +00:00
|
|
|
const STATE_DEFAULT = 'default';
|
|
|
|
const STATE_ASK = 'ask';
|
|
|
|
const STATE_DONE = 'done';
|
|
|
|
|
2016-09-09 18:28:57 +00:00
|
|
|
class PromptButton extends Component {
|
2016-11-26 08:29:16 +00:00
|
|
|
state = {state: STATE_DEFAULT};
|
2016-09-09 18:28:57 +00:00
|
|
|
|
|
|
|
_confirm (e) {
|
|
|
|
// Clear existing timeouts
|
2016-11-22 19:42:10 +00:00
|
|
|
clearTimeout(this._triggerTimeout);
|
2016-09-09 18:28:57 +00:00
|
|
|
|
|
|
|
// Fire the click handler
|
|
|
|
this.props.onClick(e);
|
2016-11-07 20:24:38 +00:00
|
|
|
|
|
|
|
// Set the state to done (but delay a bit to not alarm user)
|
|
|
|
this._doneTimeout = setTimeout(() => {
|
|
|
|
this.setState({state: STATE_DONE});
|
|
|
|
}, 100);
|
|
|
|
|
|
|
|
// Set a timeout to hide the confirmation
|
2016-11-22 19:42:10 +00:00
|
|
|
this._triggerTimeout = setTimeout(() => {
|
2016-11-07 20:24:38 +00:00
|
|
|
this.setState({state: STATE_DEFAULT});
|
|
|
|
}, 2000);
|
2016-09-09 18:28:57 +00:00
|
|
|
}
|
|
|
|
|
2016-11-07 20:24:38 +00:00
|
|
|
_ask (e) {
|
2016-09-09 18:28:57 +00:00
|
|
|
// Prevent events (ex. won't close dropdown if it's in one)
|
|
|
|
e.preventDefault();
|
|
|
|
e.stopPropagation();
|
|
|
|
|
|
|
|
// Toggle the confirmation notice
|
2016-11-07 20:24:38 +00:00
|
|
|
this.setState({state: STATE_ASK});
|
2016-09-09 18:28:57 +00:00
|
|
|
|
|
|
|
// Set a timeout to hide the confirmation
|
2016-11-22 19:42:10 +00:00
|
|
|
this._triggerTimeout = setTimeout(() => {
|
2016-11-07 20:24:38 +00:00
|
|
|
this.setState({state: STATE_DEFAULT});
|
2016-09-09 18:28:57 +00:00
|
|
|
}, 2000);
|
|
|
|
}
|
|
|
|
|
2016-11-29 22:28:55 +00:00
|
|
|
_handleClick = e => {
|
2016-11-07 20:24:38 +00:00
|
|
|
const {state} = this.state;
|
|
|
|
if (state === STATE_ASK) {
|
2016-09-09 18:28:57 +00:00
|
|
|
this._confirm(e)
|
2016-11-07 20:24:38 +00:00
|
|
|
} else if (state === STATE_DEFAULT) {
|
|
|
|
this._ask(e)
|
2016-09-09 18:28:57 +00:00
|
|
|
} else {
|
2016-11-07 20:24:38 +00:00
|
|
|
// Do nothing
|
2016-09-09 18:28:57 +00:00
|
|
|
}
|
2016-11-29 22:28:55 +00:00
|
|
|
};
|
2016-09-09 18:28:57 +00:00
|
|
|
|
|
|
|
componentWillUnmount () {
|
2016-11-22 19:42:10 +00:00
|
|
|
clearTimeout(this._triggerTimeout);
|
2016-11-07 20:24:38 +00:00
|
|
|
clearTimeout(this._doneTimeout);
|
2016-09-09 18:28:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
render () {
|
2016-11-07 20:24:38 +00:00
|
|
|
const {children, onClick, addIcon, confirmMessage, doneMessage, ...other} = this.props;
|
|
|
|
const {state} = this.state;
|
2016-09-09 18:28:57 +00:00
|
|
|
|
2016-09-21 20:32:45 +00:00
|
|
|
const CONFIRM_MESSAGE = confirmMessage || 'Click to confirm';
|
2016-11-07 20:24:38 +00:00
|
|
|
const DONE_MESSAGE = doneMessage || 'Done';
|
2016-09-09 18:28:57 +00:00
|
|
|
|
|
|
|
let innerMsg;
|
2016-11-07 20:24:38 +00:00
|
|
|
if (state === STATE_ASK && addIcon) {
|
2016-09-09 18:28:57 +00:00
|
|
|
innerMsg = (
|
|
|
|
<span className="danger">
|
2017-02-01 20:21:14 +00:00
|
|
|
<i className="fa fa-exclamation-circle"/> {CONFIRM_MESSAGE}
|
2016-09-09 18:28:57 +00:00
|
|
|
</span>
|
|
|
|
)
|
2016-11-07 20:24:38 +00:00
|
|
|
} else if (state === STATE_ASK) {
|
2016-09-09 18:28:57 +00:00
|
|
|
innerMsg = <span className="danger">{CONFIRM_MESSAGE}</span>
|
2016-11-07 20:24:38 +00:00
|
|
|
} else if (state === STATE_DONE) {
|
|
|
|
innerMsg = DONE_MESSAGE
|
2016-09-09 18:28:57 +00:00
|
|
|
} else {
|
|
|
|
innerMsg = children
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
2016-11-29 22:28:55 +00:00
|
|
|
<button onClick={this._handleClick} {...other}>
|
2016-09-09 18:28:57 +00:00
|
|
|
{innerMsg}
|
|
|
|
</button>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
PromptButton.propTypes = {
|
2016-09-21 20:32:45 +00:00
|
|
|
addIcon: PropTypes.bool,
|
2016-11-22 19:42:10 +00:00
|
|
|
confirmMessage: PropTypes.any,
|
2016-09-09 18:28:57 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
export default PromptButton;
|