Commit 0ac00386 authored by Lyza Danger Gardner's avatar Lyza Danger Gardner Committed by Lyza Gardner

Convert `AutocompleteList` to TS

parent c9a34e59
...@@ -4,30 +4,45 @@ import { useMemo } from 'preact/hooks'; ...@@ -4,30 +4,45 @@ import { useMemo } from 'preact/hooks';
import MenuArrow from './MenuArrow'; import MenuArrow from './MenuArrow';
/** const defaultListFormatter = <Item,>(item: Item) => item;
* @template T
* @param {T} item
*/
const defaultListFormatter = item => item;
/** export type AutocompleteListProps<Item> = {
* @template T /**
* @typedef AutocompleteListProps * The index of the highlighted item in the `list` of items. Defaults to `-1`
* @prop {number} [activeItem] - The index of the highlighted item. * (no item selected)
* @prop {string} [id] - Optional unique HTML attribute id. This can be used */
* for parent `aria-controls` coupling. activeItem?: number;
* @prop {string} [itemPrefixId] - Optional unique HTML attribute id prefix
* for each item in the list. The final value of each items' id is /**
* `{itemPrefixId}{activeItem}` * Optional unique HTML attribute id. This can be used for parent
* @prop {T[]} list - The list of items to render. This can be a simple * `aria-controls` coupling.
* list of strings or a list of objects when used with listFormatter. */
* @prop {(item: T, index?: number) => any} [listFormatter] - An optional formatter id?: string;
* to render each item inside an <li> tag This is useful if the list is an array of
* objects rather than just strings. /**
* @prop {(item: T) => void} onSelectItem - Callback when an item is clicked with * Optional unique HTML attribute id prefix for each item in the list. The
* the mouse. * final value of each items' id is `{itemPrefixId}{activeItem}`
* @prop {boolean} [open] - Is the list open or closed? */
*/ itemPrefixId?: string;
/**
* The list of items to render. This can be a simple list of strings or a list
* of objects when used with listFormatter.
*/
list: Item[];
/**
* An optional formatter to render each item inside an <li> tag This is useful
* if the list is an array of objects rather than just strings.
*/
listFormatter?: (item: Item, index?: number) => any;
/** Callback when an item is clicked */
onSelectItem: (item: Item) => void;
/** Is the AutocompleteList currently open (visible)? */
open?: boolean;
};
/** /**
* Custom autocomplete component. Use this in conjunction with an <input> field. * Custom autocomplete component. Use this in conjunction with an <input> field.
...@@ -36,11 +51,8 @@ const defaultListFormatter = item => item; ...@@ -36,11 +51,8 @@ const defaultListFormatter = item => item;
* used by itself. * used by itself.
* *
* Modeled after the "ARIA 1.1 Combobox with Listbox Popup" * Modeled after the "ARIA 1.1 Combobox with Listbox Popup"
*
* @template T
* @param {AutocompleteListProps<T>} props
*/ */
export default function AutocompleteList({ export default function AutocompleteList<Item>({
activeItem = -1, activeItem = -1,
id, id,
itemPrefixId, itemPrefixId,
...@@ -48,7 +60,7 @@ export default function AutocompleteList({ ...@@ -48,7 +60,7 @@ export default function AutocompleteList({
listFormatter = defaultListFormatter, listFormatter = defaultListFormatter,
onSelectItem, onSelectItem,
open = false, open = false,
}) { }: AutocompleteListProps<Item>) {
const items = useMemo(() => { const items = useMemo(() => {
return list.map((item, index) => { return list.map((item, index) => {
// only add an id if itemPrefixId is passed // only add an id if itemPrefixId is passed
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment