react-json-editor-ajrm icon indicating copy to clipboard operation
react-json-editor-ajrm copied to clipboard

Cursor jumps to beginning of text input

Open pascal-codetaal opened this issue 6 years ago • 9 comments

  1. What version of RJEA are you using (react-json-editor-ajrm version)? 2.5.8
  2. What operating system and processor architecture are you using? Mac os x
  3. What did you do? I created an instance of the json editor and the placeholder is attached a reactive value. I configured the json editor with the option reset={false} so no update should happen when the placeholder value updates.

I also created a codesandbox where you can see the problem, start editing the text and wait a little bit and the cursor will jump.

https://codesandbox.io/s/z6jmy6j8xl

  1. What did you expect to see? I would expect the cursor keep the same position.

  2. What did you see instead? The cursor jumps to the beginning of the input

pascal-codetaal avatar Oct 27 '18 20:10 pascal-codetaal

Hey @pascal-codetaal

Whenever the user stops editing the text the code will be reformatted after a certain amount of time without any further interaction. This "format" is what currently resets the cursor. I agree that this is not nice looking and rather unintuitive.

We'll look into this. Thank you for reporting this.

SachsKaylee avatar Oct 30 '18 15:10 SachsKaylee

@PatrickSachs do you have an idea how to fix this? And if so, perhaps i can help...

pascal-codetaal avatar Oct 30 '18 20:10 pascal-codetaal

Cursor jump is one of those things I have been wanting to fix for some time.

@pascal-codetaal, we are more than happy to have you involved. Feel free to submit a PR. Either way, I will also take a look at this over the weekend.

AndrewRedican avatar Nov 02 '18 08:11 AndrewRedican

@pascal-codetaal @AndrewRedican

I have the same annoying issue. If it's difficult to keep the cursor to the same position then I suggest a quicker solution:

  • on reformat event the cursor should loss of focus from the editor (blur/focus out event).

Because the main problem is while I'm typing and the cursor is reset then I continue writing at the beginning of the editor which causes json error :(

lybo avatar Jul 10 '19 08:07 lybo

Facing same issue. One notable piece of information is this doesn’t happen if there is no placeholder prop passed even after formatting the json

laxman3131 avatar Jun 23 '20 09:06 laxman3131

Yeah, this is a major issue for me as well.

CrizR avatar Oct 26 '20 15:10 CrizR

having the same problem. sometimes our JSON content is 700+ lines, reformatting for every keypress is really knocking down the performance of the application.

@lybo did you find any meaningful solution?

I couldn't understand whether everyone else is getting around this issue or perhaps it is not a big problem for them.

vejandla avatar Nov 22 '20 23:11 vejandla

An old thread I know.. but if anyone else stumbles across this looking for a solution then I've found the following to work (some option removed to keep it shorter, but essentially I am setting a 'keyPressed' boolean, a useEffect hook, and the 'viewOnly' option, which gets momentarily switched on and off. Not pretty, but it seems to work.

const Edit = ({ metadata, handleChange }) => {
	const [keyPressed, setKeyPressed] = useState(false);
	const [data, setData] = useState(false);

	useEffect(() => {
		handleChange(data);
		if (keyPressed) {
			setKeyPressed(false);
		}
	}, [keyPressed]);

	return (
		<Wrapper>
			<JSONInput
				id="editor"
				waitAfterKeyPress={0}
				placeholder={metadata}
				viewOnly={keyPressed}
				onChange={(data) => {
					setKeyPressed(true);
					setData(data);
				}}
			/>
		</Wrapper>
	);
};

alexxshadow avatar Jul 05 '21 12:07 alexxshadow

I too ran into this behavior. I ended up solving the issue by only providing the placeholder prop to the component when the component boots up, and not afterword.

Specifically:

                  <JSONInput
                    id = 'a_unique_id'
                    placeholder = { !query ? placeholder : null } // If the user has never typed anything and there is not query, set the placeholder, otherwise don't
                    locale = { locale }
                    onChange = { (q) => {
                      setQuery(q.jsObject); // When the user types, save their input to state to use elsewhere
                    }}
                  />

wdgrantham avatar Jul 14 '22 17:07 wdgrantham

I no longer intend to update this project. I am working instead on a complete rewrite.

I'd like to thank you for using and taking interest in this project.

I also would like to apologize for not following up sooner. However, do realize for the most part this has been a one-man show, and the occasional contributions I used to receive. What I am saying is, I am in dire need of volunteers.

If this is something you would be interested in participating in, you can join in the discussion.

I've taken note of this thread, and I'll keep this under consideration for the new project.

AndrewRedican avatar Jan 29 '23 03:01 AndrewRedican