import * as React from 'react'; import type { SuggestionProps, SuggestionKeyDownProps } from '@tiptap/suggestion'; import type { MentionUser } from './mention.js'; export interface MentionListHandle { onKeyDown: (props: SuggestionKeyDownProps) => boolean; } /** Keyboard-navigable people picker shown while typing `@`. */ export const MentionList = React.forwardRef>( function MentionList(props, ref) { const { items, command } = props; const [selected, setSelected] = React.useState(0); React.useEffect(() => setSelected(0), [items]); const select = React.useCallback( (index: number) => { const item = items[index]; if (item) command({ id: item.id, label: item.label }); }, [items, command] ); React.useImperativeHandle(ref, () => ({ onKeyDown: ({ event }) => { if (event.key === 'ArrowUp') { setSelected(s => (s + items.length - 1) % items.length); return true; } if (event.key === 'ArrowDown') { setSelected(s => (s + 1) % items.length); return true; } if (event.key === 'Enter') { select(selected); return true; } return false; }, })); return (
{items.length === 0 ? (
No people
) : ( items.map((item, index) => ( )) )}
); } );