mlaursen / react-md

React material design - An accessible React component library built from the Material Design guidelines in Sass
https://react-md.dev
MIT License
2.34k stars 303 forks source link

Invalid index if no values #1465

Open i23098 opened 6 months ago

i23098 commented 6 months ago

Hi! πŸ‘‹

Firstly, thanks for your work on this project! πŸ™‚

Today I used patch-package to patch @react-md/utils@4.0.3 for the project I'm working on.

I have a dropdown that has no values in some cases (probably the best would be to not show the empty dropdown but that's how I got this error triggered). If typing with the dropdown open it will fail in react-md code as it will try to access the array in an invalid index. endIndex should not be above the values array length...

Here is the diff that solved my problem:

diff --git a/node_modules/@react-md/utils/es/search/findMatchIndex.js b/node_modules/@react-md/utils/es/search/findMatchIndex.js
index fab3aa8..60b3795 100644
--- a/node_modules/@react-md/utils/es/search/findMatchIndex.js
+++ b/node_modules/@react-md/utils/es/search/findMatchIndex.js
@@ -43,7 +43,7 @@ export function findMatchIndex(value, values, startIndex, isSelfMatchable) {
     if (isSelfMatchable === void 0) { isSelfMatchable = true; }
     var index = findMatchInRange(value, values, startIndex + 1, values.length);
     if (index === -1) {
-        var endIndex = startIndex + (isSelfMatchable ? 1 : 0);
+        var endIndex = Math.min(startIndex + (isSelfMatchable ? 1 : 0), values.length);
         index = findMatchInRange(value, values, 0, endIndex);
     }
     return index;

This issue body was partially generated by patch-package.