fix(work-orders): keep mention cursor sync on current value [SH-217]

This commit is contained in:
Arthur Bassi 2026-08-10 13:37:56 -03:00
parent 95bd443359
commit 8783aa6e18

View file

@ -35,6 +35,8 @@ export function CommentMentionComposer({
onSubmit,
}: CommentMentionComposerProps) {
const inputRef = useRef<HTMLTextAreaElement | HTMLInputElement>(null);
const valueRef = useRef(value);
valueRef.current = value;
const [anchorEl, setAnchorEl] = useState<HTMLElement | null>(null);
const [cursor, setCursor] = useState(0);
const [menuOpen, setMenuOpen] = useState(false);
@ -59,12 +61,14 @@ export function CommentMentionComposer({
return;
}
const nextCursor = element.selectionStart ?? value.length;
const currentValue = valueRef.current;
const nextCursor = element.selectionStart ?? currentValue.length;
setCursor(nextCursor);
setMenuOpen(Boolean(findActiveMentionQuery(value, nextCursor)));
}, [value]);
setMenuOpen(Boolean(findActiveMentionQuery(currentValue, nextCursor)));
}, []);
const handleChange = (nextValue: string) => {
valueRef.current = nextValue;
onChange(nextValue);
requestAnimationFrame(syncCursor);
};