2017-02-28 21:32:23 +00:00
|
|
|
import React, {PureComponent, PropTypes} from 'react';
|
2017-03-03 01:44:07 +00:00
|
|
|
import autobind from 'autobind-decorator';
|
2017-03-08 05:52:17 +00:00
|
|
|
import Button from './button';
|
2016-09-09 18:28:57 +00:00
|
|
|
|
2016-11-07 20:24:38 +00:00
|
|
|
const STATE_DEFAULT = 'default';
|
|
|
|
const STATE_ASK = 'ask';
|
|
|
|
const STATE_DONE = 'done';
|
|
|
|
|
2017-03-03 01:44:07 +00:00
|
|
|
@autobind
|
2017-02-28 21:32:23 +00:00
|
|
|
class PromptButton extends PureComponent {
|
2017-03-03 01:44:07 +00:00
|
|
|
constructor (props) {
|
|
|
|
super(props);
|
|
|
|
|
|
|
|
this.state = {
|
|
|
|
state: STATE_DEFAULT
|
|
|
|
};
|
|
|
|
}
|
2016-09-09 18:28:57 +00:00
|
|
|
|
2017-02-27 21:00:13 +00:00
|
|
|
_confirm (...args) {
|
2016-09-09 18:28:57 +00:00
|
|
|
// 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
|
2017-02-27 21:00:13 +00:00
|
|
|
this.props.onClick(...args);
|
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
|
|
|
}
|
|
|
|
|
2017-02-27 21:00:13 +00:00
|
|
|
_ask (...args) {
|
|
|
|
const e = args[args.length - 1];
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2017-03-03 01:44:07 +00:00
|
|
|
_handleClick (...args) {
|
2016-11-07 20:24:38 +00:00
|
|
|
const {state} = this.state;
|
|
|
|
if (state === STATE_ASK) {
|
2017-03-03 20:09:08 +00:00
|
|
|
this._confirm(...args);
|
2016-11-07 20:24:38 +00:00
|
|
|
} else if (state === STATE_DEFAULT) {
|
2017-03-03 20:09:08 +00:00
|
|
|
this._ask(...args);
|
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
|
|
|
}
|
2017-03-03 01:44:07 +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 () {
|
2017-03-03 20:09:08 +00:00
|
|
|
const {
|
|
|
|
onClick, // eslint-disable-line no-unused-vars
|
2017-03-08 05:52:17 +00:00
|
|
|
children,
|
2017-03-03 20:09:08 +00:00
|
|
|
addIcon,
|
2017-03-08 05:52:17 +00:00
|
|
|
disabled,
|
2017-03-03 20:09:08 +00:00
|
|
|
confirmMessage,
|
|
|
|
doneMessage,
|
2017-03-09 06:23:23 +00:00
|
|
|
tabIndex,
|
2017-03-03 20:09:08 +00:00
|
|
|
...other
|
|
|
|
} = this.props;
|
2017-03-09 06:23:23 +00:00
|
|
|
|
2016-11-07 20:24:38 +00:00
|
|
|
const {state} = this.state;
|
2016-09-09 18:28:57 +00:00
|
|
|
|
2017-06-09 22:00:36 +00:00
|
|
|
const finalConfirmMessage = (confirmMessage || 'Click to confirm').trim();
|
|
|
|
const finalDoneMessage = 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 = (
|
2017-06-02 00:00:57 +00:00
|
|
|
<span className="warning" title="Click again to confirm">
|
2017-06-09 22:00:36 +00:00
|
|
|
<i className="fa fa-exclamation-circle"/>
|
|
|
|
{finalConfirmMessage
|
|
|
|
? <span className="space-left">{finalConfirmMessage}</span>
|
|
|
|
: ''
|
|
|
|
}
|
2016-09-09 18:28:57 +00:00
|
|
|
</span>
|
2017-03-03 20:09:08 +00:00
|
|
|
);
|
2016-11-07 20:24:38 +00:00
|
|
|
} else if (state === STATE_ASK) {
|
2017-06-02 00:00:57 +00:00
|
|
|
innerMsg = (
|
|
|
|
<span className="warning" title="Click again to confirm">
|
2017-06-09 22:00:36 +00:00
|
|
|
{finalConfirmMessage}
|
2017-06-02 00:00:57 +00:00
|
|
|
</span>
|
|
|
|
);
|
2016-11-07 20:24:38 +00:00
|
|
|
} else if (state === STATE_DONE) {
|
2017-06-09 22:00:36 +00:00
|
|
|
innerMsg = finalDoneMessage;
|
2016-09-09 18:28:57 +00:00
|
|
|
} else {
|
2017-03-03 20:09:08 +00:00
|
|
|
innerMsg = children;
|
2016-09-09 18:28:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
2017-03-09 06:23:23 +00:00
|
|
|
<Button onClick={this._handleClick}
|
|
|
|
disabled={disabled}
|
|
|
|
tabIndex={tabIndex}
|
|
|
|
{...other}>
|
2016-09-09 18:28:57 +00:00
|
|
|
{innerMsg}
|
2017-02-27 21:00:13 +00:00
|
|
|
</Button>
|
2017-03-03 20:09:08 +00:00
|
|
|
);
|
2016-09-09 18:28:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
PromptButton.propTypes = {
|
2017-03-08 05:52:17 +00:00
|
|
|
onClick: PropTypes.func,
|
2016-09-21 20:32:45 +00:00
|
|
|
addIcon: PropTypes.bool,
|
2017-03-08 05:52:17 +00:00
|
|
|
children: PropTypes.node,
|
|
|
|
disabled: PropTypes.bool,
|
|
|
|
confirmMessage: PropTypes.string,
|
|
|
|
doneMessage: PropTypes.string,
|
2017-03-09 06:23:23 +00:00
|
|
|
value: PropTypes.any,
|
|
|
|
tabIndex: PropTypes.number
|
2016-09-09 18:28:57 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
export default PromptButton;
|