// Type definitions for react-bootstrap-typeahead 3.4 // Project: https://github.com/ericgio/react-bootstrap-typeahead, http://ericgio.github.io/react-bootstrap-typeahead // Definitions by: Guymestef // Rajab Shakirov // Paito Anderson // Andreas Richter // Dale Fenton // HÃ¥kon Holhjem // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped // TypeScript Version: 2.9 import * as React from 'react'; export type Omit = Pick>; export type StringPropertyNames = { [K in keyof T]: T[K] extends string ? K : never }[keyof T]; /* --------------------------------------------------------------------------- Constants and Enumerated Types --------------------------------------------------------------------------- */ export type TypeaheadModel = string|object; export type TypeaheadBsSizes = 'large' | 'lg' | 'small' | 'sm'; export type TypeaheadAlign = 'justify' | 'left' | 'right'; // if options is an object, only let labelKey be a key of that object, whose value is a string // or a custom label function that takes a single option and returns a string for the label export type TypeaheadLabelKey = T extends object ? (StringPropertyNames | ((option: T) => string)) : never; // don't allow onBlur, onChange, onFocus or onKeyDown as members of inputProps // those props should be supplied directly to or export interface InputProps extends Omit, 'onBlur'|'onChange'|'onFocus'|'onKeyDown'> { } /* --------------------------------------------------------------------------- Typeahead Contexts --------------------------------------------------------------------------- */ export interface TypeaheadContext { activeIndex?: number; hintText?: string; initialItem?: T; isOnlyResult?: boolean; onActiveItemChange?: (options: T) => void; onAdd?: (option: T) => void; onInitialItemChange?: (option: T) => void; onMenuItemClick?: (option: T, e: Event) => void; selectHintOnEnter?: boolean; } export interface TypeaheadState { activeIndex: number|null; activeItem: T|null; initialItem: T|null; isFocused: boolean; selected: T[]; showMenu: boolean; shownResults: number; text: string; } export interface InputContainerPropsSingle { 'aria-activedescendant': string; 'aria-autocomplete': 'list' | 'both'; 'aria-expanded': boolean | 'true' | 'false'; 'aria-haspopup': 'listbox'; 'aria-owns': string; autoComplete: string; disabled: boolean; inputRef: React.LegacyRef; onBlur: (e: Event) => void; onChange: (selected: T[]) => void; onClick: (e: Event) => void; onFocus: (e: Event) => void; onKeyDown: (e: Event) => void; placeholder: string|null; role: 'combobox'; value: string; } export interface InputContainerPropsMultiple extends Omit, 'role'> { inputClassName: string; labelKey: TypeaheadLabelKey; onRemove: (e: Event) => void; renderToken: (selectedItem: T, props: TypeaheadMenuProps, index: number) => React.ReactNode; role: ''; selected: T[]; } export type HintedInputContextKeys = 'hintText' | 'initialItem' | 'onAdd' | 'selectHintOnEnter'; export interface HintedInputContext extends Pick, HintedInputContextKeys> {} export type MenuItemContextKeys = 'activeIndex' | 'isOnlyResult' | 'onActiveItemChange' | 'onInitialItemChange' | 'onMenuItemClick'; export interface MenuItemContext extends Pick, MenuItemContextKeys> {} export interface TokenContext { active: boolean; onBlur: (e: any) => void; onClick: (e: any) => void; onFocus: (e: any) => void; onKeyDown: (e: any) => void; } /* --------------------------------------------------------------------------- Typeahead Props and Component --------------------------------------------------------------------------- */ export interface TypeaheadContainerProps { activeIndex: number | null; activeItem: T | null; initialItem: T | null; isFocused: boolean; selected: T[]; showMenu: boolean; shownResults: number; text: string; } export type TypeaheadResult = T & { customOption: boolean }; export interface TypeaheadProps { /* For localized accessibility: Should return a string indicating the number of results for screen readers. Receives the current results. */ a11yNumResults?: () => void; /* For localized accessibility: Should return a string indicating the number of selections for screen readers. Receives the current selections. */ a11yNumSelected?: () => void; /* Specify menu alignment. The default value is justify, which makes the menu as wide as the input and truncates long values. Specifying left or right will align the menu to that side and the width will be determined by the length of menu item values. */ align?: TypeaheadAlign; /* Allows the creation of new selections on the fly. Any new items will be added to the list of selections, but not the list of original options unless handled as such by Typeahead's parent. The newly added item will always be returned as an object even if the other options are simply strings, so be sure your onChange callback can handle this. */ allowNew?: boolean | ((results: T[], props: AllTypeaheadOwnAndInjectedProps) => boolean); /* Autofocus the input when the component initially mounts. */ autoFocus?: boolean; /* Whether to render the menu inline or attach to document.body. */ bodyContainer?: boolean; /* Specify the size of the input. */ bsSize?: TypeaheadBsSizes; /* Whether or not filtering should be case-sensitive. */ caseSensitive?: boolean; /* Displays a button to clear the input when there are selections. */ clearButton?: boolean; /* The initial value displayed in the text input. */ defaultInputValue?: string; /* Whether or not the menu is displayed upon initial render. */ defaultOpen?: boolean; /* Specify any pre-selected options. Use only if you want the component to be uncontrolled. */ defaultSelected?: T[]; /* Whether to disable the input. Will also disable selections when multiple={true}. */ disabled?: boolean; /* Specify whether the menu should appear above the input. */ dropup?: boolean; /* Message displayed in the menu when there are no valid results. Passing a falsy value will hide the menu if no matches are found. */ emptyLabel?: string; /* Either an array of fields in option to search, or a custom filtering callback. */ filterBy?: (string[] | ((option: T, props: AllTypeaheadOwnAndInjectedProps) => boolean)); /* Whether or not to automatically adjust the position of the menu when it reaches the viewport boundaries. */ flip?: boolean; /* Highlights the menu item if there is only one result and allows selecting that item by hitting enter. Does not work with allowNew. */ highlightOnlyResult?: boolean; /* An html id attribute, required for assistive technologies such as screen readers. */ id?: string | number; /* Whether the filter should ignore accents and other diacritical marks. */ ignoreDiacritics?: boolean; /* Props to be applied directly to the input. onBlur, onChange, onFocus, and onKeyDown are ignored. */ inputProps?: InputProps; /* Bootstrap 4 only. Adds the `is-invalid` classname to the `form-control`. */ isInvalid?: boolean; /* Indicate whether an asynchronous data fetch is happening. */ isLoading?: boolean; /* Bootstrap 4 only. Adds the `is-valid` classname to the `form-control`. */ isValid?: boolean; /* Specify which option key to use for display or a render function. By default, the selector will use the label key. */ labelKey?: TypeaheadLabelKey; /* Maximum height of the dropdown menu. */ maxHeight?: string; /* Maximum number of results to display by default. Mostly done for performance reasons so as not to render too many DOM nodes in the case of large data sets. */ maxResults?: number; /* DEPRECATED. Id applied to the top-level menu element. Required for accessibility. */ menuId?: string; /* Number of input characters that must be entered before showing results. */ minLength?: number; /* Whether or not multiple selections are allowed. */ multiple?: boolean; /* Provides the ability to specify a prefix before the user-entered text to indicate that the selection will be new. No-op unless allowNew={true}. */ newSelectionPrefix?: string; /* Invoked when the input is blurred. Receives an event. */ onBlur?: (e: Event) => void; /* Invoked whenever items are added or removed. Receives an array of the selected options. */ onChange?: (selected: T[]) => void; /* Invoked when the input is focused. Receives an event. */ onFocus?: (e: Event) => void; /* Invoked when the input value changes. Receives the string value of the input, as well as the original event. */ onInputChange?: (input: string, e: Event) => void; /* Invoked when a key is pressed. Receives an event. */ onKeyDown?: (e: Event) => void; /* DEPRECATED: Invoked when the menu is hidden. */ onMenuHide?: () => void; /* DEPRECATED: Invoked when the menu is shown. */ onMenuShow?: () => void; /* Invoked when menu visibility changes. */ onMenuToggle?: (show: boolean) => void; /* Invoked when the pagination menu item is clicked. */ onPaginate?: (e: Event, numResults: number) => void; /* Whether or not the menu should be displayed. undefined allows the component to control visibility, while true and false show and hide the menu, respectively. */ open?: boolean; /* Full set of options, including any pre-selected options. */ options: T[]; /* Give user the ability to display additional results if the number of results exceeds maxResults. */ paginate?: boolean; /* Prompt displayed when large data sets are paginated. */ paginationText?: string; /* Placeholder text for the input. */ placeholder?: string; /* Whether to use fixed positioning for the menu, which is useful when rendering inside a container with overflow: hidden;. Uses absolute positioning by default. */ positionFixed?: boolean; /* Callback for custom menu rendering. */ renderMenu?: (results: Array>, menuProps: any) => React.ReactNode; /* Provides a hook for customized rendering of menu item contents. */ renderMenuItemChildren?: (option: TypeaheadResult, props: TypeaheadMenuProps, index: number) => React.ReactNode; /* Provides a hook for customized rendering of tokens when multiple selections are enabled. */ renderToken?: (selectedItem: T, props: TypeaheadMenuProps, index: number) => React.ReactNode; /* The selected option(s) displayed in the input. Use this prop if you want to control the component via its parent. */ selected?: T[]; /* Allows selecting the hinted result by pressing enter. */ selectHintOnEnter?: boolean; } export type AllTypeaheadOwnAndInjectedProps = TypeaheadProps & TypeaheadContainerProps; export class Typeahead extends React.Component> { } /* --------------------------------------------------------------------------- AsyncTypeahead Props and Component --------------------------------------------------------------------------- */ export interface AsyncTypeaheadProps extends TypeaheadProps { /* Delay, in milliseconds, before performing search. */ delay?: number; /* Whether or not a request is currently pending. Necessary for the component to know when new results are available. */ isLoading: boolean; /* Callback to perform when the search is executed. */ onSearch: (query: string) => void; /* Message displayed in the menu when there is no user input. */ promptText?: React.ReactNode; /* Message to display in the menu while the request is pending. */ searchText?: React.ReactNode; /* Whether or not the component should cache query results. */ useCache?: boolean; } export class AsyncTypeahead extends React.Component> { } /* --------------------------------------------------------------------------- TypeaheadInputSingle & TypeaheadInputMulti Props and Component --------------------------------------------------------------------------- */ export interface BaseTypeaheadInputProps extends React.InputHTMLAttributes<'input'> { type: 'text'; } export interface TypeaheadSingleInputWithHocProps extends Omit>, InputContainerPropsSingle {} export interface TypeaheadMulitInputWithHocProps extends Omit>, HintedInputContext, InputContainerPropsMultiple {} export type TypeaheadInputPropKeys = 'bsSize'|'disabled'|'inputProps'|'labelKey'|'multiple'| 'onBlur'|'onChange'|'onFocus'|'onKeyDown'|'placeholder'|'renderToken'|'selected'; export type TypeaheadInputProps = Pick, TypeaheadInputPropKeys>; export class TypeaheadInputSingle extends React.Component> { } export class TypeaheadInputMulti extends React.Component> { } /* --------------------------------------------------------------------------- Highlighter Props and Component --------------------------------------------------------------------------- */ export interface HighligherProps { children: React.ReactNode; search?: string; } export class Highlighter extends React.PureComponent { } /* --------------------------------------------------------------------------- ClearButton Props and Component --------------------------------------------------------------------------- */ export interface ClearButtonProps extends React.HTMLAttributes<'button'> { bsSize?: TypeaheadBsSizes; label?: string; onClick: React.HTMLAttributes<'button'>['onClick']; // make onClick requried } export const ClearButton: React.FunctionComponent; /* --------------------------------------------------------------------------- Loader Props and Component --------------------------------------------------------------------------- */ export interface LoaderProps { bsSize: TypeaheadBsSizes; } export const Loader: React.FunctionComponent; /* --------------------------------------------------------------------------- AutosizeInput Props and Component --------------------------------------------------------------------------- */ export interface AutosizeInputProps extends Pick, 'className' | 'style'> { inputClassName?: string; inputRef?: React.LegacyRef; inputStyle?: Pick; style: React.CSSProperties; } export class AutosizeInput extends React.Component { } /* --------------------------------------------------------------------------- Menu Props and Component --------------------------------------------------------------------------- */ export interface MenuProps { id: string; className?: string; emptyLabel?: string; innerRef?: React.LegacyRef; maxHeight?: string; style?: React.CSSProperties; text?: string; } export type MenuHeaderProps = Omit, 'className'>; export class Menu extends React.Component { static Divider: React.FunctionComponent; static Header: React.FunctionComponent; } /* --------------------------------------------------------------------------- TypeaheadMenu Props and Component --------------------------------------------------------------------------- */ // prop names that Typeahead provides to TypeaheadMenu export type TypeaheadMenuPropsPick = 'labelKey' | 'newSelectionPrefix'| 'options' | 'renderMenuItemChildren'; export interface TypeaheadMenuProps extends MenuProps, Pick, TypeaheadMenuPropsPick> {} export class TypeaheadMenu extends React.Component> { } /* --------------------------------------------------------------------------- Menu Item Props and Component --------------------------------------------------------------------------- */ export interface BaseMenuItemProps extends React.HTMLProps<'li'> { active?: boolean; } export class BaseMenuItem extends React.Component { } export interface MenuItemProps extends BaseMenuItemProps { option: T; position: number; label?: string; } export class MenuItem extends React.Component> { } /* --------------------------------------------------------------------------- Overlay Props and Component --------------------------------------------------------------------------- */ export type OverlayTypeaheadProps = Pick, 'align' | 'dropup' | 'flip' | 'onMenuHide' | 'onMenuShow' | 'onMenuToggle'>; export interface OverlayProps extends OverlayTypeaheadProps { children?: React.ReactNode; className?: string; container: HTMLElement; referenceElement?: HTMLElement; show?: boolean; } export class Overlay extends React.Component { } /* --------------------------------------------------------------------------- Token Props and Component --------------------------------------------------------------------------- */ export interface TokenProps extends React.HTMLProps<'div'> { active?: boolean; onRemove?: () => void; // Token does not invoke onRemove with any parameters } export class Token extends React.Component { }