Skip to content

Commit b6c322e

Browse files
authored
Focus unexpectedly shifts to the question’s Name property editor when navigating backward fix #7547 (#7563)
* Focus unexpectedly shifts to the question’s Name property editor when navigating backward fix #7547 * Enhance property grid behavior to maintain panel focus during question changes * Fix e2e test
1 parent 0fb8de9 commit b6c322e

File tree

5 files changed

+119
-10
lines changed

5 files changed

+119
-10
lines changed

e2e/property-grid/panels.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ test.describe(title, () => {
88
await page.setViewportSize({ width: 1920, height: 1080 });
99
});
1010

11-
test("Panel stay focused on question change", async ({ page }) => {
11+
test("Panel stay visible on question change", async ({ page }) => {
1212
const json = {
1313
"pages": [
1414
{
@@ -44,7 +44,7 @@ test.describe(title, () => {
4444
await expect(startWithNewLine).toBeVisible();
4545
await page.locator("div [data-name=\"minWidth\"] input").click();
4646
await question1.click();
47-
await expect(page.locator("div [data-name=\"minWidth\"] input")).toBeFocused();
47+
await expect(page.locator("div [data-name=\"minWidth\"] input")).toBeVisible();
4848
});
4949

5050
test("Show/hide panel header on entering/deleting the panel title, Bug#5720", async ({ page }) => {

packages/survey-creator-core/src/creator-base.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3661,7 +3661,7 @@ export class SurveyCreatorModel extends Base
36613661
}
36623662
if (!!propertyName) {
36633663
this.sidebar.executeOnExpand(() => {
3664-
this.designerPropertyGrid.selectProperty(propertyName, focus || !this.selectFromStringEditor);
3664+
this.designerPropertyGrid.selectProperty(propertyName, focus && !this.selectFromStringEditor);
36653665
});
36663666
}
36673667
this.expandCategoryIfNeeded();

packages/survey-creator-core/src/property-grid/index.ts

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -853,17 +853,29 @@ export class PropertyGridModel {
853853
this.updateCurrentSurveyWithNewDefinition();
854854
}
855855
}
856+
private expandPanel(panel: PanelModel): void {
857+
panel.blockAnimations();
858+
panel.expand();
859+
panel.releaseAnimations();
860+
}
856861
public selectProperty(propertyName: string, focus = true) {
857862
if (!this.survey) return;
858863
var question = this.survey.getQuestionByName(propertyName);
859-
if (!question) return;
864+
if (!question) {
865+
if (this.currentlySelectedPanel) {
866+
const panelName = this.currentlySelectedPanel.name;
867+
const panel = <PanelModel>this.survey.getPanelByName(panelName);
868+
if (panel) {
869+
this.expandPanel(panel);
870+
}
871+
}
872+
return;
873+
}
860874
var panels = this.survey.getAllPanels();
861875
for (var i = 0; i < panels.length; i++) {
862876
var panel = <PanelModel>panels[i];
863877
if (panel === question.parent) {
864-
panel.blockAnimations();
865-
panel.expand();
866-
panel.releaseAnimations();
878+
this.expandPanel(panel);
867879
} else {
868880
panel.collapse();
869881
}
@@ -1153,9 +1165,7 @@ export class PropertyGridModel {
11531165
if (!!expandedTabName && !this.getPropertyGridExpandedCategory()) {
11541166
const panel = <PanelModel>this.survey.getPanelByName(expandedTabName);
11551167
if (!!panel) {
1156-
panel.blockAnimations();
1157-
panel.expand();
1158-
panel.releaseAnimations();
1168+
this.expandPanel(panel);
11591169
}
11601170
}
11611171
}

packages/survey-creator-core/tests/creator-base-v1.tests.ts

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1251,6 +1251,48 @@ test("getDisplayText https://surveyjs.answerdesk.io/ticket/details/T1380", () =>
12511251
const model = new PageNavigatorViewModel(new PagesController(creator), "");
12521252
expect(model.items[0].title).toEqual("Page 1");
12531253
});
1254+
test("selectFromStringEditor should prevent focus on property editor", () => {
1255+
const creator = new CreatorTester();
1256+
creator.JSON = { elements: [{ type: "text", name: "q1" }, { type: "text", name: "q2" }] };
1257+
const q1 = creator.survey.getQuestionByName("q1");
1258+
const q2 = creator.survey.getQuestionByName("q2");
1259+
creator.selectElement(q1);
1260+
const propertyGrid = creator["designerPropertyGrid"];
1261+
const log: { propertyName: string, focus: boolean }[] = [];
1262+
const originalSelectProperty = propertyGrid.selectProperty.bind(propertyGrid);
1263+
propertyGrid.selectProperty = (propertyName: string, focus: boolean) => {
1264+
log.push({ propertyName, focus });
1265+
originalSelectProperty(propertyName, focus);
1266+
};
1267+
1268+
// Select q2 with propertyName "title" and selectFromStringEditor = false => should focus
1269+
creator.selectFromStringEditor = false;
1270+
creator.selectElement(q2, "title");
1271+
expect(log.length).toBe(1);
1272+
expect(log[0].propertyName).toBe("title");
1273+
expect(log[0].focus).toBe(true);
1274+
log.length = 0;
1275+
1276+
// Select q1 with propertyName "title" and selectFromStringEditor = true => should NOT focus
1277+
creator.selectFromStringEditor = true;
1278+
creator.selectElement(q1, "title");
1279+
expect(log.length).toBe(1);
1280+
expect(log[0].propertyName).toBe("title");
1281+
expect(log[0].focus).toBe(false);
1282+
log.length = 0;
1283+
1284+
// Verify selectFromStringEditor is reset to false after selectionChanged
1285+
expect(creator.selectFromStringEditor).toBe(false);
1286+
1287+
// Select q2 with propertyName "title" and selectFromStringEditor = true, focus = false => should NOT focus
1288+
creator.selectFromStringEditor = true;
1289+
creator.selectElement(q2, "title", false);
1290+
expect(log.length).toBe(1);
1291+
expect(log[0].propertyName).toBe("title");
1292+
expect(log[0].focus).toBe(false);
1293+
1294+
propertyGrid.selectProperty = originalSelectProperty;
1295+
});
12541296
test(
12551297
"creator getMenuItems should respect property visibility (e.g. for image question) - https://github.com/surveyjs/survey-creator/issues/897",
12561298
() => {

packages/survey-creator-core/tests/property-grid/property-grid-viewmodel.tests.ts

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,63 @@ test("Create the property grid survey on request", () => {
195195
expect(model.survey).toBeTruthy();
196196
});
197197

198+
test("Panel stay focused on question change - Bug#7547", () => {
199+
const creator = new CreatorTester();
200+
creator.propertyGridNavigationMode = "accordion";
201+
creator.JSON = {
202+
pages: [
203+
{
204+
name: "page1",
205+
elements: [
206+
{
207+
type: "checkbox",
208+
name: "question1",
209+
title: "question1",
210+
choices: ["Item 1", "Item 2", "Item 3"]
211+
},
212+
{
213+
type: "checkbox",
214+
name: "question2",
215+
title: "question2",
216+
choices: ["Item 1", "Item 2", "Item 3"]
217+
}
218+
]
219+
}
220+
]
221+
};
222+
223+
const question1 = creator.survey.getQuestionByName("question1");
224+
const question2 = creator.survey.getQuestionByName("question2");
225+
const propertyGrid = creator["designerPropertyGrid"];
226+
227+
// Select question1 and open the Layout category
228+
creator.selectElement(question1);
229+
propertyGrid.selectProperty("startWithNewLine", true);
230+
const layoutPanel = <PanelModel>propertyGrid.survey.getPanelByName("layout");
231+
expect(layoutPanel.isExpanded).toBeTruthy();
232+
233+
// Select question2 - Layout should stay expanded
234+
propertyGrid.currentlySelectedProperty = "startWithNewLine";
235+
creator.selectElement(question2);
236+
const layoutPanel2 = <PanelModel>propertyGrid.survey.getPanelByName("layout");
237+
expect(layoutPanel2.isExpanded).toBeTruthy();
238+
239+
// Focus on width property in the Layout panel (simulates clicking an input in the Layout category)
240+
// In the real app, focusing a sub-property like "minWidth" sets currentlySelectedProperty to "minWidth"
241+
// which doesn't exist as a top-level question in the property grid.
242+
// onFocusInQuestion also sets currentlySelectedPanel to the Layout panel.
243+
// The property grid should still preserve the expanded category (Layout) when switching elements.
244+
propertyGrid.currentlySelectedProperty = "minWidth";
245+
propertyGrid.currentlySelectedPanel = <PanelModel>propertyGrid.survey.getPanelByName("layout");
246+
247+
// Switch back to question1 - Layout should remain expanded
248+
creator.selectElement(question1);
249+
const layoutPanel1 = <PanelModel>propertyGrid.survey.getPanelByName("layout");
250+
const generalPanel1 = <PanelModel>propertyGrid.survey.getPanelByName("general");
251+
expect(layoutPanel1.isExpanded).toBeTruthy();
252+
expect(generalPanel1.isExpanded).toBeFalsy();
253+
});
254+
198255
test("Object selector's title should be leaved unchanged when changing question's name - Bug#6699", () => {
199256
const creator = new CreatorTester();
200257
creator.JSON = {

0 commit comments

Comments
 (0)