Closed aitiqad closed 6 months ago
The text mode loses focus when the text contents change. Looking at your code I see you store the state as json. That will give issues since the contents of the text mode cannot always be represented as JSON. For example: [1,2,3
. Also, JSON loses any whitespacing like spaces or newlines, so if I change [1,2,3]
to [1, 2, 3]
in the editor and you parse it to JSON and then stringify it, the whitespacing is changed, which causes the editor to reset the cursor.
To solve this issue you will have to keep the original updatedContent
(which can hold either { json }
or { text }
) in your state.
Here is a React example: https://codesandbox.io/s/svelte-jsoneditor-react-59wxz
So how shall i keep the Updated content to {json} only? As per my requirement i need JSON only.
As per my requirement i need JSON only.
If the user types the following in the editor, what do you want to do?
[
1,
2,
3
If the user types these things, I want the Json Validation to work on and return an error.
Indeed, so your application should hold the following state: either valid json, or invalid json as text plus a parse error. In your code example, the state only holds json in a variable parsedContent
, but that is not enough.
Oh, I'm getting your point now. So if I pass
content={{json:example, text:undefined}}
It should work right?
Or shall I separately declare it?
You can just let the editor do it's thing and utilize it's Content
type, that can hold both parsed JSON and an unparsed text string (read the docs and examples about that).
Only when you want to save the document, you can check whether the content is valid JSON, and if not show the user an error. At that point you can read out either updatedContent.json
or JSON.parse(updatedContent.text)
and store that in your database.
brother, I'm very new to react, so it would be really helpful if you tell me exactly where shall i make changes on my code. Please
Sure, you can check out the following React example: https://codesandbox.io/s/svelte-jsoneditor-react-59wxz
I took reference from here only. My jsonInputValue, jsonPropertiesValue and jsonDesiredPropertiesValue are already declared useStates. whose data (JSON) are being fetched from an API. while in the React Example I found that the data has to be in a certain format that,
{
json: {
example data
example data
..
..
..
example data
},
text: undefined
}
I'm not sure what you mean, can you explain?
Closing due to inactivity, please reopen if needed.
URGENT HELP REQUIRED
I don't know why but, whenever I am in the text mode and try to edit my JSON the text cursor ( | ) is automatically going to the first line, after a single edit.
this is how my component looks like
and this is how im using it in my parent component code
I've used this component thrice for three events (jsonInputValue, jsonPropertiesValue, jsonDesiredPropertiesValue)