Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 15 additions & 3 deletions src/components/CippComponents/CippTextFieldWithVariables.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,12 @@ export const CippTextFieldWithVariables = ({
// Track cursor position
const handleSelectionChange = () => {
if (textFieldRef.current) {
setCursorPosition(textFieldRef.current.selectionStart || 0);
// Check for input first, then textarea
const inputElement =
textFieldRef.current.querySelector("input") ||
textFieldRef.current.querySelector("textarea") ||
textFieldRef.current;
setCursorPosition(inputElement?.selectionStart || 0);
}
};

Expand Down Expand Up @@ -97,7 +102,11 @@ export const CippTextFieldWithVariables = ({
// Get fresh cursor position from the DOM
let cursorPos = cursorPosition;
if (textFieldRef.current) {
const inputElement = textFieldRef.current.querySelector("input") || textFieldRef.current;
// Check for input first, then textarea
const inputElement =
textFieldRef.current.querySelector("input") ||
textFieldRef.current.querySelector("textarea") ||
textFieldRef.current;
if (inputElement && typeof inputElement.selectionStart === "number") {
cursorPos = inputElement.selectionStart;
}
Expand Down Expand Up @@ -128,8 +137,11 @@ export const CippTextFieldWithVariables = ({
const newCursorPos = lastPercentIndex + variableString.length;

// Access the actual input element for Material-UI TextField
// Check for input first, then textarea
const inputElement =
textFieldRef.current.querySelector("input") || textFieldRef.current;
textFieldRef.current.querySelector("input") ||
textFieldRef.current.querySelector("textarea") ||
textFieldRef.current;
if (inputElement && inputElement.setSelectionRange) {
inputElement.setSelectionRange(newCursorPos, newCursorPos);
inputElement.focus();
Expand Down