forked from mattermost/mattermost-webapp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
confirm_modal.tsx
228 lines (197 loc) · 6.03 KB
/
confirm_modal.tsx
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
// 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 {FormattedMessage} from 'react-intl';
type Props = {
/*
* Set to show modal
*/
show: boolean;
/*
* Title to use for the modal
*/
title?: React.ReactNode;
/*
* Message to display in the body of the modal
*/
message?: React.ReactNode;
/*
* The CSS class to apply to the confirm button
*/
confirmButtonClass?: string;
/*
* The CSS class to apply to the modal
*/
modalClass?: string;
/*
* Text/jsx element on the confirm button
*/
confirmButtonText?: React.ReactNode;
/*
* Text/jsx element on the cancel button
*/
cancelButtonText?: React.ReactNode;
/*
* Set to show checkbox
*/
showCheckbox?: boolean;
/*
* Text/jsx element to display with the checkbox
*/
checkboxText?: React.ReactNode;
/*
* Function called when the confirm button or ENTER is pressed. Passes `true` if the checkbox is checked
*/
onConfirm: (checked: boolean) => void;
/*
* Function called when the cancel button is pressed or the modal is hidden. Passes `true` if the checkbox is checked
*/
onCancel: (checked: boolean) => void;
/**
* Function called when modal is dismissed
*/
onExited?: () => void;
/*
* Set to hide the cancel button
*/
hideCancel?: boolean;
};
type State = {
checked: boolean;
}
export default class ConfirmModal extends React.Component<Props, State> {
static defaultProps = {
title: '',
message: '',
confirmButtonClass: 'btn btn-primary',
confirmButtonText: '',
modalClass: '',
}
constructor(props: Props) {
super(props);
this.state = {
checked: false,
};
}
componentDidMount() {
if (this.props.show) {
document.addEventListener('keydown', this.handleKeypress);
}
}
componentWillUnmount() {
document.removeEventListener('keydown', this.handleKeypress);
}
shouldComponentUpdate(nextProps: Props, nextState: State) {
return (
nextProps.show !== this.props.show ||
nextState.checked !== this.state.checked
);
}
componentDidUpdate(prevProps: Props) {
if (prevProps.show && !this.props.show) {
document.removeEventListener('keydown', this.handleKeypress);
} else if (!prevProps.show && this.props.show) {
document.addEventListener('keydown', this.handleKeypress);
}
}
handleKeypress = (e: KeyboardEvent) => {
if (e.key === 'Enter' && this.props.show) {
const cancelButton = document.getElementById('cancelModalButton');
if (cancelButton && cancelButton === document.activeElement) {
this.handleCancel();
} else {
this.handleConfirm();
}
}
}
handleCheckboxChange = (e: React.ChangeEvent<HTMLInputElement>) => {
this.setState({checked: e.target.checked});
}
handleConfirm = () => {
this.props.onConfirm(this.state.checked);
}
handleCancel = () => {
this.props.onCancel(this.state.checked);
}
render() {
let checkbox;
if (this.props.showCheckbox) {
checkbox = (
<div className='checkbox text-right mb-0'>
<label>
<input
type='checkbox'
onChange={this.handleCheckboxChange}
checked={this.state.checked}
/>
{this.props.checkboxText}
</label>
</div>
);
}
let cancelText;
if (this.props.cancelButtonText) {
cancelText = this.props.cancelButtonText;
} else {
cancelText = (
<FormattedMessage
id='confirm_modal.cancel'
defaultMessage='Cancel'
/>
);
}
let cancelButton;
if (!this.props.hideCancel) {
cancelButton = (
<button
type='button'
className='btn btn-link btn-cancel'
onClick={this.handleCancel}
id='cancelModalButton'
>
{cancelText}
</button>
);
}
return (
<Modal
className={'modal-confirm ' + this.props.modalClass}
dialogClassName='a11y__modal'
show={this.props.show}
onHide={this.props.onCancel}
onExited={this.props.onExited}
id='confirmModal'
role='dialog'
aria-modal={true}
aria-labelledby='confirmModalLabel'
aria-describedby='confirmModalBody'
>
<Modal.Header closeButton={false}>
<Modal.Title
componentClass='h1'
id='confirmModalLabel'
>
{this.props.title}
</Modal.Title>
</Modal.Header>
<Modal.Body id='confirmModalBody'>
{this.props.message}
{checkbox}
</Modal.Body>
<Modal.Footer>
{cancelButton}
<button
autoFocus={true}
type='button'
className={this.props.confirmButtonClass}
onClick={this.handleConfirm}
id='confirmModalButton'
>
{this.props.confirmButtonText}
</button>
</Modal.Footer>
</Modal>
);
}
}