@material/list
Version:
The Material Components for the web list component
124 lines (123 loc) • 5.16 kB
TypeScript
/**
* @license
* Copyright 2020 Google Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
import { MDCListTextAndIndex } from './types';
/**
* State of a typeahead instance.
*/
export interface TypeaheadState {
typeaheadBuffer: string;
currentFirstChar: string;
sortedIndexCursor: number;
bufferClearTimeout: number;
}
/**
* Initializes a state object for typeahead. Use the same reference for calls to
* typeahead functions.
*
* @return The current state of the typeahead process. Each state reference
* represents a typeahead instance as the reference is typically mutated
* in-place.
*/
export declare function initState(): TypeaheadState;
/**
* Initializes typeahead state by indexing the current list items by primary
* text into the sortedIndexByFirstChar data structure.
*
* @param listItemCount numer of items in the list
* @param getPrimaryTextByItemIndex function that returns the primary text at a
* given index
*
* @return Map that maps the first character of the primary text to the full
* list text and it's index
*/
export declare function initSortedIndex(listItemCount: number, getPrimaryTextByItemIndex: (index: number) => string): Map<string, MDCListTextAndIndex[]>;
/**
* Arguments for matchItem
*/
export interface TypeaheadMatchItemOpts {
focusItemAtIndex: (index: number) => void;
nextChar: string;
focusedItemIndex: number;
sortedIndexByFirstChar: Map<string, MDCListTextAndIndex[]>;
skipFocus: boolean;
isItemAtIndexDisabled: (index: number) => boolean;
}
/**
* Given the next desired character from the user, it attempts to find the next
* list option matching the buffer. Wraps around if at the end of options.
*
* @param opts Options and accessors
* - nextChar - the next character to match against items
* - sortedIndexByFirstChar - output of `initSortedIndex(...)`
* - focusedItemIndex - the index of the currently focused item
* - focusItemAtIndex - function that focuses a list item at given index
* - skipFocus - whether or not to focus the matched item
* - isItemAtIndexDisabled - function that determines whether an item at a
* given index is disabled
* @param state The typeahead state instance. See `initState`.
*
* @return The index of the matched item, or -1 if no match.
*/
export declare function matchItem(opts: TypeaheadMatchItemOpts, state: TypeaheadState): number;
/**
* Whether or not the given typeahead instaance state is currently typing.
*
* @param state The typeahead state instance. See `initState`.
*/
export declare function isTypingInProgress(state: TypeaheadState): boolean;
/**
* Options for handleKeydown.
*/
export interface HandleKeydownOpts {
event: KeyboardEvent;
isTargetListItem: boolean;
focusItemAtIndex: (index: number) => void;
focusedItemIndex: number;
sortedIndexByFirstChar: Map<string, MDCListTextAndIndex[]>;
isItemAtIndexDisabled: (index: number) => boolean;
}
/**
* Clears the typeahaed buffer so that it resets item matching to the first
* character.
*
* @param state The typeahead state instance. See `initState`.
*/
export declare function clearBuffer(state: TypeaheadState): void;
/**
* Given a keydown event, it calculates whether or not to automatically focus a
* list item depending on what was typed mimicing the typeahead functionality of
* a standard <select> element that is open.
*
* @param opts Options and accessors
* - event - the KeyboardEvent to handle and parse
* - sortedIndexByFirstChar - output of `initSortedIndex(...)`
* - focusedItemIndex - the index of the currently focused item
* - focusItemAtIndex - function that focuses a list item at given index
* - isItemAtFocusedIndexDisabled - whether or not the currently focused item
* is disabled
* - isTargetListItem - whether or not the event target is a list item
* @param state The typeahead state instance. See `initState`.
*
* @returns index of the item matched by the keydown. -1 if not matched.
*/
export declare function handleKeydown(opts: HandleKeydownOpts, state: TypeaheadState): number;