adjustment: added clearTimeout

This commit is contained in:
Sheen Capadngan
2024-04-23 02:10:15 +08:00
parent 358ca3decd
commit 31a499c9cd
@@ -112,10 +112,11 @@ export const InfisicalSecretInput = forwardRef<HTMLTextAreaElement, Props>(
function setCaretPos(caretPos: number) { function setCaretPos(caretPos: number) {
if (childRef?.current) { if (childRef?.current) {
childRef.current.focus(); childRef.current.focus();
setTimeout(() => { const timeout = setTimeout(() => {
if (!childRef?.current) return; if (!childRef?.current) return;
childRef.current.selectionStart = caretPos; childRef.current.selectionStart = caretPos;
childRef.current.selectionEnd = caretPos; childRef.current.selectionEnd = caretPos;
clearTimeout(timeout);
}, 200); }, 200);
} }
} }
@@ -157,8 +158,9 @@ export const InfisicalSecretInput = forwardRef<HTMLTextAreaElement, Props>(
setCaretPos(currCaretPos); setCaretPos(currCaretPos);
if (event.currentTarget) { if (event.currentTarget) {
setTimeout(() => { const timeout = setTimeout(() => {
referencePopup(newValue, currCaretPos); referencePopup(newValue, currCaretPos);
clearTimeout(timeout);
}, 200); }, 200);
return; return;
@@ -243,11 +245,12 @@ export const InfisicalSecretInput = forwardRef<HTMLTextAreaElement, Props>(
// TODO: there should be a better way to do // TODO: there should be a better way to do
onChange?.({ target: { value: newValue } } as any); onChange?.({ target: { value: newValue } } as any);
setShowReferencePopup(type !== "secret"); setShowReferencePopup(type !== "secret");
setTimeout(() => { const timeout = setTimeout(() => {
setIsSecretFocused.on(); setIsSecretFocused.on();
if (type !== "secret") setReferenceKey(replaceReference); if (type !== "secret") setReferenceKey(replaceReference);
const caretPos = start.length + replaceReference.length + offset; const caretPos = start.length + replaceReference.length + offset;
setCaretPos(caretPos); setCaretPos(caretPos);
clearTimeout(timeout);
}, 100); }, 100);
} }
@@ -311,8 +314,9 @@ export const InfisicalSecretInput = forwardRef<HTMLTextAreaElement, Props>(
handleOpenChange={(isOpen) => handleReferenceOpenChange(isOpen)} handleOpenChange={(isOpen) => handleReferenceOpenChange(isOpen)}
onSelect={(refValue) => handleReferenceSelect(refValue)} onSelect={(refValue) => handleReferenceSelect(refValue)}
onEscapeKeyDown={() => { onEscapeKeyDown={() => {
setTimeout(() => { const timeout = setTimeout(() => {
setCaretPos(lastCaretPos); setCaretPos(lastCaretPos);
clearTimeout(timeout);
}, 200); }, 200);
}} }}
/> />
@@ -321,4 +325,4 @@ export const InfisicalSecretInput = forwardRef<HTMLTextAreaElement, Props>(
} }
); );
InfisicalSecretInput.displayName = "SecretInput"; InfisicalSecretInput.displayName = "InfisicalSecretInput";