2016-10-17 10:39:11 +13:00
|
|
|
|
2019-09-05 15:36:11 +12:00
|
|
|
import React from 'react';
|
2016-10-17 10:39:11 +13:00
|
|
|
|
2019-09-05 15:36:11 +12:00
|
|
|
export default class ConfirmationButton extends React.Component {
|
|
|
|
|
constructor(props) {
|
|
|
|
|
super(props);
|
|
|
|
|
this.state = {
|
|
|
|
|
timing_out: false,
|
|
|
|
|
confirming: false,
|
|
|
|
|
};
|
|
|
|
|
this.confirming = false;
|
|
|
|
|
this.unconfirmTimer = false;
|
|
|
|
|
}
|
2016-10-17 10:39:11 +13:00
|
|
|
|
2019-09-05 15:36:11 +12:00
|
|
|
componentWillUnmount() {
|
|
|
|
|
clearTimeout(this.unconfirmTimer);
|
|
|
|
|
}
|
2016-10-17 10:39:11 +13:00
|
|
|
|
2019-09-05 15:36:11 +12:00
|
|
|
handleClick(e) {
|
|
|
|
|
if (this.state.confirming) {
|
|
|
|
|
this.setState({ confirming: false });
|
|
|
|
|
this.props.onConfirm();
|
|
|
|
|
} else {
|
|
|
|
|
this.setState({ confirming: true });
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-10-17 22:00:29 +13:00
|
|
|
|
2019-09-05 15:36:11 +12:00
|
|
|
handleMouseEnter(e) {
|
|
|
|
|
this.setState({ timing_out: false });
|
|
|
|
|
clearTimeout(this.unconfirmTimer);
|
|
|
|
|
}
|
2016-10-17 10:39:11 +13:00
|
|
|
|
2019-09-05 15:36:11 +12:00
|
|
|
handleMouseLeave(e) {
|
|
|
|
|
if (this.state.confirming) {
|
|
|
|
|
this.setState({ timing_out: true });
|
|
|
|
|
this.unconfirmTimer = setTimeout(
|
|
|
|
|
() => {
|
|
|
|
|
this.setState({ confirming: false });
|
|
|
|
|
},
|
|
|
|
|
2000,
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-10-17 14:27:48 +13:00
|
|
|
|
2019-09-05 15:36:11 +12:00
|
|
|
render() {
|
|
|
|
|
let className = 'button';
|
|
|
|
|
let { content } = this.props;
|
2016-10-17 14:27:48 +13:00
|
|
|
|
2019-09-05 15:36:11 +12:00
|
|
|
if (this.state.confirming) {
|
|
|
|
|
className += ' button--confirming';
|
|
|
|
|
content = this.props.confirmingContent;
|
|
|
|
|
if (this.state.timing_out) {
|
|
|
|
|
className += ' button--timing-out';
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-10-17 10:39:11 +13:00
|
|
|
|
2019-09-05 15:36:11 +12:00
|
|
|
if (this.props.working) {
|
|
|
|
|
className += ' button--working';
|
2016-10-17 10:39:11 +13:00
|
|
|
|
2019-09-05 15:36:11 +12:00
|
|
|
if (this.props.workingContent) {
|
|
|
|
|
content = this.props.workingContent;
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-10-17 10:39:11 +13:00
|
|
|
|
2019-09-05 15:36:11 +12:00
|
|
|
if (this.props.className) {
|
|
|
|
|
className += ` ${this.props.className}`;
|
|
|
|
|
}
|
2018-04-16 15:51:35 +12:00
|
|
|
|
2019-09-05 15:36:11 +12:00
|
|
|
return (
|
|
|
|
|
<button
|
|
|
|
|
className={className}
|
|
|
|
|
onClick={(e) => this.handleClick(e)}
|
|
|
|
|
onMouseLeave={(e) => this.handleMouseLeave(e)}
|
|
|
|
|
onMouseEnter={(e) => this.handleMouseEnter(e)}
|
|
|
|
|
>
|
|
|
|
|
{ content }
|
|
|
|
|
</button>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|