Skip to content

Commit

Permalink
Tagbox: Handle list not rendering immediately when setting tag values…
Browse files Browse the repository at this point in the history
… [T1230995] (DevExpress#27341)

Co-authored-by: EugeniyKiyashko <[email protected]>
  • Loading branch information
1 parent 74de9b1 commit bf9a8fb
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 4 deletions.
6 changes: 3 additions & 3 deletions packages/devextreme/js/__internal/ui/m_tag_box.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1281,16 +1281,16 @@ const TagBox = (SelectBox as any).inherit({

const useButtons = this.option('applyValueMode') === 'useButtons';
const valueIndex = this._valueIndex(value);
const values = (useButtons ? this._list.option('selectedItemKeys') : this._getValue()).slice();
const values = (useButtons ? this._list?.option('selectedItemKeys') || [] : this._getValue()).slice();

if (valueIndex >= 0) {
values.splice(valueIndex, 1);
} else {
values.push(value);
}

if (this.option('applyValueMode') === 'useButtons') {
this._list.option('selectedItemKeys', values);
if (useButtons) {
this._list?.option('selectedItemKeys', values);
} else {
this.option('value', values);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1566,6 +1566,77 @@ QUnit.module('the \'onCustomItemCreating\' option', moduleSetup, () => {
assert.equal($listItems.length, 1, 'list item should be selected after enter press');
assert.equal(checkbox.option('value'), true, 'checkbox is checked');
});

[true, false].forEach((deferRendering) => {
QUnit.test(`No errors should be encountored when new item is added, items option is empty, deferRendering is ${deferRendering} [T1230995]`, function(assert) {
try {
const $element = $('#tagBox').dxTagBox({
applyValueMode: 'useButtons',
acceptCustomValue: true,
onCustomItemCreating(args) {
const newValue = args.text;
const { component } = args;
const currentItems = component.option('items');
const isItemInDataSource = currentItems.some((item) => item === newValue);
if(!isItemInDataSource) {
currentItems.unshift(newValue);
component.option('items', currentItems);
}
args.customItem = newValue;
},
deferRendering
});
const $input = $element.find(`.${TEXTEDITOR_INPUT_CLASS}`);

keyboardMock($input, true)
.focus()
.type('test 1')
.press('enter');

const instance = $element.dxTagBox('instance');

assert.deepEqual(instance.option('selectedItems'), []);
assert.deepEqual(instance.option('items'), ['test 1']);
} catch(error) {
assert.ok(false, `Error encountered is: ${error.message}`);
}
});

QUnit.test(`No errors should be encountored when new item is added, deferRendering is ${deferRendering} [T1230995]`, function(assert) {
try {
const $element = $('#tagBox').dxTagBox({
items: ['Item_1', 'Item_2', 'Item_3'],
applyValueMode: 'useButtons',
acceptCustomValue: true,
onCustomItemCreating(args) {
const newValue = args.text;
const { component } = args;
const currentItems = component.option('items');
const isItemInDataSource = currentItems.some((item) => item === newValue);
if(!isItemInDataSource) {
currentItems.unshift(newValue);
component.option('items', currentItems);
}
args.customItem = newValue;
},
deferRendering
});
const $input = $element.find(`.${TEXTEDITOR_INPUT_CLASS}`);

keyboardMock($input, true)
.focus()
.type('test 1')
.press('enter');

const instance = $element.dxTagBox('instance');

assert.deepEqual(instance.option('selectedItems'), []);
assert.deepEqual(instance.option('items'), ['test 1', 'Item_1', 'Item_2', 'Item_3']);
} catch(error) {
assert.ok(false, `Error encountered is: ${error.message}`);
}
});
});
});

QUnit.module('placeholder', () => {
Expand Down Expand Up @@ -5207,7 +5278,6 @@ QUnit.module('the \'fieldTemplate\' option', moduleSetup, () => {
});
});


QUnit.module('the "customItemCreateEvent" option', {
beforeEach: function() {
this.onCustomItemCreatingSpy = sinon.spy();
Expand Down

0 comments on commit bf9a8fb

Please sign in to comment.