adjustment: converted callback functions to use arrow notation

This commit is contained in:
Sheen Capadngan
2024-04-23 02:28:42 +08:00
parent 31a499c9cd
commit 6daeed68a0
@@ -94,7 +94,7 @@ export const InfisicalSecretInput = forwardRef<HTMLTextAreaElement, Props>(
const [referenceKey, setReferenceKey] = useState<string>(); const [referenceKey, setReferenceKey] = useState<string>();
const [lastCaretPos, setLastCaretPos] = useState<number>(0); const [lastCaretPos, setLastCaretPos] = useState<number>(0);
function isCaretInsideReference(str: string, start: number) { const isCaretInsideReference = (str: string, start: number) => {
const matches = [...str.matchAll(REGEX_SECRET_REFERENCE_FIND)]; const matches = [...str.matchAll(REGEX_SECRET_REFERENCE_FIND)];
for (let i = 0; i < matches.length; i += 1) { for (let i = 0; i < matches.length; i += 1) {
const match = matches[i]; const match = matches[i];
@@ -107,9 +107,9 @@ export const InfisicalSecretInput = forwardRef<HTMLTextAreaElement, Props>(
} }
} }
return null; return null;
} };
function setCaretPos(caretPos: number) { const setCaretPos = (caretPos: number) => {
if (childRef?.current) { if (childRef?.current) {
childRef.current.focus(); childRef.current.focus();
const timeout = setTimeout(() => { const timeout = setTimeout(() => {
@@ -119,26 +119,26 @@ export const InfisicalSecretInput = forwardRef<HTMLTextAreaElement, Props>(
clearTimeout(timeout); clearTimeout(timeout);
}, 200); }, 200);
} }
} };
function referencePopup(text: string, pos: number) { const referencePopup = (text: string, pos: number) => {
const match = isCaretInsideReference(text, pos); const match = isCaretInsideReference(text, pos);
if (match && typeof match.index !== "undefined") { if (match && typeof match.index !== "undefined") {
setLastCaretPos(pos); setLastCaretPos(pos);
setReferenceKey(match?.[2]); setReferenceKey(match?.[2]);
} }
setShowReferencePopup(!!match); setShowReferencePopup(!!match);
} };
function handleReferencePopup(element: HTMLTextAreaElement) { const handleReferencePopup = (element: HTMLTextAreaElement) => {
const { selectionStart, selectionEnd, value: text } = element; const { selectionStart, selectionEnd, value: text } = element;
if (selectionStart !== selectionEnd || selectionStart === 0) { if (selectionStart !== selectionEnd || selectionStart === 0) {
return; return;
} }
referencePopup(text, selectionStart); referencePopup(text, selectionStart);
} };
function handleKeyUp(event: React.KeyboardEvent<HTMLTextAreaElement>) { const handleKeyUp = (event: React.KeyboardEvent<HTMLTextAreaElement>) => {
if (event.key === "Escape") { if (event.key === "Escape") {
setShowReferencePopup(false); setShowReferencePopup(false);
return; return;
@@ -169,9 +169,9 @@ export const InfisicalSecretInput = forwardRef<HTMLTextAreaElement, Props>(
if (!(event.key.startsWith("Arrow") || event.key === "Backspace" || event.key === "Delete")) { if (!(event.key.startsWith("Arrow") || event.key === "Backspace" || event.key === "Delete")) {
handleReferencePopup(event.currentTarget); handleReferencePopup(event.currentTarget);
} }
} };
function handleKeyDown(event: React.KeyboardEvent<HTMLTextAreaElement>) { const handleKeyDown = (event: React.KeyboardEvent<HTMLTextAreaElement>) => {
if ( if (
!( !(
event.key.startsWith("Arrow") || event.key.startsWith("Arrow") ||
@@ -182,13 +182,13 @@ export const InfisicalSecretInput = forwardRef<HTMLTextAreaElement, Props>(
const match = isCaretInsideReference(value, event.currentTarget.selectionEnd); const match = isCaretInsideReference(value, event.currentTarget.selectionEnd);
if (match) event.preventDefault(); if (match) event.preventDefault();
} }
} };
function handleMouseClick(event: React.MouseEvent<HTMLTextAreaElement, MouseEvent>) { const handleMouseClick = (event: React.MouseEvent<HTMLTextAreaElement, MouseEvent>) => {
handleReferencePopup(event.currentTarget); handleReferencePopup(event.currentTarget);
} };
async function handleReferenceSelect({ const handleReferenceSelect = ({
name, name,
type, type,
slug slug
@@ -196,7 +196,7 @@ export const InfisicalSecretInput = forwardRef<HTMLTextAreaElement, Props>(
name: string; name: string;
type: "folder" | "secret" | "environment"; type: "folder" | "secret" | "environment";
slug?: string; slug?: string;
}) { }) => {
setShowReferencePopup(false); setShowReferencePopup(false);
// forward ref for parent component // forward ref for parent component
@@ -252,16 +252,16 @@ export const InfisicalSecretInput = forwardRef<HTMLTextAreaElement, Props>(
setCaretPos(caretPos); setCaretPos(caretPos);
clearTimeout(timeout); clearTimeout(timeout);
}, 100); }, 100);
} };
function handleChange(event: React.ChangeEvent<HTMLTextAreaElement>) { const handleChange = (event: React.ChangeEvent<HTMLTextAreaElement>) => {
setValue(event.target.value); setValue(event.target.value);
if (typeof onChange === "function") onChange(event); if (typeof onChange === "function") onChange(event);
} };
function handleReferenceOpenChange(currOpen: boolean) { const handleReferenceOpenChange = (currOpen: boolean) => {
if (!currOpen) setShowReferencePopup(false); if (!currOpen) setShowReferencePopup(false);
} };
return ( return (
<div <div