The open alternative to Salesforce, designed for AI.
TypeScript crmcrm-systemcustomergood-first-issuegraphqlhacktoberfestjavascriptmarketingmonoreponestjsopen-sourcepostgresqlreactreactjssalestypescriptweb
import { isDefined } from '@ui/utilities/utils/isDefined';
import { compareDropdownElementOrder } from './compareDropdownElementOrder';
import { getNextDropdownGridItem } from './getNextDropdownGridItem';
export const getNextDropdownItem = ({
key,
items,
currentIndex,
search,
isSearch,
isRightToLeft = false,
}: {
key: string;
items: HTMLElement[];
currentIndex: number;
search: HTMLInputElement | null;
isSearch: boolean;
isRightToLeft?: boolean;
}): HTMLElement | undefined => {
const gridItem = getNextDropdownGridItem({
key,
items,
currentItem: items[currentIndex],
search,
isRightToLeft,
});
if (isDefined(gridItem)) {
return gridItem;
}
const lastIndex = items.length - 1;
const lastItem = items[lastIndex];
const isSearchAfterItems =
isDefined(search) &&
isDefined(lastItem) &&
compareDropdownElementOrder({
firstElement: lastItem,
secondElement: search,
}) < 0;
if (key === 'ArrowDown' && currentIndex === lastIndex && isSearchAfterItems) {
return search ?? undefined;
}
if (key === 'ArrowDown') {
return items[currentIndex === lastIndex ? 0 : currentIndex + 1];
}
if (
key === 'ArrowUp' &&
currentIndex === 0 &&
isDefined(search) &&
!isSearchAfterItems
) {
return search;
}
if (key === 'ArrowUp') {
return items[currentIndex <= 0 ? lastIndex : currentIndex - 1];
}
if (!isSearch && key === 'Home') {
return items[0];
}
if (!isSearch && key === 'End') {
return items[lastIndex];
}
return undefined;
};