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

89 lines
1.9 KiB
JavaScript
Raw Normal View History

2019-09-05 15:36:11 +12:00
import React from 'react';
import { i18n } from '../../locale';
import Button from '../Button';
export default class ConfirmationButton extends React.Component {
2019-09-05 15:36:11 +12:00
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);
}
handleClick = () => {
const { confirming } = this.state;
const { onConfirm } = this.props;
if (confirming) {
2019-09-05 15:36:11 +12:00
this.setState({ confirming: false });
onConfirm();
2019-09-05 15:36:11 +12:00
} else {
this.setState({ confirming: true });
}
}
handleMouseEnter = () => {
2019-09-05 15:36:11 +12:00
this.setState({ timing_out: false });
clearTimeout(this.unconfirmTimer);
}
handleMouseLeave = () => {
const { confirming } = this.state;
if (confirming) {
2019-09-05 15:36:11 +12:00
this.setState({ timing_out: true });
this.unconfirmTimer = setTimeout(
() => {
this.setState({ confirming: false });
},
2000,
);
}
}
2016-10-17 14:27:48 +13:00
render = () => {
const {
confirmingContent,
working,
workingContent,
onConfirm,
...rest
} = this.props;
2019-09-05 15:36:11 +12:00
let { content } = this.props;
const {
confirming,
timing_out,
} = this.state;
2016-10-17 14:27:48 +13:00
if (confirming) {
content = confirmingContent || i18n('actions.confirm');
2019-09-05 15:36:11 +12:00
}
if (working && workingContent) {
content = workingContent;
2019-09-05 15:36:11 +12:00
}
2019-09-05 15:36:11 +12:00
return (
<Button
type="destructive"
confirming={confirming}
timingOut={timing_out}
working={working}
onClick={this.handleClick}
onMouseLeave={this.handleMouseLeave}
onMouseEnter={this.handleMouseEnter}
{...rest}
2019-09-05 15:36:11 +12:00
>
{content}
</Button>
2019-09-05 15:36:11 +12:00
);
}
}