Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix choose file doesn't work when survey in shadow dom #8623

Merged
merged 3 commits into from
Jul 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions src/question_file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -479,9 +479,11 @@ export class QuestionFileModel extends QuestionFileModelBase {
this.setPropertyValue("maxSize", val);
}
public chooseFile(event: MouseEvent): void {
if (!DomDocumentHelper.isAvailable()) return;
if (!this.rootElement) return;

const inputElement = this.rootElement.querySelector(`#${this.inputId}`) as HTMLInputElement;
if(!inputElement) return;

const inputElement = DomDocumentHelper.getDocument().getElementById(this.inputId) as HTMLInputElement;
event.preventDefault();
event.stopImmediatePropagation();
if (inputElement) {
Expand Down Expand Up @@ -992,9 +994,11 @@ export class QuestionFileModel extends QuestionFileModelBase {
// web-based methods
private rootElement: HTMLElement;
private canDragDrop(): boolean { return !this.isInputReadOnly && this.currentMode !== "camera" && !this.isPlayingVideo; }
afterRender(el: HTMLElement): void {
public afterRenderQuestionElement(el: HTMLElement): void {
this.rootElement = el;
super.afterRender(el);
}
public beforeDestroyQuestionElement(el: HTMLElement): void {
this.rootElement = undefined;
}
private dragCounter: number = 0;
onDragEnter = (event: any) => {
Expand Down
24 changes: 15 additions & 9 deletions src/react/reactquestion_element.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,7 @@ export class ReactSurveyElement extends SurveyElementBase<any, any> {

export class SurveyQuestionElementBase extends SurveyElementBase<any, any> {
control: HTMLElement;
content: HTMLElement;
constructor(props: any) {
super(props);
}
Expand All @@ -208,19 +209,19 @@ export class SurveyQuestionElementBase extends SurveyElementBase<any, any> {
componentWillUnmount() {
super.componentWillUnmount();
if (!!this.questionBase) {
const el: HTMLElement = this.control;
this.questionBase.beforeDestroyQuestionElement(el);
if (!!el) {
el.removeAttribute("data-rendered");
const contentElement: HTMLElement = this.content || this.control;
this.questionBase.beforeDestroyQuestionElement(contentElement);
if (!!contentElement) {
contentElement.removeAttribute("data-rendered");
}
}
}
protected updateDomElement() {
const el: HTMLElement = this.control;
if (!!el) {
if (el.getAttribute("data-rendered") !== "r") {
el.setAttribute("data-rendered", "r");
this.questionBase.afterRenderQuestionElement(el);
const contentElement = this.content || this.control;
if (!!contentElement) {
if (contentElement.getAttribute("data-rendered") !== "r") {
contentElement.setAttribute("data-rendered", "r");
this.questionBase.afterRenderQuestionElement(contentElement);
}
}
}
Expand Down Expand Up @@ -274,6 +275,11 @@ export class SurveyQuestionElementBase extends SurveyElementBase<any, any> {
this.control = element;
}
}
public setContent(element: HTMLElement | null): void {
if(!!element) {
this.content = element;
}
}
}

export class SurveyQuestionUncontrolledElement<
Expand Down
2 changes: 1 addition & 1 deletion src/react/reactquestion_file.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ export class SurveyQuestionFile extends SurveyQuestionElementBase {
}

return (
<div className={this.question.fileRootCss}>
<div className={this.question.fileRootCss} ref={el => (this.setContent(el))}>
{fileInput}
<div
className={this.question.cssClasses.dragArea}
Expand Down
10 changes: 10 additions & 0 deletions testCafe/questions/file.js
Original file line number Diff line number Diff line change
Expand Up @@ -259,4 +259,14 @@ frameworks.forEach(framework => {
.resizeWindow(1920, 1080)
.expect(fileNavigatorSelector.exists).notOk();
});
test("check file question has correct root element", async t => {
const testRootElement = ClientFunction(() => {
const rootElement = survey.getAllQuestions()[0].rootElement;
return rootElement && rootElement.tagName == "DIV" && rootElement.classList.contains("sd-file");
});
await t
.resizeWindow(1920, 1080)
.expect(testRootElement())
.ok();
});
});