Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Doublecheck the input length at the last moment #137

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

duffyjp
Copy link
Contributor

@duffyjp duffyjp commented Jun 5, 2023

I've had issues where deleting characters in the input field faster than the server is responding to queries can leave old results on screen, even if the entire field is cleared. It largely occurs when holding the backspace key and releasing either exactly when no characters exist or while the count is < the minLength. The issue is most noticeable if your data-autocomplete-delay-value is set very low (mine is 11).

Things that didn't work:

  • Add a timestamp in a new data attribute with Date.now() and verify the result being rendered is the newest based on when it started rather than was completed in async. They almost never came in out of order so that wasn't it.
  • Remove the debounce entirely. Had no effect.

What did work:

  • At the very last moment in replaceResults(html) double check the input length and if it's less than minLengthValue delete the innerHTML instead of replacing it.

Why it works (AFAIK):

  • The input length was only checked before the query is dispatched. If while the query was being processed the user deleted enough characters to be under the minimum, but before the async stuff finishes it still gets displayed. You'll notice if watch really closely on the Before gif below that the results are removed correctly by onInputChange when characters are deleted but redisplayed by the async query when it finishes.

Example:

  • data-autocomplete-min-length-value="3"
  • data-autocomplete-delay-value="11"
Before After
stimulus-autocomplete-main 2023-06-05 16_00_58 stimulus-autocomplete-update 2023-06-05 16_02_07

@netlify
Copy link

netlify bot commented Jun 5, 2023

Deploy Preview for stimulus-autocomplete canceled.

Name Link
🔨 Latest commit c12cc00
🔍 Latest deploy log https://app.netlify.com/sites/stimulus-autocomplete/deploys/647e4ffc07a8800008a23f59

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant