| 1 |
/** |
| 2 |
* Custom override of @wordpress/components autocomplete types. |
| 3 |
* |
| 4 |
* Path mapping is required because ReplaceOption is a type alias (not interface), |
| 5 |
* which cannot be augmented via `declare module`. This file completely replaces |
| 6 |
* the original types when importing from "@wordpress/components/build-types/autocomplete/types". |
| 7 |
* |
| 8 |
* Modified: ReplaceOption.value allows string in addition to RichTextValue, |
| 9 |
* which is the actual runtime behavior when using custom autocompleters. |
| 10 |
*/ |
| 11 |
|
| 12 |
import type { ReactElement } from "react"; |
| 13 |
import type { RichTextValue } from "@wordpress/rich-text"; |
| 14 |
|
| 15 |
export type InsertOption = { |
| 16 |
action: "insert-at-caret"; |
| 17 |
value: React.ReactNode; |
| 18 |
}; |
| 19 |
|
| 20 |
export type ReplaceOption = { |
| 21 |
action: "replace"; |
| 22 |
value: RichTextValue | string; |
| 23 |
}; |
| 24 |
|
| 25 |
export type OptionCompletion = React.ReactNode | InsertOption | ReplaceOption; |
| 26 |
|
| 27 |
type OptionLabel = string | ReactElement | Array<string | ReactElement>; |
| 28 |
|
| 29 |
export type KeyedOption = { |
| 30 |
key: string; |
| 31 |
value: any; |
| 32 |
label: OptionLabel; |
| 33 |
keywords: Array<string>; |
| 34 |
isDisabled: boolean; |
| 35 |
}; |
| 36 |
|
| 37 |
export type WPCompleter<TCompleterOption = any> = { |
| 38 |
name: string; |
| 39 |
triggerPrefix: string; |
| 40 |
options: |
| 41 |
| (( |
| 42 |
query: string |
| 43 |
) => |
| 44 |
| PromiseLike<readonly TCompleterOption[]> |
| 45 |
| readonly TCompleterOption[]) |
| 46 |
| readonly TCompleterOption[]; |
| 47 |
getOptionKeywords?: (option: TCompleterOption) => Array<string>; |
| 48 |
isOptionDisabled?: (option: TCompleterOption) => boolean; |
| 49 |
getOptionLabel: (option: TCompleterOption) => OptionLabel; |
| 50 |
allowContext?: (before: string, after: string) => boolean; |
| 51 |
getOptionCompletion?: ( |
| 52 |
option: TCompleterOption, |
| 53 |
query: string |
| 54 |
) => OptionCompletion; |
| 55 |
useItems?: (filterValue: string) => readonly [Array<KeyedOption>]; |
| 56 |
isDebounced?: boolean; |
| 57 |
className?: string; |
| 58 |
}; |
| 59 |
|
| 60 |
type ContentRef = React.RefObject<HTMLElement>; |
| 61 |
|
| 62 |
export type AutocompleterUIProps = { |
| 63 |
filterValue: string; |
| 64 |
instanceId: number; |
| 65 |
listBoxId: string | undefined; |
| 66 |
className?: string; |
| 67 |
selectedIndex: number; |
| 68 |
onChangeOptions: (items: Array<KeyedOption>) => void; |
| 69 |
onSelect: (option: KeyedOption) => void; |
| 70 |
onReset?: () => void; |
| 71 |
reset: (event: Event) => void; |
| 72 |
value?: RichTextValue; |
| 73 |
contentRef: ContentRef; |
| 74 |
}; |
| 75 |
|
| 76 |
export type CancelablePromise<T = void> = Promise<T> & { |
| 77 |
canceled?: boolean; |
| 78 |
}; |
| 79 |
|
| 80 |
export type UseAutocompleteProps = { |
| 81 |
record: RichTextValue & { |
| 82 |
start: NonNullable<RichTextValue["start"]>; |
| 83 |
end: NonNullable<RichTextValue["end"]>; |
| 84 |
}; |
| 85 |
onChange: (value: RichTextValue) => void; |
| 86 |
onReplace: (values: RichTextValue[]) => void; |
| 87 |
completers: Array<WPCompleter>; |
| 88 |
contentRef: ContentRef; |
| 89 |
}; |
| 90 |
|
| 91 |
export type AutocompleteProps = UseAutocompleteProps & { |
| 92 |
children: ( |
| 93 |
props: Omit<ReturnType<typeof import(".").useAutocomplete>, "popover"> |
| 94 |
) => React.ReactNode; |
| 95 |
isSelected: boolean; |
| 96 |
}; |
| 97 |
|