Skip to content

Commit

Permalink
feat: add scrollToStart and scrollToEnd API (#7144)
Browse files Browse the repository at this point in the history
Co-authored-by: w98389 <[email protected]>
Co-authored-by: Sergey Vinogradov <[email protected]>
  • Loading branch information
3 people committed Feb 28, 2024
1 parent b0cda63 commit 4f6f47e
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 0 deletions.
10 changes: 10 additions & 0 deletions packages/text-area/src/vaadin-text-area-mixin.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,14 @@ export declare class TextAreaMixinClass {
* The pattern must match the entire value, not just some subset.
*/
pattern: string;

/**
* Scrolls the textarea to the start if it has a vertical scrollbar.
*/
scrollToStart(): void;

/**
* Scrolls the textarea to the end if it has a vertical scrollbar.
*/
scrollToEnd(): void;
}
14 changes: 14 additions & 0 deletions packages/text-area/src/vaadin-text-area-mixin.js
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,20 @@ export const TextAreaMixin = (superClass) =>
inputField.scrollTop = scrollTop;
}

/**
* Scrolls the textarea to the start if it has a vertical scrollbar.
*/
scrollToStart() {
this._inputField.scrollTop = 0;
}

/**
* Scrolls the textarea to the end if it has a vertical scrollbar.
*/
scrollToEnd() {
this._inputField.scrollTop = this._inputField.scrollHeight;
}

/**
* Returns true if the current textarea value satisfies all constraints (if any).
* @return {boolean}
Expand Down
22 changes: 22 additions & 0 deletions packages/text-area/test/text-area.common.js
Original file line number Diff line number Diff line change
Expand Up @@ -418,4 +418,26 @@ describe('text-area', () => {
});
});
});

describe('programmatic scrolling', () => {
beforeEach(() => {
textArea.value = Array(400).join('400');
textArea.style.height = '300px';
});

it('should scroll to start', () => {
textArea._inputField.scrollTop = 100; // Simulate scrolling
textArea.scrollToStart();
expect(textArea._inputField.scrollTop).to.equal(0);
});

it('should scroll to end', () => {
textArea.scrollToStart();
expect(textArea._inputField.scrollTop).to.equal(0);
textArea.scrollToEnd();
expect(textArea._inputField.scrollTop).to.equal(
textArea._inputField.scrollHeight - textArea._inputField.clientHeight,
);
});
});
});
3 changes: 3 additions & 0 deletions packages/text-area/test/typings/text-area.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,6 @@ area.addEventListener('validated', (event) => {
assertType<TextAreaValidatedEvent>(event);
assertType<boolean>(event.detail.valid);
});

assertType<() => void>(area.scrollToStart);
assertType<() => void>(area.scrollToEnd);

0 comments on commit 4f6f47e

Please sign in to comment.