Skip to content

Commit

Permalink
getItemIndexByCharacterKey
Browse files Browse the repository at this point in the history
  • Loading branch information
silviuaavram committed Jan 7, 2021
1 parent 61af7c4 commit 339e5ba
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 44 deletions.
24 changes: 12 additions & 12 deletions .size-snapshot.json
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
{
"downshift.umd.min.js": {
"bundled": 155548,
"minified": 51408,
"gzipped": 13925
"bundled": 155186,
"minified": 51325,
"gzipped": 13899
},
"downshift.umd.js": {
"bundled": 199921,
"minified": 69516,
"gzipped": 18265
"bundled": 199559,
"minified": 69433,
"gzipped": 18236
},
"downshift.esm.js": {
"bundled": 143636,
"minified": 64364,
"gzipped": 14049,
"bundled": 143294,
"minified": 64281,
"gzipped": 14023,
"treeshaked": {
"rollup": {
"code": 2274,
Expand All @@ -24,8 +24,8 @@
}
},
"downshift.cjs.js": {
"bundled": 147041,
"minified": 67338,
"gzipped": 14184
"bundled": 146699,
"minified": 67255,
"gzipped": 14157
}
}
15 changes: 10 additions & 5 deletions src/hooks/useMultipleSelection/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ function getInitialValue(props, propKey) {
* @param {string} propKey Props key to generate the value for.
* @returns {any} The initial value for that prop.
*/
export function getDefaultValue(props, propKey) {
function getDefaultValue(props, propKey) {
return getDefaultValueCommon(props, propKey, defaultStateValues)
}

Expand All @@ -43,7 +43,7 @@ export function getDefaultValue(props, propKey) {
* @param {Object} props Props passed to the hook.
* @returns {Object} The initial state.
*/
export function getInitialState(props) {
function getInitialState(props) {
const activeIndex = getInitialValue(props, 'activeIndex')
const selectedItems = getInitialValue(props, 'selectedItems')

Expand All @@ -62,7 +62,7 @@ export function getInitialState(props) {
* @param {KeyboardEvent} event The event from keydown.
* @returns {boolean} Whether the operation is allowed.
*/
export function isKeyDownOperationPermitted(event) {
function isKeyDownOperationPermitted(event) {
if (event.shiftKey || event.metaKey || event.ctrlKey || event.altKey) {
return false
}
Expand Down Expand Up @@ -96,7 +96,7 @@ function getA11yRemovalMessage(selectionParameters) {
return `${itemToStringLocal(removedSelectedItem)} has been removed.`
}

export const propTypes = {
const propTypes = {
selectedItems: PropTypes.array,
initialSelectedItems: PropTypes.array,
defaultSelectedItems: PropTypes.array,
Expand Down Expand Up @@ -139,4 +139,9 @@ if (process.env.NODE_ENV !== 'production') {
}
}

export {validatePropTypes}
export {
validatePropTypes,
getDefaultValue,
getInitialState,
isKeyDownOperationPermitted,
}
39 changes: 12 additions & 27 deletions src/hooks/useSelect/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,39 +6,24 @@ function getItemIndexByCharacterKey(
keysSoFar,
highlightedIndex,
items,
itemToStringParam,
itemToString,
getItemNodeFromIndex,
) {
const lowerCasedItemStrings = items.map(item =>
itemToStringParam(item).toLowerCase(),
)
const lowerCasedKeysSoFar = keysSoFar.toLowerCase()
const isValid = (itemString, index) => {
const element = getItemNodeFromIndex(index)

return (
itemString.startsWith(lowerCasedKeysSoFar) &&
!(element && element.hasAttribute('disabled'))
)
}

for (
let index = highlightedIndex + 1;
index < lowerCasedItemStrings.length;
index++
) {
const itemString = lowerCasedItemStrings[index]

if (isValid(itemString, index)) {
return index
}
}
for (let index = 0; index < items.length; index++) {
const offsetIndex = (index + highlightedIndex + 1) % items.length

for (let index = 0; index < highlightedIndex; index++) {
const itemString = lowerCasedItemStrings[index]
if (
itemToString(items[offsetIndex])
.toLowerCase()
.startsWith(lowerCasedKeysSoFar)
) {
const element = getItemNodeFromIndex(offsetIndex)

if (isValid(itemString, index)) {
return index
if (!(element && element.hasAttribute('disabled'))) {
return offsetIndex
}
}
}

Expand Down

0 comments on commit 339e5ba

Please sign in to comment.