Files
Iris/src/js/components/Fields/ConfirmationButton.js

81 lines
1.8 KiB
JavaScript
Raw Normal View History

2019-09-05 15:36:11 +12:00
import React from 'react';
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;
}
2019-09-05 15:36:11 +12:00
componentWillUnmount() {
clearTimeout(this.unconfirmTimer);
}
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 });
}
}
2019-09-05 15:36:11 +12:00
handleMouseEnter(e) {
this.setState({ timing_out: false });
clearTimeout(this.unconfirmTimer);
}
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';
}
}
2019-09-05 15:36:11 +12:00
if (this.props.working) {
className += ' button--working';
2019-09-05 15:36:11 +12:00
if (this.props.workingContent) {
content = this.props.workingContent;
}
}
2019-09-05 15:36:11 +12:00
if (this.props.className) {
className += ` ${this.props.className}`;
}
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>
);
}
}