-
Notifications
You must be signed in to change notification settings - Fork 321
Description
Describe the Bug
The AutocompleteInput component experiences noticeable lag and slowness, especially when the user is typing quickly.
It appears the performance issue is related to the onKeyDown event handler. The handler is currently defined as an inline function, which causes it to be recreated on every single render of the component. This likely triggers unnecessary re-renders in the underlying Mantine Textarea and adds performance overhead during rapid key presses.
Proposed Solution
To fix this, I propose refactoring the event handlers to prevent them from being recreated on every render. This involves using the useCallback hook.
The "delay logic" for fetching suggestions is already correctly handled by useDebounce, so this change will specifically optimize the keyboard event handling itself.
The planned changes are:
- Memoize
onAcceptSuggestion: Wrap theonAcceptSuggestionhelper function inuseCallback. - Memoize
onKeyDown: Extract theonKeyDownlogic into a new function calledhandleKeyDownand wrap it inuseCallbackwith its proper dependency array (includingonAcceptSuggestion,suggestedProperties,value, etc.). - Assign Memoized Handler: Update the
Textareaprop to use the memoized handler:<Textarea ... onKeyDown={handleKeyDown} />.
This should significantly improve the responsiveness of the input field by stabilizing the function props passed to the Textarea.
Is this a known issue, or would you be open to a PR with this optimization? Thanks!