1 | import { isString } from './nodash';
|
2 | import hasOwnProperty from './hasOwnProperty';
|
3 | export function isDisabledOption(index, items) {
|
4 | const option = items[index];
|
5 | return !!option && !isString(option) && hasOwnProperty(option, 'disabled');
|
6 | }
|
7 | export function skipDisabledOptions(currentIndex, key, items) {
|
8 | let newIndex = currentIndex;
|
9 | while (isDisabledOption(newIndex, items)) {
|
10 | newIndex += key === 'ArrowUp' ? -1 : 1;
|
11 | }
|
12 | return newIndex;
|
13 | }
|
14 | export default function getUpdatedActiveIndex(currentIndex, key, items) {
|
15 | let newIndex = currentIndex;
|
16 | newIndex += key === 'ArrowUp' ? -1 : 1;
|
17 | newIndex = skipDisabledOptions(newIndex, key, items);
|
18 | if (newIndex === items.length) {
|
19 | newIndex = -1;
|
20 | }
|
21 | else if (newIndex === -2) {
|
22 | newIndex = items.length - 1;
|
23 | newIndex = skipDisabledOptions(newIndex, key, items);
|
24 | }
|
25 | return newIndex;
|
26 | }
|