Skip to content

Commit

Permalink
Merge "[INTERNAL] sap.m.Input: showSuggestions setter fixed" into rel…
Browse files Browse the repository at this point in the history
…-1.65
  • Loading branch information
Gergana Kremenska authored and Gerrit Code Review committed Jul 1, 2019
2 parents 8e3ac7b + 1ca4632 commit e22288b
Show file tree
Hide file tree
Showing 2 changed files with 151 additions and 68 deletions.
158 changes: 90 additions & 68 deletions src/sap.m/src/sap/m/Input.js
Original file line number Diff line number Diff line change
Expand Up @@ -1337,12 +1337,17 @@ function(
* @param {boolean} bValue Show suggestions.
* @return {sap.m.Input} this Input instance for chaining.
*/
Input.prototype.setShowSuggestion = function(bValue){
Input.prototype.setShowSuggestion = function(bValue) {
this.setProperty("showSuggestion", bValue, true);

if (bValue) {
this._oSuggPopover = this._getSuggestionsPopover();
this._oSuggPopover._iPopupListSelectedIndex = -1;
if (!this._oSuggPopover._oPopover) {
this._createSuggestionsPopoverPopup();
this._synchronizeSuggestions();
this._createSuggestionPopupContent();
}
} else {
if (this._oSuggPopover) {
this._oSuggPopover._destroySuggestionPopup();
Expand Down Expand Up @@ -1985,7 +1990,7 @@ function(
}

// fires suggest event when startSuggestion is set to 0 and input has no text
if (!this._bPopupHasFocus && !this.getStartSuggestion() && !this.getValue()) {
if (!this._bPopupHasFocus && !this.getStartSuggestion() && !this.getValue() && this.getShowSuggestion()) {
this._triggerSuggest(this.getValue());
}
this._bPopupHasFocus = undefined;
Expand Down Expand Up @@ -2403,7 +2408,7 @@ function(
this._oSuggPopover._oPopupInput = this._createPopupInput();
}

this._oSuggPopover._createSuggestionPopup(); // TODO move this call to SuggestionsPopover constructor
this._createSuggestionsPopoverPopup();

this._oSuggPopover._bAutocompleteEnabled = this.getAutocomplete();

Expand All @@ -2422,82 +2427,99 @@ function(
if (this.getShowTableSuggestionValueHelp()) {
this._addShowMoreButton();
}
}

if (this._bUseDialog) {
this._oSuggPopover._oPopover
.attachBeforeClose(function () {
// call _getInputValue to apply the maxLength to the typed value
this.setDOMValue(this
._getInputValue(this._oSuggPopover._oPopupInput
.getValue()));
this.onChange();

if (this instanceof sap.m.MultiInput && this._bUseDialog) {
this._onDialogClose();
}
return this._oSuggPopover;
};

}, this)
.attachAfterClose(function() {
var oList = this._oSuggPopover._oList;
/**
* Creates a suggestion popover popup.
*
* @private
*/
Input.prototype._createSuggestionsPopoverPopup = function () {

if (Table && !(oList instanceof Table)) {
oList.destroyItems();
} else {
oList.removeSelections(true);
}
}.bind(this))
.attachAfterOpen(function () {
var sValue = this.getValue();

this._oSuggPopover._oPopupInput.setValue(sValue);
this._triggerSuggest(sValue);
this._refreshListItems();
}, this)
.attachBeforeOpen(function() {
// set the same placeholder and maxLength as the original input
this._oSuggPopover._oPopupInput.setPlaceholder(this.getPlaceholder());
this._oSuggPopover._oPopupInput.setMaxLength(this.getMaxLength());
}, this);

this._oSuggPopover._oPopover.getBeginButton()
.attachPress(function() {
this._closeSuggestionPopup();
}, this);
} else {
this._oSuggPopover._oPopover
.attachAfterClose(function() {
if (!this._oSuggPopover) {
return;
}

this._updateSelectionFromList();
var oSuggPopover = this._oSuggPopover;
oSuggPopover._createSuggestionPopup();

var oList = this._oSuggPopover._oList;
var oPopover = oSuggPopover._oPopover;

// only destroy items in simple suggestion mode
if (oList instanceof Table) {
oList.removeSelections(true);
} else {
oList.destroyItems();
}
if (this._bUseDialog) {
oPopover
.attachBeforeClose(function () {
// call _getInputValue to apply the maxLength to the typed value
this.setDOMValue(this
._getInputValue(this._oSuggPopover._oPopupInput
.getValue()));
this.onChange();

this._oSuggPopover._deregisterResize();
}.bind(this))
.attachBeforeOpen(function () {
this._oSuggPopover._sPopoverContentWidth = this.getMaxSuggestionWidth();
this._oSuggPopover._bEnableHighlighting = this.getEnableSuggestionsHighlighting();
this._oSuggPopover._bAutocompleteEnabled = this.getAutocomplete();
this._oSuggPopover._bIsInputIncrementalType = this._isIncrementalType();
this._sBeforeSuggest = this.getValue();
this._oSuggPopover._resizePopup();
this._oSuggPopover._registerResize();
}, this);
}
if (this instanceof sap.m.MultiInput && this._bUseDialog) {
this._onDialogClose();
}

// add popup to a hidden aggregation to also propagate the model and bindings to the content of the popover
this.setAggregation("_suggestionPopup", this._oSuggPopover._oPopover);
}, this)
.attachAfterClose(function() {
var oList = this._oSuggPopover._oList;

this._oSuggestionPopup = this._oSuggPopover._oPopover; // for backward compatibility (used in some other controls)
if (Table && !(oList instanceof Table)) {
oList.destroyItems();
} else {
oList.removeSelections(true);
}
}.bind(this))
.attachAfterOpen(function () {
var sValue = this.getValue();

this._oSuggPopover._oPopupInput.setValue(sValue);
this._triggerSuggest(sValue);
this._refreshListItems();
}, this)
.attachBeforeOpen(function() {
// set the same placeholder and maxLength as the original input
this._oSuggPopover._oPopupInput.setPlaceholder(this.getPlaceholder());
this._oSuggPopover._oPopupInput.setMaxLength(this.getMaxLength());
}, this);

oPopover.getBeginButton()
.attachPress(function() {
this._closeSuggestionPopup();
}, this);
} else {
oPopover
.attachAfterClose(function() {

this._updateSelectionFromList();

var oList = this._oSuggPopover._oList;

// only destroy items in simple suggestion mode
if (oList instanceof Table) {
oList.removeSelections(true);
} else {
oList.destroyItems();
}

this._oSuggPopover._deregisterResize();
}.bind(this))
.attachBeforeOpen(function () {
this._oSuggPopover._sPopoverContentWidth = this.getMaxSuggestionWidth();
this._oSuggPopover._bEnableHighlighting = this.getEnableSuggestionsHighlighting();
this._oSuggPopover._bAutocompleteEnabled = this.getAutocomplete();
this._oSuggPopover._bIsInputIncrementalType = this._isIncrementalType();
this._sBeforeSuggest = this.getValue();
this._oSuggPopover._resizePopup();
this._oSuggPopover._registerResize();
}, this);
}

return this._oSuggPopover;
// add popup to a hidden aggregation to also propagate the model and bindings to the content of the popover
this.setAggregation("_suggestionPopup", oPopover);

this._oSuggestionPopup = oPopover; // for backward compatibility (used in some other controls)
};

/**
Expand Down
61 changes: 61 additions & 0 deletions src/sap.m/test/sap/m/qunit/Input.qunit.js
Original file line number Diff line number Diff line change
Expand Up @@ -2669,6 +2669,67 @@ sap.ui.define([
oInput = null;
});

QUnit.test("Set showSuggestions", function (assert) {

// Arrange
var oInput = new Input({
startSuggestion: 0,
showSuggestion: true,
suggestionItems: [
new Item({ text: "test" })
]
});
var fnTriggerSuggestSpy = sinon.spy(oInput, "_triggerSuggest");
oInput.placeAt("content");
sap.ui.getCore().applyChanges();

oInput.onfocusin();
oInput._$input.focus().val("te").trigger("input");

this.clock.tick(300);

// Assert
assert.ok(fnTriggerSuggestSpy.called, "Should have triggered suggest.");
assert.ok(oInput._oSuggPopover._oPopover, "Should have suggestions popover.");
assert.ok(oInput._oSuggPopover._oPopover.isOpen(), "Should have opened suggestions popover.");
assert.ok(oInput._oSuggPopover._oList, "Should have created a list with suggestions.");

// Act
oInput._$input.val("");
oInput.onfocusout();
oInput.setShowSuggestion(false);
fnTriggerSuggestSpy.reset();
oInput.onfocusin();
oInput._$input.focus().val("te").trigger("input");

this.clock.tick(300);

// Assert
assert.notOk(fnTriggerSuggestSpy.called, "Should have NOT triggered suggest.");
assert.notOk(oInput._oSuggPopover._oPopover, "Should NOT have suggestions popover.");
assert.notOk(oInput._oSuggPopover._oList, "Should have NOT created a list with suggestions.");

// Act
oInput._$input.val("");
oInput.onfocusout();
oInput.setShowSuggestion(true);
fnTriggerSuggestSpy.reset();
oInput.onfocusin();
oInput._$input.focus().val("te").trigger("input");

this.clock.tick(300);

// Assert
assert.ok(fnTriggerSuggestSpy.called, "Should have triggered suggest.");
assert.ok(oInput._oSuggPopover._oPopover, "Should have suggestions popover.");
assert.ok(oInput._oSuggPopover._oPopover.isOpen(), "Should have opened suggestions popover.");
assert.ok(oInput._oSuggPopover._oList, "Should have created a list with suggestions.");

// Cleanup
fnTriggerSuggestSpy.restore();
oInput.destroy();
});

QUnit.module("Key and Value");

function createInputWithSuggestions () {
Expand Down

0 comments on commit e22288b

Please sign in to comment.