From 339e5ba9d48bcdaf28121baf209ca4337e4834c3 Mon Sep 17 00:00:00 2001 From: Silviu Avram Date: Thu, 7 Jan 2021 21:08:36 +0200 Subject: [PATCH] getItemIndexByCharacterKey --- .size-snapshot.json | 24 +++++++-------- src/hooks/useMultipleSelection/utils.js | 15 ++++++---- src/hooks/useSelect/utils.js | 39 ++++++++----------------- 3 files changed, 34 insertions(+), 44 deletions(-) diff --git a/.size-snapshot.json b/.size-snapshot.json index 444dbc62c..7c8493cb6 100644 --- a/.size-snapshot.json +++ b/.size-snapshot.json @@ -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, @@ -24,8 +24,8 @@ } }, "downshift.cjs.js": { - "bundled": 147041, - "minified": 67338, - "gzipped": 14184 + "bundled": 146699, + "minified": 67255, + "gzipped": 14157 } } diff --git a/src/hooks/useMultipleSelection/utils.js b/src/hooks/useMultipleSelection/utils.js index 789e9ff04..6c4c64781 100644 --- a/src/hooks/useMultipleSelection/utils.js +++ b/src/hooks/useMultipleSelection/utils.js @@ -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) } @@ -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') @@ -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 } @@ -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, @@ -139,4 +139,9 @@ if (process.env.NODE_ENV !== 'production') { } } -export {validatePropTypes} +export { + validatePropTypes, + getDefaultValue, + getInitialState, + isKeyDownOperationPermitted, +} diff --git a/src/hooks/useSelect/utils.js b/src/hooks/useSelect/utils.js index 926ea5557..57782a4ec 100644 --- a/src/hooks/useSelect/utils.js +++ b/src/hooks/useSelect/utils.js @@ -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 + } } }