Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add a generic type for suggestion items #2610

Merged
merged 1 commit into from
Apr 28, 2022
Merged
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
28 changes: 14 additions & 14 deletions packages/suggestion/src/suggestion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { EditorState, Plugin, PluginKey } from 'prosemirror-state'
import { Decoration, DecorationSet, EditorView } from 'prosemirror-view'
import { findSuggestionMatch } from './findSuggestionMatch'

export interface SuggestionOptions {
export interface SuggestionOptions<I = any> {
pluginKey?: PluginKey,
editor: Editor,
char?: string,
Expand All @@ -15,18 +15,18 @@ export interface SuggestionOptions {
command?: (props: {
editor: Editor,
range: Range,
props: any,
props: I,
}) => void,
items?: (props: {
query: string,
editor: Editor,
}) => any[] | Promise<any[]>,
}) => I[] | Promise<I[]>,
render?: () => {
onBeforeStart?: (props: SuggestionProps) => void
onStart?: (props: SuggestionProps) => void,
onBeforeUpdate?: (props: SuggestionProps) => void
onUpdate?: (props: SuggestionProps) => void,
onExit?: (props: SuggestionProps) => void,
onBeforeStart?: (props: SuggestionProps<I>) => void
onStart?: (props: SuggestionProps<I>) => void,
onBeforeUpdate?: (props: SuggestionProps<I>) => void
onUpdate?: (props: SuggestionProps<I>) => void,
onExit?: (props: SuggestionProps<I>) => void,
onKeyDown?: (props: SuggestionKeyDownProps) => boolean,
},
allow?: (props: {
Expand All @@ -36,13 +36,13 @@ export interface SuggestionOptions {
}) => boolean,
}

export interface SuggestionProps {
export interface SuggestionProps<I = any> {
editor: Editor,
range: Range,
query: string,
text: string,
items: any[],
command: (props: any) => void,
items: I[],
command: (props: I) => void,
decorationNode: Element | null,
clientRect: (() => DOMRect) | null,
}
Expand All @@ -55,7 +55,7 @@ export interface SuggestionKeyDownProps {

export const SuggestionPluginKey = new PluginKey('suggestion')

export function Suggestion({
export function Suggestion<I = any>({
pluginKey = SuggestionPluginKey,
editor,
char = '@',
Expand All @@ -68,9 +68,9 @@ export function Suggestion({
items = () => [],
render = () => ({}),
allow = () => true,
}: SuggestionOptions) {
}: SuggestionOptions<I>) {

let props: SuggestionProps | undefined
let props: SuggestionProps<I> | undefined
const renderer = render?.()

return new Plugin({
Expand Down