Skip to content

Commit b5480bc

Browse files
committed
Form: fix get item by path (T1311534)
1 parent cee58b9 commit b5480bc

File tree

3 files changed

+61
-28
lines changed

3 files changed

+61
-28
lines changed

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

Lines changed: 11 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1448,11 +1448,11 @@ class Form extends Widget<FormProperties> {
14481448
_getItemByField(field: string | {
14491449
fieldName: string;
14501450
fieldPath: string[];
1451-
}, items: PreparedItem[]): PreparedItem | false {
1451+
}, items: PreparedItem[]): PreparedItem | null {
14521452
const fieldParts = isObject(field) ? field : this._getFieldParts(field);
14531453
const { fieldName } = fieldParts;
14541454
const { fieldPath } = fieldParts;
1455-
let resultItem: PreparedItem | false = false;
1455+
let resultItem: PreparedItem | null = null;
14561456

14571457
if (items.length) {
14581458
each(items, (_index: number, item: PreparedItem): boolean => {
@@ -1491,34 +1491,24 @@ class Form extends Widget<FormProperties> {
14911491
fieldName: string;
14921492
fieldPath: string[];
14931493
} {
1494-
const fieldSeparator = '.';
1495-
let fieldName = field;
1496-
let separatorIndex = fieldName.indexOf(fieldSeparator);
1497-
const resultPath = [];
1498-
1499-
while (separatorIndex !== -1) {
1500-
// @ts-expect-error ts-error
1501-
resultPath.push(fieldName.substr(0, separatorIndex));
1502-
fieldName = fieldName.substr(separatorIndex + 1);
1503-
separatorIndex = fieldName.indexOf(fieldSeparator);
1504-
}
1494+
const [fieldName, ...fieldPath] = field.split('.').reverse();
15051495

15061496
return {
15071497
fieldName,
1508-
fieldPath: resultPath.reverse(),
1498+
fieldPath,
15091499
};
15101500
}
15111501

15121502
_getItemByFieldPath(
15131503
path: string[],
15141504
fieldName: string,
15151505
item: Item,
1516-
): Item | false {
1506+
): Item | null {
15171507
const { itemType } = item;
15181508
const subItemsField = this._getSubItemField(itemType);
15191509

15201510
const isItemWithSubItems = itemType === 'group' || itemType === 'tabbed' || (item as TabItem).title;
1521-
let result: Item | false = false;
1511+
let result: Item | null = null;
15221512

15231513
do {
15241514
if (isItemWithSubItems) {
@@ -1534,7 +1524,7 @@ class Form extends Widget<FormProperties> {
15341524
pathNode = path.pop();
15351525
}
15361526

1537-
if (!path.length) {
1527+
if (!path.length && nameWithoutSpaces === pathNode) {
15381528
result = this._getItemByField(fieldName, item[subItemsField]);
15391529

15401530
// eslint-disable-next-line max-depth
@@ -1565,19 +1555,13 @@ class Form extends Widget<FormProperties> {
15651555
path: string[],
15661556
fieldName: string,
15671557
items: Item[],
1568-
): Item | false {
1569-
let result: Item | false = false;
1558+
): Item | null {
1559+
let result: Item | null = null;
15701560
each(items, (_index: number, groupItem: GroupItem): boolean => {
15711561
result = this._getItemByFieldPath(path.slice(), fieldName, groupItem);
1572-
if (result) {
1573-
return false;
1574-
}
1575-
return true;
1576-
});
15771562

1578-
if (!result) {
1579-
return false;
1580-
}
1563+
return !result;
1564+
});
15811565

15821566
return result;
15831567
}

packages/devextreme/js/__internal/ui/form/form.utils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ export const tryGetTabPath = (fullPath: string): string => {
6262

6363
export const getItemPath = (
6464
items: PreparedItem[],
65-
item: PreparedItem | false,
65+
item: PreparedItem | null,
6666
isTabs?: boolean,
6767
): string => {
6868
if (!item) {

packages/devextreme/testing/tests/DevExpress.ui.widgets.form/form.tests.js

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2580,6 +2580,55 @@ QUnit.test('Use \'itemOption\' with groups and one group has empty caption (T359
25802580
assert.equal($testContainer.find('.' + FORM_GROUP_CAPTION_CLASS).last().text(), 'custom', 'new caption rendered');
25812581
});
25822582

2583+
QUnit.test('Use \'itemOption\' with path when items have same name or caption (T1311534)', function(assert) {
2584+
const targetField = {
2585+
itemType: 'simple',
2586+
name: 'Target',
2587+
};
2588+
2589+
const targetGroup = {
2590+
itemType: 'group',
2591+
caption: 'Target',
2592+
items: [
2593+
{ itemType: 'simple', name: 'Field1' },
2594+
{ itemType: 'simple', name: 'Field2' },
2595+
{ itemType: 'simple', name: 'Field3' },
2596+
],
2597+
};
2598+
2599+
const targetFieldInGroup = {
2600+
...targetField,
2601+
caption: 'Target in Group'
2602+
};
2603+
2604+
const form = $('#form').dxForm({
2605+
formData: {},
2606+
items: [
2607+
{
2608+
itemType: 'group',
2609+
name: 'ContainerGroup',
2610+
items: [
2611+
{ itemType: 'simple', name: 'Field4' },
2612+
targetGroup,
2613+
]
2614+
},
2615+
{
2616+
itemType: 'group',
2617+
name: 'FieldGroup',
2618+
items: [
2619+
targetFieldInGroup,
2620+
{ itemType: 'simple', name: 'Field5' },
2621+
],
2622+
},
2623+
targetField,
2624+
] }
2625+
).dxForm('instance');
2626+
2627+
assert.deepEqual(form.itemOption('Target'), targetField, 'Simple item retrieved by name');
2628+
assert.deepEqual(form.itemOption('ContainerGroup.Target'), targetGroup, 'Group item in group retrieved by path');
2629+
assert.deepEqual(form.itemOption('FieldGroup.Target'), targetFieldInGroup, 'Simple item in group retrieved by path');
2630+
});
2631+
25832632
QUnit.test('Use \'itemOption\' with tabs', function(assert) {
25842633
const $testContainer = $('#form').dxForm({
25852634
formData: { EmployeeID: 1, LastName: 'John', FirstName: 'Dow', BirthData: '01/01/1970', HireDate: '12/11/1995', Country: 'USA', City: 'Phoenix', Region: 'Arizona', Title: 'Ms' },

0 commit comments

Comments
 (0)