Skip to content

Commit 4f6f47e

Browse files
AbdullahGheithw98389vursen
authored
feat: add scrollToStart and scrollToEnd API (#7144)
Co-authored-by: w98389 <agh@spillemyndigheden.dk> Co-authored-by: Sergey Vinogradov <mr.vursen@gmail.com>
1 parent b0cda63 commit 4f6f47e

File tree

4 files changed

+49
-0
lines changed

4 files changed

+49
-0
lines changed

packages/text-area/src/vaadin-text-area-mixin.d.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,4 +61,14 @@ export declare class TextAreaMixinClass {
6161
* The pattern must match the entire value, not just some subset.
6262
*/
6363
pattern: string;
64+
65+
/**
66+
* Scrolls the textarea to the start if it has a vertical scrollbar.
67+
*/
68+
scrollToStart(): void;
69+
70+
/**
71+
* Scrolls the textarea to the end if it has a vertical scrollbar.
72+
*/
73+
scrollToEnd(): void;
6474
}

packages/text-area/src/vaadin-text-area-mixin.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,20 @@ export const TextAreaMixin = (superClass) =>
178178
inputField.scrollTop = scrollTop;
179179
}
180180

181+
/**
182+
* Scrolls the textarea to the start if it has a vertical scrollbar.
183+
*/
184+
scrollToStart() {
185+
this._inputField.scrollTop = 0;
186+
}
187+
188+
/**
189+
* Scrolls the textarea to the end if it has a vertical scrollbar.
190+
*/
191+
scrollToEnd() {
192+
this._inputField.scrollTop = this._inputField.scrollHeight;
193+
}
194+
181195
/**
182196
* Returns true if the current textarea value satisfies all constraints (if any).
183197
* @return {boolean}

packages/text-area/test/text-area.common.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -418,4 +418,26 @@ describe('text-area', () => {
418418
});
419419
});
420420
});
421+
422+
describe('programmatic scrolling', () => {
423+
beforeEach(() => {
424+
textArea.value = Array(400).join('400');
425+
textArea.style.height = '300px';
426+
});
427+
428+
it('should scroll to start', () => {
429+
textArea._inputField.scrollTop = 100; // Simulate scrolling
430+
textArea.scrollToStart();
431+
expect(textArea._inputField.scrollTop).to.equal(0);
432+
});
433+
434+
it('should scroll to end', () => {
435+
textArea.scrollToStart();
436+
expect(textArea._inputField.scrollTop).to.equal(0);
437+
textArea.scrollToEnd();
438+
expect(textArea._inputField.scrollTop).to.equal(
439+
textArea._inputField.scrollHeight - textArea._inputField.clientHeight,
440+
);
441+
});
442+
});
421443
});

packages/text-area/test/typings/text-area.types.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,3 +45,6 @@ area.addEventListener('validated', (event) => {
4545
assertType<TextAreaValidatedEvent>(event);
4646
assertType<boolean>(event.detail.valid);
4747
});
48+
49+
assertType<() => void>(area.scrollToStart);
50+
assertType<() => void>(area.scrollToEnd);

0 commit comments

Comments
 (0)