UNPKG

1.06 kBJavaScriptView Raw
1import { isString } from './nodash';
2import hasOwnProperty from './hasOwnProperty';
3export function isDisabledOption(index, items) {
4 var option = items[index];
5 return !!option && !isString(option) && hasOwnProperty(option, 'disabled');
6}
7export function skipDisabledOptions(currentIndex, key, items) {
8 var newIndex = currentIndex;
9
10 while (isDisabledOption(newIndex, items)) {
11 newIndex += key === 'ArrowUp' ? -1 : 1;
12 }
13
14 return newIndex;
15}
16export default function getUpdatedActiveIndex(currentIndex, key, items) {
17 var newIndex = currentIndex; // Increment or decrement index based on user keystroke.
18
19 newIndex += key === 'ArrowUp' ? -1 : 1; // Skip over any disabled options.
20
21 newIndex = skipDisabledOptions(newIndex, key, items); // If we've reached the end, go back to the beginning or vice-versa.
22
23 if (newIndex === items.length) {
24 newIndex = -1;
25 } else if (newIndex === -2) {
26 newIndex = items.length - 1; // Skip over any disabled options.
27
28 newIndex = skipDisabledOptions(newIndex, key, items);
29 }
30
31 return newIndex;
32}
\No newline at end of file