Skip to content

Commit db8ad67

Browse files
marker-daomarker dao ®
andauthored
SelectBox: Skip custom item creating if customItemCreateEvent is '' and component loses focus (T1269852)
Co-authored-by: marker dao ® <[email protected]>
1 parent 073a9d8 commit db8ad67

File tree

2 files changed

+37
-10
lines changed

2 files changed

+37
-10
lines changed

packages/devextreme/js/__internal/ui/m_select_box.ts

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ const SelectBox = (DropDownList as any).inherit({
5555
this._resetCaretPosition(true);
5656
}
5757

58-
parent.tab && parent.tab.apply(this, arguments);
58+
parent.tab?.apply(this, arguments);
5959

6060
if (!popupHasFocusableElements) {
6161
this._cancelSearchIfNeed();
@@ -85,18 +85,18 @@ const SelectBox = (DropDownList as any).inherit({
8585
},
8686
rightArrow() {
8787
searchIfNeeded();
88-
parent.rightArrow && parent.rightArrow.apply(this, arguments);
88+
parent.rightArrow?.apply(this, arguments);
8989
},
9090
home() {
9191
searchIfNeeded();
92-
parent.home && parent.home.apply(this, arguments);
92+
parent.home?.apply(this, arguments);
9393
},
9494
end() {
9595
searchIfNeeded();
96-
parent.end && parent.end.apply(this, arguments);
96+
parent.end?.apply(this, arguments);
9797
},
9898
escape() {
99-
const result = parent.escape && parent.escape.apply(this, arguments);
99+
const result = parent.escape?.apply(this, arguments);
100100
this._cancelEditing();
101101

102102
return result ?? true;
@@ -126,7 +126,7 @@ const SelectBox = (DropDownList as any).inherit({
126126
return isOpened;
127127
}
128128

129-
if (parent.enter && parent.enter.apply(this, arguments)) {
129+
if (parent.enter?.apply(this, arguments)) {
130130
return isOpened;
131131
}
132132
}
@@ -269,7 +269,7 @@ const SelectBox = (DropDownList as any).inherit({
269269
},
270270

271271
_scrollToSelectedItem() {
272-
this._list && this._list.scrollToItem(this._list.option('selectedItem'));
272+
this._list?.scrollToItem(this._list.option('selectedItem'));
273273
},
274274

275275
_listContentReadyHandler() {
@@ -534,16 +534,25 @@ const SelectBox = (DropDownList as any).inherit({
534534
return;
535535
}
536536

537-
this._loadItemDeferred && this._loadItemDeferred.always(() => {
537+
this._loadItemDeferred?.always(() => {
538538
const {
539539
acceptCustomValue,
540540
text,
541541
selectedItem: initialSelectedItem,
542+
customItemCreateEvent,
542543
} = this.option();
543544

544545
if (acceptCustomValue) {
545546
if (!saveEditingValue && !this._isValueChanging) {
546-
this._updateField(initialSelectedItem ?? this._createCustomItem(text));
547+
let initialItem = null;
548+
549+
if (isDefined(initialSelectedItem)) {
550+
initialItem = initialSelectedItem;
551+
} else if (customItemCreateEvent !== '') {
552+
initialItem = this._createCustomItem(text);
553+
}
554+
555+
this._updateField(initialItem);
547556
this._clearFilter();
548557
}
549558
return;
@@ -664,7 +673,7 @@ const SelectBox = (DropDownList as any).inherit({
664673
_fieldRenderData() {
665674
const $listFocused = this._list && this.option('opened') && $(this._list.option('focusedElement'));
666675

667-
if ($listFocused && $listFocused.length) {
676+
if ($listFocused?.length) {
668677
return this._list._getItemData($listFocused);
669678
}
670679

packages/devextreme/testing/tests/DevExpress.ui.widgets.editors/selectBox.tests.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2380,6 +2380,24 @@ QUnit.module('editing', moduleSetup, () => {
23802380
assert.equal(onCustomItemCreating.callCount, 0, 'action has not been called');
23812381
});
23822382

2383+
QUnit.test('onCustomItemCreating should not be called when customItemCreateEvent is equal to empty string and component loses focus (T1269852)', function(assert) {
2384+
const onCustomItemCreating = sinon.stub();
2385+
2386+
const $selectBox = $('#selectBox').dxSelectBox({
2387+
acceptCustomValue: true,
2388+
customItemCreateEvent: '',
2389+
onCustomItemCreating: onCustomItemCreating,
2390+
});
2391+
2392+
const $input = $selectBox.find(toSelector(TEXTEDITOR_INPUT_CLASS));
2393+
const keyboard = keyboardMock($input);
2394+
2395+
keyboard.type('t');
2396+
$input.trigger('focusout');
2397+
2398+
assert.strictEqual(onCustomItemCreating.callCount, 0, 'action has not been called');
2399+
});
2400+
23832401
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) {
23842402
let handlerCallCount = 0;
23852403

0 commit comments

Comments
 (0)