@point-api/dropdown-react
Version:
HOC to add a Point API autocomplete dropdown
99 lines (98 loc) • 4.98 kB
TypeScript
import { PointApi, Snippet, AutocompleteSession, AutocompleteResponse } from "@point-api/js-sdk";
import * as React from "react";
import EditableAdapter from "./EditableAdapter";
import { DropdownComponent } from "./Dropdown";
/** Props passed to a Point Autocomplete instance */
interface AutoCompleteOptions {
/** Class passed to Dropdown component */
dropdownClass?: string;
/** Toggle how snippets are displayed (standard, keyword, or hybrid) */
searchType?: string;
/** Whether to trigger autocomplete after using the tab button */
tabCompletion?: boolean;
/** Whether to store interactions */
storeInteractions?: boolean;
}
/** State variables of a Point dropdown instance */
interface AutoCompleteState {
/** List of top snippets to display in the dropdown */
suggestedSnippets: Snippet[];
/** The current Point API Response Id */
currentResponseId: string | null;
/** The current query text */
query: string | null;
matchType?: 'name' | 'content';
}
/**
* Attach a Point dropdown to a given Editable component
* @param Editable - A component containing a ContentEditable, TextArea, or Input(type=text) element
* @param pointApi - An instance of Point API wrapper object
* @param options - Additional options to pass to the dropdown
* @returns An Autocomplete component containing the editable with the attached dropdown
*/
declare function addDropdown<P>(Editable: React.ComponentType<P>, pointApi: PointApi, { dropdownClass, tabCompletion, searchType, storeInteractions, }?: AutoCompleteOptions): {
new (props: P): {
/** Ref to the attached dropdown */
dropdown: React.RefObject<DropdownComponent>;
/** An Adapter for the wrapped Editable element */
adapter: EditableAdapter | null;
/** A PointApi Instance to query snippets with */
pointApi: PointApi;
/** Current Point API Autocomplete session */
session: AutocompleteSession;
mounted: boolean;
initSession: () => Promise<void>;
componentDidMount(): void;
componentWillUnmount(): void;
createAdapter(): void;
/**
* Get the text of the current sentence a user's cursor is in
* @returns the current sentence as a regex match and type of match
*/
searchCurrentSentence(regex: RegExp): RegExpExecArray | undefined;
/**
* Query the PointApi to refresh the list of top snippets
*/
updateSuggestedSnippets(): Promise<void>;
/**
* Set state after receiving data from Point API event
*/
readonly setStateAfterRequest: (query: string, response: AutocompleteResponse | null, matchType: "content" | "name") => void;
/**
* Replace sentence text and update dropdown position
* @param snippet - selected snippet
*/
onSnippetSelect: (snippet: Snippet) => void;
storeChosenSnippetInteraction(responseId: string, snippet: Snippet): void;
onSnippetDelete: (snippetId: string) => Promise<void>;
/**
* Update the dropdown position and refresh top snippets
*/
updateDropdown: () => void;
render(): JSX.Element;
context: any;
setState<K extends "query" | "suggestedSnippets" | "currentResponseId" | "matchType">(state: AutoCompleteState | ((prevState: Readonly<AutoCompleteState>, props: Readonly<P>) => AutoCompleteState | Pick<AutoCompleteState, K> | null) | Pick<AutoCompleteState, K> | null, callback?: (() => void) | undefined): void;
forceUpdate(callback?: (() => void) | undefined): void;
readonly props: Readonly<P> & Readonly<{
children?: React.ReactNode;
}>;
state: Readonly<AutoCompleteState>;
refs: {
[key: string]: React.ReactInstance;
};
shouldComponentUpdate?(nextProps: Readonly<P>, nextState: Readonly<AutoCompleteState>, nextContext: any): boolean;
componentDidCatch?(error: Error, errorInfo: React.ErrorInfo): void;
getSnapshotBeforeUpdate?(prevProps: Readonly<P>, prevState: Readonly<AutoCompleteState>): any;
componentDidUpdate?(prevProps: Readonly<P>, prevState: Readonly<AutoCompleteState>, snapshot?: any): void;
componentWillMount?(): void;
UNSAFE_componentWillMount?(): void;
componentWillReceiveProps?(nextProps: Readonly<P>, nextContext: any): void;
UNSAFE_componentWillReceiveProps?(nextProps: Readonly<P>, nextContext: any): void;
componentWillUpdate?(nextProps: Readonly<P>, nextState: Readonly<AutoCompleteState>, nextContext: any): void;
UNSAFE_componentWillUpdate?(nextProps: Readonly<P>, nextState: Readonly<AutoCompleteState>, nextContext: any): void;
};
displayName: string;
contextType?: React.Context<any> | undefined;
};
export declare type AutoCompleteInstance = InstanceType<ReturnType<typeof addDropdown>>;
export default addDropdown;