Skip to content

Commit a0d919b

Browse files
Satyarueckstiess
authored andcommitted
COMPASS-533: Escape key closes modal (#737)
* added onHide method to close modal on 'escape' key press * remove keyboard=false for drop index as well * added canHide bool for doc insert/clone when input focused * added some functional tests
1 parent d91ca3c commit a0d919b

File tree

9 files changed

+98
-17
lines changed

9 files changed

+98
-17
lines changed

src/internal-packages/crud/lib/component/insert-document-dialog.jsx

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class InsertDocumentDialog extends React.Component {
1919
*/
2020
constructor(props) {
2121
super(props);
22-
this.state = { open: false };
22+
this.state = { open: false, canHide: false };
2323
}
2424

2525
/**
@@ -63,6 +63,24 @@ class InsertDocumentDialog extends React.Component {
6363
this.setState({ open: false });
6464
}
6565

66+
/**
67+
* handle losing focus from element
68+
*/
69+
handleBlur() {
70+
this.setState({canHide: false});
71+
}
72+
73+
/**
74+
* handle hide event rather than cancel
75+
*/
76+
handleHide() {
77+
if (this.state.canHide) {
78+
this.setState({ open: false });
79+
} else {
80+
this.setState({ canHide: true });
81+
}
82+
}
83+
6684
/**
6785
* Handles completion of the document insert.
6886
*
@@ -88,12 +106,13 @@ class InsertDocumentDialog extends React.Component {
88106
*/
89107
render() {
90108
return (
91-
<Modal show={this.state.open} backdrop="static" keyboard={false}>
109+
<Modal show={this.state.open} backdrop="static"
110+
onHide={this.handleHide.bind(this)}>
92111
<Modal.Header>
93112
<Modal.Title>Insert Document</Modal.Title>
94113
</Modal.Header>
95114

96-
<Modal.Body>
115+
<Modal.Body onFocus={this.handleBlur.bind(this)} >
97116
<InsertDocument doc={this.state.doc} />
98117
<InsertDocumentFooter />
99118
</Modal.Body>

src/internal-packages/database/lib/components/create-collection-dialog.jsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ class CreateCollectionDialog extends React.Component {
6767
/**
6868
* When the cancel button is clicked.
6969
*/
70-
onCancelButtonClicked() {
70+
onHideModal() {
7171
this.setState({ open: false });
7272
}
7373

@@ -160,7 +160,7 @@ class CreateCollectionDialog extends React.Component {
160160
<Modal
161161
show={this.state.open}
162162
backdrop="static"
163-
keyboard={false}
163+
onHide={this.onHideModal.bind(this)}
164164
dialogClassName="create-collection-dialog">
165165

166166
<Modal.Header>
@@ -194,7 +194,7 @@ class CreateCollectionDialog extends React.Component {
194194
<TextButton
195195
className="btn btn-default"
196196
text="Cancel"
197-
clickHandler={this.onCancelButtonClicked.bind(this)} />
197+
clickHandler={this.onHideModal.bind(this)} />
198198
<TextButton
199199
className="btn btn-primary"
200200
dataTestId="create-collection-button"

src/internal-packages/database/lib/components/drop-collection-dialog.jsx

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ class DropCollectionDialog extends React.Component {
5656
/**
5757
* When the cancel button is clicked.
5858
*/
59-
onCancelButtonClicked() {
59+
onHideModal() {
6060
this.setState({ open: false });
6161
}
6262

@@ -97,7 +97,8 @@ class DropCollectionDialog extends React.Component {
9797
*/
9898
render() {
9999
return (
100-
<Modal show={this.state.open} backdrop="static" keyboard={false} dialogClassName="drop-collection-dialog">
100+
<Modal show={this.state.open} backdrop="static" onHide={this.onHideModal.bind(this)}
101+
dialogClassName="drop-collection-dialog">
101102
<Modal.Header>
102103
<Modal.Title>Drop Collection</Modal.Title>
103104
</Modal.Header>
@@ -133,7 +134,7 @@ class DropCollectionDialog extends React.Component {
133134
<TextButton
134135
className="btn btn-default"
135136
text="Cancel"
136-
clickHandler={this.onCancelButtonClicked.bind(this)} />
137+
clickHandler={this.onHideModal.bind(this)} />
137138
<button
138139
className="btn btn-primary"
139140
data-test-id="drop-collection-button"

src/internal-packages/indexes/lib/component/create-index-modal.jsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,6 @@ class CreateIndexModal extends React.Component {
228228
<Modal show={this.props.open}
229229
backdrop="static"
230230
dialogClassName="create-index-modal"
231-
keyboard={false}
232231
onHide={this.close.bind(this)} >
233232

234233
<div className="create-index-modal-content" data-test-id="create-index-modal">

src/internal-packages/indexes/lib/component/drop-index-modal.jsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,6 @@ class DropIndexModal extends React.Component {
127127
<Modal show={this.props.open}
128128
backdrop="static"
129129
dialogClassName="drop-index-modal"
130-
keyboard={false}
131130
onHide={this.handleClose.bind(this)} >
132131
<div className="drop-index-modal-content">
133132
<Modal.Header>

src/internal-packages/server-stats/lib/component/create-database-dialog.jsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ class CreateDatabaseDialog extends React.Component {
7070
/**
7171
* When the cancel button is clicked.
7272
*/
73-
onCancelButtonClicked() {
73+
onHideModal() {
7474
this.setState({ open: false });
7575
}
7676

@@ -183,7 +183,7 @@ class CreateDatabaseDialog extends React.Component {
183183
<Modal
184184
show={this.state.open}
185185
backdrop="static"
186-
keyboard={false}
186+
onHide={this.onHideModal.bind(this)}
187187
dialogClassName="create-collection-dialog">
188188

189189
<Modal.Header>
@@ -227,7 +227,7 @@ class CreateDatabaseDialog extends React.Component {
227227
<TextButton
228228
className="btn btn-default"
229229
text="Cancel"
230-
clickHandler={this.onCancelButtonClicked.bind(this)} />
230+
clickHandler={this.onHideModal.bind(this)} />
231231
<TextButton
232232
className="btn btn-primary"
233233
dataTestId="create-database-button"

src/internal-packages/server-stats/lib/component/drop-database-dialog.jsx

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ class DropDatabaseDialog extends React.Component {
4949
/**
5050
* When the cancel button is clicked.
5151
*/
52-
onCancelButtonClicked() {
52+
onHideModal() {
5353
this.setState({ open: false });
5454
}
5555

@@ -90,7 +90,8 @@ class DropDatabaseDialog extends React.Component {
9090
*/
9191
render() {
9292
return (
93-
<Modal show={this.state.open} backdrop="static" keyboard={false} dialogClassName="drop-database-dialog">
93+
<Modal show={this.state.open} backdrop="static" onHide={this.onHideModal.bind(this)}
94+
dialogClassName="drop-database-dialog">
9495
<Modal.Header>
9596
<Modal.Title>Drop Database</Modal.Title>
9697
</Modal.Header>
@@ -126,7 +127,7 @@ class DropDatabaseDialog extends React.Component {
126127
<TextButton
127128
className="btn btn-default"
128129
text="Cancel"
129-
clickHandler={this.onCancelButtonClicked.bind(this)} />
130+
clickHandler={this.onHideModal.bind(this)} />
130131
<button
131132
className="btn btn-primary"
132133
data-test-id="drop-database-button"

test/compass-functional.test.js

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,18 @@ describe('Compass Functional Test Suite #spectron', function() {
215215
});
216216
});
217217

218+
context('when the escape key is pressed', function() {
219+
it('closes the create databases modal', function() {
220+
return client
221+
.clickDatabasesTab()
222+
.clickCreateDatabaseButton()
223+
.waitForCreateDatabaseModal()
224+
.pressEscape()
225+
.waitForCreateDatabasesModalHidden()
226+
.should.eventually.be.true;
227+
});
228+
});
229+
218230
context('when the database name is invalid', function() {
219231
it('displays the error message', function() {
220232
return client
@@ -301,6 +313,17 @@ describe('Compass Functional Test Suite #spectron', function() {
301313
});
302314
});
303315

316+
context('when the escape key is pressed', function() {
317+
it('closes the drop databases modal', function() {
318+
return client
319+
.clickDeleteDatabaseButton('music')
320+
.waitForDropDatabaseModal()
321+
.pressEscape()
322+
.waitForDropDatabasesModalHidden()
323+
.should.eventually.be.true;
324+
});
325+
});
326+
304327
it('requires database name confirmation', function() {
305328
return client
306329
.clickCreateDatabaseButton()
@@ -451,6 +474,23 @@ describe('Compass Functional Test Suite #spectron', function() {
451474
.should.eventually.include('Aphex Twin');
452475
});
453476
});
477+
478+
context('when pressing escape key twice', function() {
479+
it('does not close the insert documents modal on first press', function() {
480+
return client
481+
.clickInsertDocumentButton()
482+
.waitForInsertDocumentModal()
483+
.pressEscape()
484+
.waitForInsertDocumentModal()
485+
.should.eventually.be.true;
486+
});
487+
it('closes the insert documents modal on second press', function() {
488+
return client
489+
.pressEscape()
490+
.waitForInsertDocumentModalHidden()
491+
.should.eventually.be.true;
492+
});
493+
});
454494
});
455495

456496
context('when editing a document', function() {

test/support/spectron-support.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -347,6 +347,18 @@ function addWaitCommands(client) {
347347
const row = `${base} ${selector('sortable-table-column-0')}[title=${name}]`;
348348
return this.waitForExistInCompass(row, true);
349349
});
350+
351+
client.addCommand('waitForCreateDatabasesModalHidden', function() {
352+
return this.waitForVisibleInCompass(selector('create-database-modal'), true);
353+
});
354+
355+
client.addCommand('waitForDropDatabasesModalHidden', function() {
356+
return this.waitForVisibleInCompass(selector('drop-database-modal'), true);
357+
});
358+
359+
client.addCommand('waitForInsertDocumentModalHidden', function() {
360+
return this.waitForVisibleInCompass(selector('insert-document-modal'), true);
361+
});
350362
}
351363

352364
/**
@@ -734,6 +746,15 @@ function addClickCommands(client) {
734746
// clickSaveFavoriteButton
735747
}
736748

749+
function addKeyPressCommands(client) {
750+
/**
751+
* Press escape
752+
*/
753+
client.addCommand('pressEscape', function() {
754+
return this.keys(['Escape']);
755+
});
756+
}
757+
737758
/**
738759
* Add commands to the client to get values from the screen.
739760
*
@@ -1335,6 +1356,7 @@ function launchCompass() {
13351356
addExtendedWaitCommands(client);
13361357
addWaitCommands(client);
13371358
addClickCommands(client);
1359+
addKeyPressCommands(client);
13381360
addGetCommands(client);
13391361
addInputCommands(client);
13401362
chaiAsPromised.transferPromiseness = app.transferPromiseness;

0 commit comments

Comments
 (0)