Skip to content

Commit

Permalink
SelectBox: Skip custom item creating if customItemCreateEvent is '' a…
Browse files Browse the repository at this point in the history
…nd component loses focus (T1269852)

Co-authored-by: marker dao ® <youdontknow@marker-dao.eth>
  • Loading branch information
marker-dao and marker dao ® authored Mar 6, 2025
1 parent 82f607a commit 5842726
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 10 deletions.
29 changes: 19 additions & 10 deletions packages/devextreme/js/__internal/ui/m_select_box.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ const SelectBox = (DropDownList as any).inherit({
this._resetCaretPosition(true);
}

parent.tab && parent.tab.apply(this, arguments);
parent.tab?.apply(this, arguments);

if (!popupHasFocusableElements) {
this._cancelSearchIfNeed();
Expand Down Expand Up @@ -85,18 +85,18 @@ const SelectBox = (DropDownList as any).inherit({
},
rightArrow() {
searchIfNeeded();
parent.rightArrow && parent.rightArrow.apply(this, arguments);
parent.rightArrow?.apply(this, arguments);
},
home() {
searchIfNeeded();
parent.home && parent.home.apply(this, arguments);
parent.home?.apply(this, arguments);
},
end() {
searchIfNeeded();
parent.end && parent.end.apply(this, arguments);
parent.end?.apply(this, arguments);
},
escape() {
const result = parent.escape && parent.escape.apply(this, arguments);
const result = parent.escape?.apply(this, arguments);
this._cancelEditing();

return result ?? true;
Expand Down Expand Up @@ -126,7 +126,7 @@ const SelectBox = (DropDownList as any).inherit({
return isOpened;
}

if (parent.enter && parent.enter.apply(this, arguments)) {
if (parent.enter?.apply(this, arguments)) {
return isOpened;
}
}
Expand Down Expand Up @@ -269,7 +269,7 @@ const SelectBox = (DropDownList as any).inherit({
},

_scrollToSelectedItem() {
this._list && this._list.scrollToItem(this._list.option('selectedItem'));
this._list?.scrollToItem(this._list.option('selectedItem'));
},

_listContentReadyHandler() {
Expand Down Expand Up @@ -528,16 +528,25 @@ const SelectBox = (DropDownList as any).inherit({
return;
}

this._loadItemDeferred && this._loadItemDeferred.always(() => {
this._loadItemDeferred?.always(() => {
const {
acceptCustomValue,
text,
selectedItem: initialSelectedItem,
customItemCreateEvent,
} = this.option();

if (acceptCustomValue) {
if (!saveEditingValue && !this._isValueChanging) {
this._updateField(initialSelectedItem ?? this._createCustomItem(text));
let initialItem = null;

if (isDefined(initialSelectedItem)) {
initialItem = initialSelectedItem;
} else if (customItemCreateEvent !== '') {
initialItem = this._createCustomItem(text);
}

this._updateField(initialItem);
this._clearFilter();
}
return;
Expand Down Expand Up @@ -658,7 +667,7 @@ const SelectBox = (DropDownList as any).inherit({
_fieldRenderData() {
const $listFocused = this._list && this.option('opened') && $(this._list.option('focusedElement'));

if ($listFocused && $listFocused.length) {
if ($listFocused?.length) {
return this._list._getItemData($listFocused);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2380,6 +2380,24 @@ QUnit.module('editing', moduleSetup, () => {
assert.equal(onCustomItemCreating.callCount, 0, 'action has not been called');
});

QUnit.test('onCustomItemCreating should not be called when customItemCreateEvent is equal to empty string and component loses focus (T1269852)', function(assert) {
const onCustomItemCreating = sinon.stub();

const $selectBox = $('#selectBox').dxSelectBox({
acceptCustomValue: true,
customItemCreateEvent: '',
onCustomItemCreating: onCustomItemCreating,
});

const $input = $selectBox.find(toSelector(TEXTEDITOR_INPUT_CLASS));
const keyboard = keyboardMock($input);

keyboard.type('t');
$input.trigger('focusout');

assert.strictEqual(onCustomItemCreating.callCount, 0, 'action has not been called');
});

QUnit.test('onCustomItemCreating should not be called more then once even when there is value change handler call inside of event handler (T893205)', function(assert) {
let handlerCallCount = 0;

Expand Down

0 comments on commit 5842726

Please sign in to comment.