1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
|
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See License.txt for license information.
import React from 'react';
import {Modal} from 'react-bootstrap';
import PropTypes from 'prop-types';
import {FormattedMessage} from 'react-intl';
export default class ConfirmModal extends React.Component {
static propTypes = {
/*
* Set to show modal
*/
show: PropTypes.bool.isRequired,
/*
* Title to use for the modal
*/
title: PropTypes.node,
/*
* Message to display in the body of the modal
*/
message: PropTypes.node,
/*
* The CSS class to apply to the confirm button
*/
confirmButtonClass: PropTypes.string,
/*
* Text/jsx element on the confirm button
*/
confirmButtonText: PropTypes.node,
/*
* Text/jsx element on the cancel button
*/
cancelButtonText: PropTypes.node,
/*
* Set to show checkbox
*/
showCheckbox: PropTypes.bool,
/*
* Text/jsx element to display with the checkbox
*/
checkboxText: PropTypes.node,
/*
* Function called when the confirm button or ENTER is pressed. Passes `true` if the checkbox is checked
*/
onConfirm: PropTypes.func.isRequired,
/*
* Function called when the cancel button is pressed or the modal is hidden. Passes `true` if the checkbox is checked
*/
onCancel: PropTypes.func.isRequired
}
static defaultProps = {
title: '',
message: '',
confirmButtonClass: 'btn btn-primary',
confirmButtonText: ''
}
componentDidMount() {
if (this.props.show) {
document.addEventListener('keypress', this.handleKeypress);
}
}
componentWillUnmount() {
document.removeEventListener('keypress', this.handleKeypress);
}
componentWillReceiveProps(nextProps) {
if (this.props.show && !nextProps.show) {
document.removeEventListener('keypress', this.handleKeypress);
} else if (!this.props.show && nextProps.show) {
document.addEventListener('keypress', this.handleKeypress);
}
}
handleKeypress = (e) => {
if (e.key === 'Enter' && this.props.show) {
this.handleConfirm();
}
}
handleConfirm = () => {
const checked = this.refs.checkbox ? this.refs.checkbox.checked : false;
this.props.onConfirm(checked);
}
handleCancel = () => {
const checked = this.refs.checkbox ? this.refs.checkbox.checked : false;
this.props.onCancel(checked);
}
render() {
let checkbox;
if (this.props.showCheckbox) {
checkbox = (
<div className='checkbox text-right margin-bottom--none'>
<label>
<input
ref='checkbox'
type='checkbox'
/>
{this.props.checkboxText}
</label>
</div>
);
}
let cancelText;
if (this.props.cancelButtonText) {
cancelText = this.props.cancelButtonText;
} else {
cancelText = (
<FormattedMessage
id='confirm_modal.cancel'
defaultMessage='Cancel'
/>
);
}
return (
<Modal
className='modal-confirm'
show={this.props.show}
onHide={this.props.onCancel}
>
<Modal.Header closeButton={false}>
<Modal.Title>{this.props.title}</Modal.Title>
</Modal.Header>
<Modal.Body>
{this.props.message}
{checkbox}
</Modal.Body>
<Modal.Footer>
<button
type='button'
className='btn btn-default'
onClick={this.handleCancel}
>
{cancelText}
</button>
<button
type='button'
className={this.props.confirmButtonClass}
onClick={this.handleConfirm}
>
{this.props.confirmButtonText}
</button>
</Modal.Footer>
</Modal>
);
}
}
|