-
Notifications
You must be signed in to change notification settings - Fork 933
/
Copy pathutils.ts
60 lines (52 loc) · 1.52 KB
/
utils.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import PropTypes from 'prop-types'
import {
commonDropdownPropTypes,
defaultProps as commonDefaultProps,
} from '../utils'
import {noop} from '../../utils'
import {GetItemIndexByCharacterKeyOptions} from './types'
export function getItemIndexByCharacterKey<Item>({
keysSoFar,
highlightedIndex,
items,
itemToString,
isItemDisabled,
}: GetItemIndexByCharacterKeyOptions<Item>) {
const lowerCasedKeysSoFar = keysSoFar.toLowerCase()
for (let index = 0; index < items.length; index++) {
// if we already have a search query in progress, we also consider the current highlighted item.
const offsetIndex =
(index + highlightedIndex + (keysSoFar.length < 2 ? 1 : 0)) % items.length
const item = items[offsetIndex]
if (
item !== undefined &&
itemToString(item).toLowerCase().startsWith(lowerCasedKeysSoFar) &&
!isItemDisabled(item, offsetIndex)
) {
return offsetIndex
}
}
return highlightedIndex
}
const propTypes = {
...commonDropdownPropTypes,
items: PropTypes.array.isRequired,
isItemDisabled: PropTypes.func,
}
export const defaultProps = {
...commonDefaultProps,
isItemDisabled() {
return false
},
}
// eslint-disable-next-line import/no-mutable-exports
export let validatePropTypes = noop as (
options: unknown,
caller: Function,
) => void
/* istanbul ignore next */
if (process.env.NODE_ENV !== 'production') {
validatePropTypes = (options: unknown, caller: Function): void => {
PropTypes.checkPropTypes(propTypes, options, 'prop', caller.name)
}
}