insomnia/app/ui/components/base/prompt-button.js

133 lines
3.1 KiB
JavaScript
Raw Normal View History

2017-08-10 01:56:27 +00:00
import React, {PureComponent} from 'react';
import PropTypes from 'prop-types';
import autobind from 'autobind-decorator';
import Button from './button';
2016-09-09 18:28:57 +00:00
const STATE_DEFAULT = 'default';
const STATE_ASK = 'ask';
const STATE_DONE = 'done';
@autobind
class PromptButton extends PureComponent {
constructor (props) {
super(props);
this.state = {
state: STATE_DEFAULT
};
}
2016-09-09 18:28:57 +00:00
_confirm (...args) {
2016-09-09 18:28:57 +00:00
// Clear existing timeouts
clearTimeout(this._triggerTimeout);
2016-09-09 18:28:57 +00:00
// Fire the click handler
this.props.onClick(...args);
// 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
this._triggerTimeout = setTimeout(() => {
this.setState({state: STATE_DEFAULT});
}, 2000);
2016-09-09 18:28:57 +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
this.setState({state: STATE_ASK});
2016-09-09 18:28:57 +00:00
// Set a timeout to hide the confirmation
this._triggerTimeout = setTimeout(() => {
this.setState({state: STATE_DEFAULT});
2016-09-09 18:28:57 +00:00
}, 2000);
}
_handleClick (...args) {
const {state} = this.state;
if (state === STATE_ASK) {
this._confirm(...args);
} else if (state === STATE_DEFAULT) {
this._ask(...args);
2016-09-09 18:28:57 +00:00
} else {
// Do nothing
2016-09-09 18:28:57 +00:00
}
}
2016-09-09 18:28:57 +00:00
componentWillUnmount () {
clearTimeout(this._triggerTimeout);
clearTimeout(this._doneTimeout);
2016-09-09 18:28:57 +00:00
}
render () {
const {
onClick, // eslint-disable-line no-unused-vars
children,
addIcon,
disabled,
confirmMessage,
doneMessage,
tabIndex,
...other
} = this.props;
const {state} = this.state;
2016-09-09 18:28:57 +00:00
const finalConfirmMessage = (confirmMessage || 'Click to confirm').trim();
const finalDoneMessage = doneMessage || 'Done';
2016-09-09 18:28:57 +00:00
let innerMsg;
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">
<i className="fa fa-exclamation-circle"/>
{finalConfirmMessage
? <span className="space-left">{finalConfirmMessage}</span>
: ''
}
2016-09-09 18:28:57 +00:00
</span>
);
} else if (state === STATE_ASK) {
2017-06-02 00:00:57 +00:00
innerMsg = (
<span className="warning" title="Click again to confirm">
{finalConfirmMessage}
2017-06-02 00:00:57 +00:00
</span>
);
} else if (state === STATE_DONE) {
innerMsg = finalDoneMessage;
2016-09-09 18:28:57 +00:00
} else {
innerMsg = children;
2016-09-09 18:28:57 +00:00
}
return (
<Button onClick={this._handleClick}
disabled={disabled}
tabIndex={tabIndex}
{...other}>
2016-09-09 18:28:57 +00:00
{innerMsg}
</Button>
);
2016-09-09 18:28:57 +00:00
}
}
PromptButton.propTypes = {
onClick: PropTypes.func,
2016-09-21 20:32:45 +00:00
addIcon: PropTypes.bool,
children: PropTypes.node,
disabled: PropTypes.bool,
confirmMessage: PropTypes.string,
doneMessage: PropTypes.string,
value: PropTypes.any,
tabIndex: PropTypes.number
2016-09-09 18:28:57 +00:00
};
export default PromptButton;