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

Allow always scroll into view on focus #6787

Merged
merged 2 commits into from
Aug 25, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 1 addition & 1 deletion src/base-interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ export interface ISurvey extends ITextProcessor, ISurveyErrorOwner {
element: ISurveyElement,
question: IQuestion,
page: IPage,
id: string
id: string, scrollIfVisible?: boolean
): any;
runExpression(expression: string): any;
elementContentVisibilityChanged(element: ISurveyElement): void;
Expand Down
8 changes: 4 additions & 4 deletions src/question.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1012,20 +1012,20 @@ export class Question extends SurveyElement<Question>
* Moves focus to the input field of this question.
* @param onError Pass `true` if you want to focus an input field with the first validation error. Default value: `false` (focuses the first input field). Applies to question types with multiple input fields.
*/
public focus(onError: boolean = false): void {
public focus(onError: boolean = false, scrollIfVisible?: boolean): void {
if (this.isDesignMode || !this.isVisible || !this.survey) return;
let page = this.page;
const shouldChangePage = !!page && this.survey.activePage !== page;
if(shouldChangePage) {
this.survey.focusQuestionByInstance(this, onError);
} else {
this.focuscore(onError);
this.focuscore(onError, scrollIfVisible);
}
}
private focuscore(onError: boolean = false): void {
private focuscore(onError: boolean = false, scrollIfVisible?: boolean): void {
if (!!this.survey) {
this.expandAllParents(this);
this.survey.scrollElementToTop(this, this, null, this.id);
this.survey.scrollElementToTop(this, this, null, this.id, scrollIfVisible);
}
var id = !onError
? this.getFirstInputElementId()
Expand Down
4 changes: 2 additions & 2 deletions src/survey-element.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,12 +161,12 @@ export class SurveyElement<E = any> extends SurveyElementCore implements ISurvey

public readOnlyChangedCallback: () => void;

public static ScrollElementToTop(elementId: string): boolean {
public static ScrollElementToTop(elementId: string, scrollIfVisible?: boolean): boolean {
const { root } = settings.environment;
if (!elementId || typeof root === "undefined") return false;
const el = root.getElementById(elementId);
if (!el || !el.scrollIntoView) return false;
const elemTop: number = el.getBoundingClientRect().top;
const elemTop: number = scrollIfVisible ? -1 : el.getBoundingClientRect().top;
if (elemTop < 0) el.scrollIntoView();
return elemTop < 0;
}
Expand Down
4 changes: 2 additions & 2 deletions src/survey.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4855,7 +4855,7 @@ export class SurveyModel extends SurveyElementCore
element: ISurveyElement,
question: Question,
page: PageModel,
id: string
id: string, scrollIfVisible?: boolean
): any {
const options: ScrollingElementToTopEvent = {
element: element,
Expand All @@ -4866,7 +4866,7 @@ export class SurveyModel extends SurveyElementCore
};
this.onScrollingElementToTop.fire(this, options);
if (!options.cancel) {
SurveyElement.ScrollElementToTop(options.elementId);
SurveyElement.ScrollElementToTop(options.elementId, scrollIfVisible);
}
}

Expand Down