Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

expand all fields in tree mode in ReactJS #266

Closed
nitzcard opened this issue May 1, 2023 · 15 comments
Closed

expand all fields in tree mode in ReactJS #266

nitzcard opened this issue May 1, 2023 · 15 comments

Comments

@nitzcard
Copy link

nitzcard commented May 1, 2023

Hi, I try to expend all the fields in tree mode in react right from the start,
I saw there is an expand function:
refEditor.current?.expand((path) => true);

I try to do it in the useEffect when initting the library but it doesn't work,
do you have an idea how it can be achieved?

@nitzcard nitzcard changed the title expand all fields in ReactJS expand all fields in tree mode in ReactJS May 1, 2023
@nitzcard
Copy link
Author

nitzcard commented May 1, 2023

#267

@josdejong
Copy link
Owner

Thanks for your PR @nitzcard . I'm not entirely sure I understand the original issue: at the start, the document contents is expanded by default. See for example this basic React example: https://codesandbox.io/s/svelte-jsoneditor-react-59wxz

@nitzcard
Copy link
Author

nitzcard commented May 2, 2023

At my internal system it doesn't start expanded all the times,
I think it has to do with getDefaultExpand()

@josdejong
Copy link
Owner

Can you share a minimal example demonstrating your issue?

The only case where I know it happens is if you initially load an empty document, and later update it with actual contents. The editor leaves the expanded state as is when updating a document. When I load a new document, I normally create a new editor with clean state, so it expands the document contents like it should.

@nitzcard
Copy link
Author

nitzcard commented May 2, 2023

Could be that's what happens at my form infra internally, when the data loads after. Will check and return to you.

If that is the case, how can I enforce expanded at the first time the actual data loads?

@josdejong
Copy link
Owner

If that is the case, how can I enforce expanded at the first time the actual data loads?

Either create a new editor with the document, or use .set(...) initially instead of .update(...).

@nitzcard
Copy link
Author

nitzcard commented May 2, 2023

Because my real content may come not at init time, I still need to set the editor to be at expanded state right from the start, so I need to intervene in getDefaultExpand() (that it will return true even if no content is available).
I tried to achieve a workaround with smth like this, but it's not working:
image

So I believe my pr can help with this

@josdejong
Copy link
Owner

I think what should work is:

  • do not pass content via refEditor.current.updateProps, remove it from the props before passing
  • when loading the document initially, use refEditor.current.set(content) (that will reset the state of the editor)
  • when loading the document consecutively, use refEditor.current.update(content) (that will keep the state of the editor)

Alternatively, you can create the editor only after you have the document and can directly pass it during initialization of the editor.

@nitzcard
Copy link
Author

nitzcard commented May 4, 2023

  1. Waiting for the editor to load when the content loads is not doable for me, because sometimes there is no content and I would not know that (and I want the instruction message when text: ''), so I need a different solution.
  2. The set/update thing you suggested sounds to me like a hack, I will try it nevertheless, but I advocate for a more coherent solution as I suggested.
  3. I think giving the control about expand/collapse is good, what if user wants the documents to starts collapsed/expanded regardless the content?

@josdejong
Copy link
Owner

One way or another, you will need to tell the editor when to keep the expanded state or reset it. Only you know the difference between loading a new document versus updating the existing document. The startExpanded option in the PR you propose does not solve that issue. What could be useful is introducing an option expand, but that would still not address your issue.

You will need to let the editor know when to reset the state of the editor by either (1) creating a new editor, (2) use the method .set() instead of .update(), or (3) invoke the method .expand() after loading your document. I think the current API is sufficient for that.

On a side note: I was thinking that maybe your code in #266 (comment) does not work because you call .expand without awaiting updateProps to rerender the app, you would have to call it like:

await refEditor.current.updateProps(props)
//...
if (...) {
  await refEditor.current.expand(...)
}

@josdejong
Copy link
Owner

Good to mention here: we're thinking about exposing the internal state of the editor so you can control that via props, see #163 (comment). That would introduce another option to change/reset the expanded state whenever you want.

@nitzcard
Copy link
Author

nitzcard commented May 4, 2023

using await inside useEffect is discouraged, but it's not working.
I think my only solution right now is how you suggested: to use "set" only on initial content loading and after that use "update",
I'll update if works good :)

@nitzcard
Copy link
Author

nitzcard commented May 4, 2023

ok so implementing it for me via set is still very hacky with a lot of edge cases,
I would still be glad for an option to enforce the state of the editor to be expanded even without content, via prop of startExpanded or other option, without altering editor state and ruining the expanded state

@josdejong
Copy link
Owner

I'm still not sure whether I correctly understand what you're trying to do.

Can you create a minimal CodeSandbox example demonstrating the issue? Can be based on https://codesandbox.io/s/svelte-jsoneditor-react-59wxz

@josdejong
Copy link
Owner

Any more information on this @nitzcard ? If not relevant anymore I'll close this issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants