2022-08-18 17:36:20 +00:00
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
2022-09-16 20:35:14 +00:00
|
|
|
import { getItemLabelType, DefaultItemType } from './types';
|
2022-08-18 17:36:20 +00:00
|
|
|
|
2022-09-16 20:35:14 +00:00
|
|
|
function isDefaultItemType< ItemType >(
|
|
|
|
item: ItemType | DefaultItemType | null
|
|
|
|
): item is DefaultItemType {
|
|
|
|
return (
|
|
|
|
Boolean( item ) &&
|
|
|
|
( item as DefaultItemType ).label !== undefined &&
|
|
|
|
( item as DefaultItemType ).value !== undefined
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
export const defaultGetItemLabel = < ItemType >( item: ItemType | null ) => {
|
|
|
|
if ( isDefaultItemType< ItemType >( item ) ) {
|
|
|
|
return item.label;
|
|
|
|
}
|
|
|
|
return '';
|
|
|
|
};
|
|
|
|
|
|
|
|
export const defaultGetItemValue = < ItemType >( item: ItemType | null ) => {
|
|
|
|
if ( isDefaultItemType< ItemType >( item ) ) {
|
|
|
|
return item.value;
|
|
|
|
}
|
|
|
|
return '';
|
2022-08-18 17:36:20 +00:00
|
|
|
};
|
|
|
|
|
2022-09-16 20:35:14 +00:00
|
|
|
export const defaultGetFilteredItems = < ItemType >(
|
2022-08-18 17:36:20 +00:00
|
|
|
allItems: ItemType[],
|
|
|
|
inputValue: string,
|
2022-09-16 20:35:14 +00:00
|
|
|
selectedItems: ItemType[],
|
|
|
|
getItemLabel: getItemLabelType< ItemType >
|
2022-08-18 17:36:20 +00:00
|
|
|
) => {
|
|
|
|
return allItems.filter(
|
|
|
|
( item ) =>
|
|
|
|
selectedItems.indexOf( item ) < 0 &&
|
2022-09-16 20:35:14 +00:00
|
|
|
getItemLabel( item )
|
|
|
|
.toLowerCase()
|
|
|
|
.startsWith( inputValue.toLowerCase() )
|
2022-08-18 17:36:20 +00:00
|
|
|
);
|
|
|
|
};
|