Skip to content

Commit 77f7216

Browse files
authored
fix(autocomplete): position autocomplete correctly (#87)
Fixed position of the autocomplete when the textarea is scrolled CLOSE #86
1 parent 2db757f commit 77f7216

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

__tests__/__snapshots__/index.spec.js.snap

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ exports[`object-based items match the snapshot 1`] = `
1414
onBlur={[Function]}
1515
onChange={[Function]}
1616
onClick={[Function]}
17+
onScroll={[Function]}
1718
onSelect={[Function]}
1819
placeholder="Write a message."
1920
style={
@@ -29,6 +30,7 @@ exports[`object-based items match the snapshot 1`] = `
2930
exports[`object-based items match the snapshot of dropdown, list, and item 1`] = `
3031
<div
3132
class="rta__autocomplete "
33+
style="top: 0px;"
3234
>
3335
<ul
3436
class="rta__list my-rta-list"
@@ -171,6 +173,7 @@ exports[`object-based items with keys match the snapshot 1`] = `
171173
onBlur={[Function]}
172174
onChange={[Function]}
173175
onClick={[Function]}
176+
onScroll={[Function]}
174177
onSelect={[Function]}
175178
placeholder="Write a message."
176179
style={
@@ -186,6 +189,7 @@ exports[`object-based items with keys match the snapshot 1`] = `
186189
exports[`object-based items with keys match the snapshot of dropdown, list, and item 1`] = `
187190
<div
188191
class="rta__autocomplete "
192+
style="top: 0px;"
189193
>
190194
<ul
191195
class="rta__list my-rta-list"
@@ -367,6 +371,7 @@ exports[`object-based items without keys and custom unique generator match the s
367371
onBlur={[Function]}
368372
onChange={[Function]}
369373
onClick={[Function]}
374+
onScroll={[Function]}
370375
onSelect={[Function]}
371376
placeholder="Write a message."
372377
value="Controlled text"
@@ -383,6 +388,7 @@ exports[`string-based items w/o output fn match match the snapshot 1`] = `
383388
onBlur={[Function]}
384389
onChange={[Function]}
385390
onClick={[Function]}
391+
onScroll={[Function]}
386392
onSelect={[Function]}
387393
placeholder="Write a message."
388394
style={
@@ -404,6 +410,7 @@ exports[`string-based items with output fn match match the snapshot 1`] = `
404410
onBlur={[Function]}
405411
onChange={[Function]}
406412
onClick={[Function]}
413+
onScroll={[Function]}
407414
onSelect={[Function]}
408415
placeholder="Write a message."
409416
style={

src/Textarea.jsx

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -508,7 +508,16 @@ class ReactTextareaAutocomplete extends React.Component<
508508
// if we have single char - trigger it means we want to re-position the autocomplete
509509
lastToken.length === 1
510510
) {
511-
this.setState(getCaretCoordinates(textarea, selectionEnd));
511+
const { top: newTop, left: newLeft } = getCaretCoordinates(
512+
textarea,
513+
selectionEnd
514+
);
515+
516+
this.setState({
517+
// make position relative to textarea
518+
top: newTop - this.textareaRef.scrollTop || 0,
519+
left: newLeft,
520+
});
512521
}
513522

514523
this.setState(
@@ -567,6 +576,10 @@ class ReactTextareaAutocomplete extends React.Component<
567576
}
568577
};
569578

579+
_onScrollHandler = () => {
580+
this._closeAutocomplete();
581+
};
582+
570583
_dropdownScroll = (item: HTMLDivElement) => {
571584
const { scrollToItem } = this.props;
572585

@@ -638,6 +651,7 @@ class ReactTextareaAutocomplete extends React.Component<
638651
className={`rta__textarea ${className || ''}`}
639652
onChange={this._changeHandler}
640653
onSelect={this._selectHandler}
654+
onScroll={this._onScrollHandler}
641655
onClick={
642656
// The textarea itself is outside the autoselect dropdown.
643657
this._onClickAndBlurHandler

0 commit comments

Comments
 (0)