Add confirmOnEnter prop to confirm dialog

For some dialogs, the confirm event should fire on enter. This change
adds this optional property.

Checking the disabled property before firing the confirm event should
prevent unintended side effects throughout the app.

Change-Id: I5603065b37bf87b1c4b36ff54376fb4066869afe
This commit is contained in:
Kasper Nilsson
2018-02-12 16:24:31 -08:00
parent 076543f040
commit 7d50910d77
8 changed files with 45 additions and 3 deletions

View File

@@ -68,6 +68,7 @@ limitations under the License.
class="confirmDialog" class="confirmDialog"
disabled="[[!_hasNewGroupName]]" disabled="[[!_hasNewGroupName]]"
confirm-label="Create" confirm-label="Create"
confirm-on-enter
on-confirm="_handleCreateGroup" on-confirm="_handleCreateGroup"
on-cancel="_handleCloseCreate"> on-cancel="_handleCloseCreate">
<div class="header" slot="header"> <div class="header" slot="header">

View File

@@ -28,6 +28,7 @@ limitations under the License.
</style> </style>
<gr-confirm-dialog <gr-confirm-dialog
confirm-label="Delete [[_computeItemName(itemType)]]" confirm-label="Delete [[_computeItemName(itemType)]]"
confirm-on-enter
on-confirm="_handleConfirmTap" on-confirm="_handleConfirmTap"
on-cancel="_handleCancelTap"> on-cancel="_handleCancelTap">
<div class="header" slot="header">[[_computeItemName(itemType)]] Deletion</div> <div class="header" slot="header">[[_computeItemName(itemType)]] Deletion</div>

View File

@@ -167,6 +167,7 @@ limitations under the License.
id="confirmDeleteDialog" id="confirmDeleteDialog"
class="confirmDialog" class="confirmDialog"
confirm-label="Delete" confirm-label="Delete"
confirm-on-enter
on-cancel="_handleConfirmDialogCancel" on-cancel="_handleConfirmDialogCancel"
on-confirm="_handleDeleteConfirm"> on-confirm="_handleDeleteConfirm">
<div class="header" slot="header"> <div class="header" slot="header">
@@ -180,6 +181,7 @@ limitations under the License.
id="confirmDeleteEditDialog" id="confirmDeleteEditDialog"
class="confirmDialog" class="confirmDialog"
confirm-label="Delete" confirm-label="Delete"
confirm-on-enter
on-cancel="_handleConfirmDialogCancel" on-cancel="_handleConfirmDialogCancel"
on-confirm="_handleDeleteEditConfirm"> on-confirm="_handleDeleteEditConfirm">
<div class="header" slot="header"> <div class="header" slot="header">

View File

@@ -331,6 +331,7 @@ limitations under the License.
<gr-confirm-dialog <gr-confirm-dialog
id="confirmDiscardDialog" id="confirmDiscardDialog"
confirm-label="Discard" confirm-label="Discard"
confirm-on-enter
on-confirm="_handleConfirmDiscard" on-confirm="_handleConfirmDiscard"
on-cancel="_closeConfirmDiscardOverlay"> on-cancel="_closeConfirmDiscardOverlay">
<div class="header" slot="header"> <div class="header" slot="header">

View File

@@ -83,6 +83,7 @@ limitations under the License.
class="invisible dialog" class="invisible dialog"
disabled$="[[!_isValidPath(_path)]]" disabled$="[[!_isValidPath(_path)]]"
confirm-label="Open" confirm-label="Open"
confirm-on-enter
on-confirm="_handleOpenConfirm" on-confirm="_handleOpenConfirm"
on-cancel="_handleDialogCancel"> on-cancel="_handleDialogCancel">
<div class="header" slot="header"> <div class="header" slot="header">
@@ -100,6 +101,7 @@ limitations under the License.
class="invisible dialog" class="invisible dialog"
disabled$="[[!_isValidPath(_path)]]" disabled$="[[!_isValidPath(_path)]]"
confirm-label="Delete" confirm-label="Delete"
confirm-on-enter
on-confirm="_handleDeleteConfirm" on-confirm="_handleDeleteConfirm"
on-cancel="_handleDialogCancel"> on-cancel="_handleDialogCancel">
<div class="header" slot="header">Delete a file from the repo</div> <div class="header" slot="header">Delete a file from the repo</div>
@@ -115,6 +117,7 @@ limitations under the License.
class="invisible dialog" class="invisible dialog"
disabled$="[[!_computeRenameDisabled(_path, _newPath)]]" disabled$="[[!_computeRenameDisabled(_path, _newPath)]]"
confirm-label="Rename" confirm-label="Rename"
confirm-on-enter
on-confirm="_handleRenameConfirm" on-confirm="_handleRenameConfirm"
on-cancel="_handleDialogCancel"> on-cancel="_handleDialogCancel">
<div class="header" slot="header">Rename a file in the repo</div> <div class="header" slot="header">Rename a file in the repo</div>
@@ -134,6 +137,7 @@ limitations under the License.
id="restoreDialog" id="restoreDialog"
class="invisible dialog" class="invisible dialog"
confirm-label="Restore" confirm-label="Restore"
confirm-on-enter
on-confirm="_handleRestoreConfirm" on-confirm="_handleRestoreConfirm"
on-cancel="_handleDialogCancel"> on-cancel="_handleDialogCancel">
<div class="header" slot="header">Restore this file?</div> <div class="header" slot="header">Restore this file?</div>

View File

@@ -54,12 +54,12 @@ limitations under the License.
justify-content: flex-end; justify-content: flex-end;
} }
</style> </style>
<div class="container"> <div class="container" on-keydown="_handleKeydown">
<header><slot name="header"></slot></header> <header><slot name="header"></slot></header>
<main><slot name="main"></slot></main> <main><slot name="main"></slot></main>
<footer> <footer>
<gr-button link on-tap="_handleCancelTap">[[cancelLabel]]</gr-button> <gr-button link on-tap="_handleCancelTap">[[cancelLabel]]</gr-button>
<gr-button link primary on-tap="_handleConfirmTap" disabled="[[disabled]]"> <gr-button link primary on-tap="_handleConfirm" disabled="[[disabled]]">
[[confirmLabel]] [[confirmLabel]]
</gr-button> </gr-button>
</footer> </footer>

View File

@@ -42,13 +42,19 @@
type: Boolean, type: Boolean,
value: false, value: false,
}, },
confirmOnEnter: {
type: Boolean,
value: false,
},
}, },
hostAttributes: { hostAttributes: {
role: 'dialog', role: 'dialog',
}, },
_handleConfirmTap(e) { _handleConfirm(e) {
if (this.disabled) { return; }
e.preventDefault(); e.preventDefault();
this.fire('confirm', null, {bubbles: false}); this.fire('confirm', null, {bubbles: false});
}, },
@@ -57,5 +63,9 @@
e.preventDefault(); e.preventDefault();
this.fire('cancel', null, {bubbles: false}); this.fire('cancel', null, {bubbles: false});
}, },
_handleKeydown(e) {
if (this.confirmOnEnter && e.keyCode === 13) { this._handleConfirm(e); }
},
}); });
})(); })();

View File

@@ -34,11 +34,15 @@ limitations under the License.
<script> <script>
suite('gr-confirm-dialog tests', () => { suite('gr-confirm-dialog tests', () => {
let element; let element;
let sandbox;
setup(() => { setup(() => {
sandbox = sinon.sandbox.create();
element = fixture('basic'); element = fixture('basic');
}); });
teardown(() => { sandbox.restore(); });
test('events', done => { test('events', done => {
let numEvents = 0; let numEvents = 0;
function handler() { if (++numEvents == 2) { done(); } } function handler() { if (++numEvents == 2) { done(); } }
@@ -49,5 +53,24 @@ limitations under the License.
MockInteractions.tap(element.$$('gr-button[primary]')); MockInteractions.tap(element.$$('gr-button[primary]'));
MockInteractions.tap(element.$$('gr-button:not([primary])')); MockInteractions.tap(element.$$('gr-button:not([primary])'));
}); });
test('confirmOnEnter', () => {
element.confirmOnEnter = false;
const handleConfirmStub = sandbox.stub(element, '_handleConfirm');
const handleKeydownSpy = sandbox.spy(element, '_handleKeydown');
MockInteractions.pressAndReleaseKeyOn(element.$$('main'),
13, null, 'enter');
flushAsynchronousOperations();
assert.isTrue(handleKeydownSpy.called);
assert.isFalse(handleConfirmStub.called);
element.confirmOnEnter = true;
MockInteractions.pressAndReleaseKeyOn(element.$$('main'),
13, null, 'enter');
flushAsynchronousOperations();
assert.isTrue(handleConfirmStub.called);
});
}); });
</script> </script>