Skip to content
Open
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
10 changes: 9 additions & 1 deletion src/DropdownMenu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ function DropdownMenu(props: DropdownMenuProps) {
onFocus,
onBlur,
onScroll,
textareaRef,
} = React.useContext(MentionsContext);

const { prefixCls, options, opened } = props;
Expand All @@ -34,6 +35,13 @@ function DropdownMenu(props: DropdownMenuProps) {
return;
}

if (
textareaRef?.current &&
document.activeElement !== textareaRef.current.nativeElement
) {
return;
}

const activeItem = menuRef.current?.findItem?.({ key: activeOption.key });

if (activeItem) {
Expand All @@ -42,7 +50,7 @@ function DropdownMenu(props: DropdownMenuProps) {
inline: 'nearest',
});
}
}, [activeIndex, activeOption.key, opened]);
}, [activeIndex, activeOption.key, opened, textareaRef]);

return (
<Menu
Expand Down
1 change: 1 addition & 0 deletions src/Mentions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -529,6 +529,7 @@ const InternalMentions = forwardRef<MentionsRef, InternalMentionsProps>(
onFocus: onDropdownFocus,
onBlur: onDropdownBlur,
onScroll: onInternalPopupScroll,
textareaRef,
}}
>
<KeywordTrigger
Expand Down
2 changes: 2 additions & 0 deletions src/MentionsContext.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/* tslint:disable: no-object-literal-type-assertion */
import * as React from 'react';
import type { OptionProps } from './Option';
import { TextAreaRef } from '@rc-component/textarea';

export interface MentionsContextProps {
notFoundContent: React.ReactNode;
Expand All @@ -10,6 +11,7 @@ export interface MentionsContextProps {
onFocus: React.FocusEventHandler<HTMLElement>;
onBlur: React.FocusEventHandler<HTMLElement>;
onScroll: React.UIEventHandler<HTMLElement>;
textareaRef: React.MutableRefObject<TextAreaRef>;
}

// We will never use default, here only to fix TypeScript warning
Expand Down
27 changes: 26 additions & 1 deletion tests/DropdownMenu.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ describe('DropdownMenu', () => {
// Setup component with UnstableContext for testing dropdown behavior
const { container } = render(
<UnstableContext.Provider value={{ open: true }}>
<Mentions defaultValue="@" options={generateOptions} />
<Mentions autoFocus defaultValue="@" options={generateOptions} />
</UnstableContext.Provider>,
);

Expand Down Expand Up @@ -50,4 +50,29 @@ describe('DropdownMenu', () => {

scrollIntoViewMock.mockRestore();
});

it('should NOT scroll into view when navigating without focus', async () => {
const { container } = render(
<UnstableContext.Provider value={{ open: true }}>
<Mentions defaultValue="@" options={generateOptions} />
</UnstableContext.Provider>,
);

const scrollIntoViewMock = jest
.spyOn(HTMLElement.prototype, 'scrollIntoView')
.mockImplementation(jest.fn());

simulateInput(container, '@');

for (let i = 0; i < 10; i++) {
await act(async () => {
jest.advanceTimersByTime(1000);
await Promise.resolve();
});
}

expect(scrollIntoViewMock).not.toHaveBeenCalled();

scrollIntoViewMock.mockRestore();
});
});