In my code, dropdown shown when user type $ symbol, this is working fine, I wanted to when user gave $ symbol again that dropdown should be set at last position of curser but now dropdown is moving whatever we have clicked to text field, so how to fix that the dropdown should move relative to the cursor on the current text only?
This is fine but whenever we have type $ symbol at the first position dropdown should be move from last to initial position but its not working, so how to deal with that?
import { useState, useRef } from "react";
export default function App() {
const [value, setValue] = useState("");
const [show, setShow] = useState(false);
const [cursor, setCursor] = useState(0);
const ref = useRef(null);
const foo = (e) => {
setValue(e.target.value);
if (e.nativeEvent.data === "$") {
setShow(true);
} else {
setShow(false);
}
setCursor(Math.min(e.target.value?.length, 22) * 8);
};
const boo = (e) => {
console.log(e.clientX);
setCursor(e.clientX);
};
return (
<div className="App">
<input value={value} onChange={foo} ref={ref} onKeyDown={boo} />
{show && (
<div
style={{
width: "210px",
height: "210px",
background: "pink",
marginLeft: cursor + "px",
position: "absolute",
left: "2px",
}}
>
-DropDown-
</div>
)}
</div>
);
}