Skip to content

Commit

Permalink
Fix the autocomplete pattern search logic for the zero-width pattern …
Browse files Browse the repository at this point in the history
…case
  • Loading branch information
davidmz committed Oct 25, 2024
1 parent 0c9445b commit 1f75f8e
Showing 1 changed file with 9 additions and 18 deletions.
27 changes: 9 additions & 18 deletions src/components/autocomplete/autocomplete.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -105,31 +105,22 @@ export function Autocomplete({ inputRef, context, anchor = defaultAnchor }) {
* @returns {[number, number]|null}
*/
function getQueryPosition({ value, selectionStart }, anchor) {
if (!selectionStart) {
return null;
}

anchor.lastIndex = 0;

let found = -1;
while (anchor.exec(value) !== null) {
if (anchor.lastIndex > selectionStart) {
const pos = anchor.lastIndex;
if (pos > selectionStart) {
break;
}
found = anchor.lastIndex;
}

if (found === -1) {
return null;
}

const match = value.slice(found).match(/^[a-z\d-]+/i)?.[0];
// Check that the caret is inside the match or is at its edge
if (!match || match.length <= selectionStart - found - 1) {
return null;
const match = value.slice(pos).match(/^[a-z\d-]+/i)?.[0];
// Check that the caret is inside the match or is at its edge
if (match && match.length > selectionStart - pos - 1) {
return [pos, pos + match.length];
}
anchor.lastIndex = pos + 1;
}

return [found, found + match.length];
return null;
}

/**
Expand Down

0 comments on commit 1f75f8e

Please sign in to comment.