Skip to content

Commit bb30bd1

Browse files
authored
Collection: make user options separate for each instance (#31632)
1 parent 4fbe77f commit bb30bd1

File tree

2 files changed

+45
-3
lines changed

2 files changed

+45
-3
lines changed

packages/devextreme/js/__internal/ui/collection/collection_widget.edit.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,9 @@ class CollectionWidget<
7474
// eslint-disable-next-line @typescript-eslint/no-explicit-any
7575
TKey extends CollectionItemKey = any,
7676
> extends BaseCollectionWidget<TProperties, TItem, TKey> {
77-
static _userOptions = {};
77+
static _initUserOptions?: Record<string, unknown> = {};
78+
79+
_userOptions?: Record<string, unknown>;
7880

7981
// @ts-expect-error TItem
8082
_selection!: Selection<TItem, TKey, false>;
@@ -92,7 +94,7 @@ class CollectionWidget<
9294
_keyGetter!: (item: TItem) => TKey;
9395

9496
constructor(element: Element, options?: Record<string, unknown>) {
95-
CollectionWidget._userOptions = options ?? {};
97+
CollectionWidget._initUserOptions = options ?? {};
9698
// @ts-expect-error
9799
super(element, options);
98100
}
@@ -125,6 +127,9 @@ class CollectionWidget<
125127
}
126128

127129
_init(): void {
130+
this._userOptions = { ...CollectionWidget._initUserOptions };
131+
CollectionWidget._initUserOptions = undefined;
132+
128133
this._initEditStrategy();
129134

130135
super._init();
@@ -429,7 +434,7 @@ class CollectionWidget<
429434
const { [name]: optionValue } = this.option();
430435
const length = isDefined(optionValue) && Array.isArray(optionValue) && optionValue.length;
431436

432-
return !!length || name in CollectionWidget._userOptions;
437+
return !!length || name in (this._userOptions ?? {});
433438
};
434439

435440
if (isOptionDefined('selectedItems')) {

packages/devextreme/testing/tests/DevExpress.ui/collectionWidgetParts/editingTests.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2823,3 +2823,40 @@ module('internal methods', () => {
28232823
assert.strictEqual(getCallSpy.callCount, 0);
28242824
});
28252825
});
2826+
2827+
module('construction and initialization', {
2828+
beforeEach: function() {
2829+
this.element = document.createElement('div');
2830+
},
2831+
afterEach: function() {
2832+
TestComponent._initUserOptions = undefined;
2833+
}
2834+
});
2835+
2836+
test('passed options should be properly written in the non-static class field', function(assert) {
2837+
const testOptions = {
2838+
items: [1, 2, 3],
2839+
selectedIndex: 0,
2840+
customProp: 'test'
2841+
};
2842+
2843+
const widget = new TestComponent(this.element, testOptions);
2844+
2845+
assert.deepEqual(
2846+
widget._userOptions,
2847+
testOptions,
2848+
'_userOptions should contain passed options'
2849+
);
2850+
});
2851+
2852+
test('_initUserOptions static field should be cleared after init', function(assert) {
2853+
const testOptions = { items: [1, 2, 3] };
2854+
2855+
new TestComponent(this.element, testOptions);
2856+
2857+
assert.strictEqual(
2858+
TestComponent._initUserOptions,
2859+
undefined,
2860+
'static _initUserOptions should be cleared after widget initialization'
2861+
);
2862+
});

0 commit comments

Comments
 (0)