Dispatch a 'cancel' event if the user hits escape

This commit is contained in:
Bryan Housel
2018-12-11 00:09:55 -05:00
parent 6cf1d63b48
commit ff646fa2c8
2 changed files with 11 additions and 1 deletions

View File

@@ -21,7 +21,7 @@ import { utilRebind, utilTriggerEvent } from '../util';
var _comboTimerID;
export function uiCombobox(context) {
var dispatch = d3_dispatch('accept');
var dispatch = d3_dispatch('accept', 'cancel');
var container = context.container();
var _suggestions = [];
var _values = [];
@@ -183,6 +183,9 @@ export function uiCombobox(context) {
function keyup() {
switch (d3_event.keyCode) {
case 27: // ⎋ Escape
container.selectAll('.combobox-option.selected').each(function (d) {
dispatch.call('cancel', this, d);
});
hide();
break;

View File

@@ -159,6 +159,13 @@ export function uiFieldLocalized(field, context) {
utilGetSetValue(input, tags.name);
dispatch.call('change', this, tags);
})
.on('cancel', function() {
// user hit escape, remove whatever is after the '-'
var name = utilGetSetValue(input);
name = name.split('-', 2)[0].trim();
utilGetSetValue(input, name);
dispatch.call('change', this, { name: name });
})
);
}
}