diff --git a/README.md b/README.md index 237eb4a1..b6e045a3 100644 --- a/README.md +++ b/README.md @@ -114,6 +114,7 @@ Then open `http://localhost:8000`. | onResize | `(size: { width: number; height: number }) => void` | - | Triggered when textarea size changes. | | onSearch | `(text: string, prefix: string) => void` | - | Triggered when a prefix starts a search. | | onSelect | `(option: OptionProps, prefix: string) => void` | - | Triggered when an option is selected. | +| popupRender | `(menu: React.ReactElement) => ReactNode` | - | Customize the dropdown menu rendering | ### Option diff --git a/src/KeywordTrigger.tsx b/src/KeywordTrigger.tsx index 6d0d4b06..1c25788a 100644 --- a/src/KeywordTrigger.tsx +++ b/src/KeywordTrigger.tsx @@ -52,6 +52,7 @@ interface KeywordTriggerProps { getPopupContainer?: () => HTMLElement; popupClassName?: string; popupStyle?: React.CSSProperties; + popupRender?: (menu: React.ReactElement) => React.ReactNode; } const KeywordTrigger: FC = props => { @@ -66,6 +67,7 @@ const KeywordTrigger: FC = props => { popupStyle, direction, placement, + popupRender, } = props; const dropdownPrefix = `${prefixCls}-dropdown`; @@ -89,11 +91,15 @@ const KeywordTrigger: FC = props => { return popupPlacement; }, [direction, placement]); + const dropdownPopup = popupRender + ? popupRender(dropdownElement) + : dropdownElement; + return ( ) => void; + /** + * Customize the dropdown menu rendering + * @param menu The default dropdown menu + * @returns The customized dropdown menu + */ + popupRender?: (menu: React.ReactElement) => React.ReactNode; } export interface MentionsRef { @@ -150,6 +156,7 @@ const InternalMentions = forwardRef( // @ts-expect-error visible, onPopupScroll, + popupRender, // Rest ...restProps @@ -607,6 +614,7 @@ const InternalMentions = forwardRef( getPopupContainer={getPopupContainer} popupClassName={clsx(popupClassName, mentionClassNames?.popup)} popupStyle={styles?.popup} + popupRender={popupRender} > {mergedMeasurePrefix} diff --git a/tests/Mentions.spec.tsx b/tests/Mentions.spec.tsx index 46114e2f..d0e24b4c 100644 --- a/tests/Mentions.spec.tsx +++ b/tests/Mentions.spec.tsx @@ -484,4 +484,24 @@ describe('Mentions', () => { expect(textarea).toHaveStyle({ resize: 'vertical' }); }); }); + + describe('popupRender', () => { + it('should render custom popup content', () => { + const { container, baseElement } = renderMentions({ + popupRender: menu => ( +
+
Custom Header
+ {menu} +
+ ), + }); + + simulateInput(container, '@'); + + expect(baseElement.querySelector('.custom-header')).toBeTruthy(); + expect( + baseElement.querySelector('.rc-mentions-dropdown-menu'), + ).toBeTruthy(); + }); + }); });