Skip to content

Commit

Permalink
Merge branch 'autocomplete' into beta
Browse files Browse the repository at this point in the history
  • Loading branch information
davidmz committed Jun 27, 2024
2 parents 657784d + 29bd8bc commit 43bce9e
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 7 deletions.
1 change: 1 addition & 0 deletions src/components/autocomplete/autocomplete.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -131,4 +131,5 @@ function replaceQuery(input, replacement) {
input.setSelectionRange(newCaretPos, newCaretPos);

input.dispatchEvent(new Event('input', { bubbles: true }));
input.updateStates?.();
}
17 changes: 10 additions & 7 deletions src/components/smart-textarea.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,13 @@ export const SmartTextarea = forwardRef(function SmartTextarea(
};
}, [cancelEmptyDraftOnBlur, draftKey, ref]);

ref.current.insertText = useDebouncedInsert(100, ref, onText, draftKey);
// Public component methods
ref.current.insertText = useDebouncedInsert(100, ref);
ref.current.updateStates = useCallback(() => {
const text = ref.current.value;
onText?.(text);
draftKey && setDraftField(draftKey, 'text', text);
}, [draftKey, onText, ref]);

useEffect(() => {
if (!draftKey && !onText) {
Expand Down Expand Up @@ -242,7 +248,7 @@ function containsFiles(dndEvent) {
return false;
}

function useDebouncedInsert(interval, inputRef, onText, draftKey) {
function useDebouncedInsert(interval, inputRef) {
const queue = useRef([]);
const timer = useRef(0);

Expand All @@ -267,11 +273,8 @@ function useDebouncedInsert(interval, inputRef, onText, draftKey) {
input.value = text;
input.setSelectionRange(selStart, selEnd);
input.focus();
onText?.(input.value);
if (draftKey) {
setDraftField(draftKey, 'text', input.value);
}
}, [draftKey, inputRef, onText]);
input.updateStates();
}, [inputRef]);

return useCallback(
(insertion) => {
Expand Down

0 comments on commit 43bce9e

Please sign in to comment.