Skip to content

Commit

Permalink
Merge pull request #24 from szhsin/feat/grouped
Browse files Browse the repository at this point in the history
feat: grouped items
  • Loading branch information
szhsin authored Mar 29, 2024
2 parents 31e043d + d94ed58 commit 95e985f
Show file tree
Hide file tree
Showing 14 changed files with 926 additions and 49 deletions.
37 changes: 29 additions & 8 deletions dist/cjs/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -245,26 +245,47 @@ const linearTraversal = ({
traverse: isForward => {
if (!focusItem) instance.a = -1;else if (focusItem !== items[instance.a]) instance.a = items.indexOf(focusItem);
const baseIndex = traverseInput ? -1 : 0;
let nextIndex = instance.a;
let nextItem;
let newItem,
nextIndex = instance.a,
itemCounter = 0;
const itemLength = items.length;
do {
for (;;) {
if (isForward) {
if (++nextIndex >= itemLength) nextIndex = baseIndex;
} else {
if (--nextIndex < baseIndex) nextIndex = itemLength - 1;
}
nextItem = items[nextIndex];
if (!nextItem || !isItemDisabled(nextItem)) break;
} while (nextIndex !== instance.a);
newItem = items[nextIndex];
if (!newItem || !isItemDisabled(newItem)) break;
if (++itemCounter >= itemLength) return focusItem;
}
instance.a = nextIndex;
setFocusItem(nextItem);
return nextItem;
setFocusItem(newItem);
return newItem;
}
};
};

const isArray = Array.isArray;
const groupedTraversal = ({
groupedItems,
getItemsInGroup,
...restProps
}) => {
const groups = isArray(groupedItems) ? groupedItems : groupedItems ? Object.values(groupedItems) : [];
const items = [];
groups.forEach(group => {
const itemsInGroup = isArray(group) ? group : getItemsInGroup ? getItemsInGroup(group) : [];
items.push(...itemsInGroup);
});
return linearTraversal({
...restProps,
items
});
};

exports.autocomplete = autocomplete;
exports.groupedTraversal = groupedTraversal;
exports.linearTraversal = linearTraversal;
exports.supercomplete = supercomplete;
exports.useAutocomplete = useAutocomplete;
1 change: 1 addition & 0 deletions dist/esm/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ export { useAutocomplete } from './hooks/useAutocomplete.js';
export { autocomplete } from './features/autocomplete.js';
export { supercomplete } from './features/supercomplete.js';
export { linearTraversal } from './traversals/linearTraversal.js';
export { groupedTraversal } from './traversals/groupedTraversal.js';
21 changes: 21 additions & 0 deletions dist/esm/traversals/groupedTraversal.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { linearTraversal } from './linearTraversal.js';

const isArray = Array.isArray;
const groupedTraversal = ({
groupedItems,
getItemsInGroup,
...restProps
}) => {
const groups = isArray(groupedItems) ? groupedItems : groupedItems ? Object.values(groupedItems) : [];
const items = [];
groups.forEach(group => {
const itemsInGroup = isArray(group) ? group : getItemsInGroup ? getItemsInGroup(group) : [];
items.push(...itemsInGroup);
});
return linearTraversal({
...restProps,
items
});
};

export { groupedTraversal };
18 changes: 10 additions & 8 deletions dist/esm/traversals/linearTraversal.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,23 @@ const linearTraversal = ({
traverse: isForward => {
if (!focusItem) instance.a = -1;else if (focusItem !== items[instance.a]) instance.a = items.indexOf(focusItem);
const baseIndex = traverseInput ? -1 : 0;
let nextIndex = instance.a;
let nextItem;
let newItem,
nextIndex = instance.a,
itemCounter = 0;
const itemLength = items.length;
do {
for (;;) {
if (isForward) {
if (++nextIndex >= itemLength) nextIndex = baseIndex;
} else {
if (--nextIndex < baseIndex) nextIndex = itemLength - 1;
}
nextItem = items[nextIndex];
if (!nextItem || !isItemDisabled(nextItem)) break;
} while (nextIndex !== instance.a);
newItem = items[nextIndex];
if (!newItem || !isItemDisabled(newItem)) break;
if (++itemCounter >= itemLength) return focusItem;
}
instance.a = nextIndex;
setFocusItem(nextItem);
return nextItem;
setFocusItem(newItem);
return newItem;
}
};
};
Expand Down
2 changes: 1 addition & 1 deletion examples/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 95e985f

Please sign in to comment.