fix(tag-dropdown): last char dropped bug#945
Conversation
|
The latest updates on your projects. Learn more about Vercel for Git ↗︎
1 Skipped Deployment
|
There was a problem hiding this comment.
Greptile Summary
This PR fixes a race condition bug in the tag dropdown functionality where the last character would be dropped when users typed variable values ending with a closing bracket '>'.
The core issue occurred because React's asynchronous state updates created timing problems between the tag dropdown's closure logic and character entry processing. When users typed a closing bracket, it would trigger the checkTagTrigger function to close the dropdown while simultaneously trying to process that final character, resulting in the character being lost.
The fix implements two key strategies across multiple input components (short-input, long-input, code, combobox, and iteration-badges):
-
State Update Ordering: The cursor position (
setCursorPosition) is now updated before the store value (setStoreValue) in all input change handlers. This ensures that dropdown logic operating on cursor position has access to the most current state when making decisions about whether to show or hide the tag dropdown. -
Live DOM Tracking: In the
tag-dropdown.tsxcomponent, thehandleTagSelectfunction now reads directly from the DOM's active element to get the current cursor position and input value, rather than relying solely on potentially stale React state. This prevents the tag replacement logic from operating on outdated information.
The changes are applied consistently across all input components that interact with the tag dropdown system, including text inputs, textareas, code editors, and comboboxes. The fix maintains backward compatibility by using fallback values when DOM access isn't available, and doesn't alter the fundamental logic flow of these components.
Confidence score: 4/5
- This PR addresses a specific, well-identified race condition with a targeted solution that doesn't introduce breaking changes
- The fix is applied consistently across multiple input components, showing thorough consideration of the problem
- Pay close attention to the tag dropdown interaction logic and ensure manual testing covers various typing speeds and patterns
6 files reviewed, 1 comment
| if (typeof (activeEl as any).value === 'string') { | ||
| liveValue = (activeEl as any).value | ||
| } |
There was a problem hiding this comment.
style: The (activeEl as any).value type assertion could be more specific. Consider type guard or explicit HTMLInputElement/HTMLTextAreaElement check
Context Used: Context - Avoid using type assertions to 'any' in TypeScript. Instead, ensure proper type definitions are used to maintain type safety. (link)
Summary
The closing bracket ('>') was triggering an effect to close the tag dropdown, that was racing with the entry of the last character. Could be the cause of the bug. Use live dom tracking in the tag dropdown to prevent this. Also move set cursor before store value setter for extra precaution.
Type of Change
Testing
Tested manually typing out variable values.
Checklist