Skip to content

Commit

Permalink
ComboBox: if selectedIndex or selectedItem is set in an Event.CLOSE l…
Browse files Browse the repository at this point in the history
…istener, it should take precedence over the internal pendingSelectedIndex
  • Loading branch information
joshtynjala committed Apr 1, 2024
1 parent 1c072da commit e35cc40
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 6 deletions.
20 changes: 14 additions & 6 deletions src/feathers/controls/ComboBox.hx
Original file line number Diff line number Diff line change
Expand Up @@ -1347,11 +1347,21 @@ class ComboBox extends FeathersControl implements IIndexSelector implements IDat
private function comboBox_popUpAdapter_closeHandler(event:Event):Void {
this.popUpAdapter.removeEventListener(Event.OPEN, comboBox_popUpAdapter_openHandler);
this.popUpAdapter.removeEventListener(Event.CLOSE, comboBox_popUpAdapter_closeHandler);
var pendingSelectedIndex = this.pendingSelectedIndex;
var filterText = this._filterText;
this.pendingSelectedIndex = -1;
this._filterText = null;
var oldSelectedIndex = this._selectedIndex;
FeathersEvent.dispatch(this, Event.CLOSE);
if (this._selectedIndex != oldSelectedIndex) {
// it was changed in the Event.CLOSE listener, so it takes
// precendence over pendingSelectedIndex
pendingSelectedIndex = this._selectedIndex;
}

var newSelectedItem:Dynamic = null;
if (this.pendingSelectedIndex != -1) {
newSelectedItem = this._dataProvider.get(this.pendingSelectedIndex);
if (pendingSelectedIndex != -1) {
newSelectedItem = this._dataProvider.get(pendingSelectedIndex);
} else if (this._filterText != null) {
var filterText = this._filterText.toLowerCase();
if (this._dataProvider != null && this._dataProvider.length > 0) {
Expand All @@ -1371,11 +1381,9 @@ class ComboBox extends FeathersControl implements IIndexSelector implements IDat
return;
}
var customSelectedItem:Dynamic = null;
if (this._allowCustomUserValue && newSelectedItem == null && this._filterText != null && this._filterText.length > 0) {
customSelectedItem = this.textToItem(this._filterText);
if (this._allowCustomUserValue && newSelectedItem == null && filterText != null && filterText.length > 0) {
customSelectedItem = this.textToItem(filterText);
}
this._filterText = null;
this.pendingSelectedIndex = -1;
if (this._dataProvider != null) {
this._dataProvider.refresh();
}
Expand Down
36 changes: 36 additions & 0 deletions test/src/feathers/controls/ComboBoxTest.hx
Original file line number Diff line number Diff line change
Expand Up @@ -565,4 +565,40 @@ class ComboBoxTest extends Test {
Assert.notNull(textInput);
Assert.equals(customVariant2, textInput.variant);
}

public function testSetSelectedIndexInCloseListener():Void {
var item1 = {text: "One"};
var item2 = {text: "Two"};
var item3 = {text: "Three"};
this._comboBox.dataProvider = new ArrayCollection([item1, item2, item3]);
var oldSelectedIndex = 2;
var newSelectedIndex = 1;
this._comboBox.selectedIndex = oldSelectedIndex;
this._comboBox.addEventListener(Event.CLOSE, event -> {
this._comboBox.selectedIndex = newSelectedIndex;
});
this._comboBox.validateNow();
this._comboBox.openListView();
Assert.equals(oldSelectedIndex, this._comboBox.selectedIndex);
this._comboBox.closeListView();
Assert.equals(newSelectedIndex, this._comboBox.selectedIndex);
}

public function testSetSelectedItemInCloseListener():Void {
var item1 = {text: "One"};
var item2 = {text: "Two"};
var item3 = {text: "Three"};
this._comboBox.dataProvider = new ArrayCollection([item1, item2, item3]);
var oldSelectedItem = item2;
var newSelectedItem = item3;
this._comboBox.selectedItem = oldSelectedItem;
this._comboBox.addEventListener(Event.CLOSE, event -> {
this._comboBox.selectedItem = newSelectedItem;
});
this._comboBox.validateNow();
this._comboBox.openListView();
Assert.equals(oldSelectedItem, this._comboBox.selectedItem);
this._comboBox.closeListView();
Assert.equals(newSelectedItem, this._comboBox.selectedItem);
}
}
15 changes: 15 additions & 0 deletions test/src/feathers/controls/PopUpDatePickerTest.hx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

package feathers.controls;

import openfl.events.Event;
import openfl.Lib;
import utest.Assert;
import utest.Test;
Expand Down Expand Up @@ -143,4 +144,18 @@ class PopUpDatePickerTest extends Test {
Assert.notNull(textInput);
Assert.equals(customVariant2, textInput.variant);
}

public function testSetSelectedDateInCloseListener():Void {
var oldDate = new Date(2011, 0, 1, 0, 0, 0);
var newDate = new Date(2012, 0, 1, 0, 0, 0);
this._popUpDatePicker.selectedDate = oldDate;
this._popUpDatePicker.addEventListener(Event.CLOSE, event -> {
this._popUpDatePicker.selectedDate = newDate;
});
this._popUpDatePicker.validateNow();
this._popUpDatePicker.openDatePicker();
Assert.equals(oldDate, this._popUpDatePicker.selectedDate);
this._popUpDatePicker.closeDatePicker();
Assert.equals(newDate, this._popUpDatePicker.selectedDate);
}
}
36 changes: 36 additions & 0 deletions test/src/feathers/controls/PopUpListViewTest.hx
Original file line number Diff line number Diff line change
Expand Up @@ -513,4 +513,40 @@ class PopUpListViewTest extends Test {
Assert.notNull(button);
Assert.equals(customVariant2, button.variant);
}

public function testSetSelectedIndexInCloseListener():Void {
var item1 = {text: "One"};
var item2 = {text: "Two"};
var item3 = {text: "Three"};
this._listView.dataProvider = new ArrayCollection([item1, item2, item3]);
var oldSelectedIndex = 2;
var newSelectedIndex = 1;
this._listView.selectedIndex = oldSelectedIndex;
this._listView.addEventListener(Event.CLOSE, event -> {
this._listView.selectedIndex = newSelectedIndex;
});
this._listView.validateNow();
this._listView.openListView();
Assert.equals(oldSelectedIndex, this._listView.selectedIndex);
this._listView.closeListView();
Assert.equals(newSelectedIndex, this._listView.selectedIndex);
}

public function testSetSelectedItemInCloseListener():Void {
var item1 = {text: "One"};
var item2 = {text: "Two"};
var item3 = {text: "Three"};
this._listView.dataProvider = new ArrayCollection([item1, item2, item3]);
var oldSelectedItem = item2;
var newSelectedItem = item3;
this._listView.selectedItem = oldSelectedItem;
this._listView.addEventListener(Event.CLOSE, event -> {
this._listView.selectedItem = newSelectedItem;
});
this._listView.validateNow();
this._listView.openListView();
Assert.equals(oldSelectedItem, this._listView.selectedItem);
this._listView.closeListView();
Assert.equals(newSelectedItem, this._listView.selectedItem);
}
}

0 comments on commit e35cc40

Please sign in to comment.