mui / toolpad

Toolpad: Full stack components and low-code builder for dashboards and internal apps.
https://mui.com/toolpad/
MIT License
1.31k stars 295 forks source link

Binding editor doesn't accept object expressions #1543

Open Janpot opened 1 year ago

Janpot commented 1 year ago

Steps to reproduce 🕹

Happens when trying to bind to an object with more than one property. See:

  1. Open binding editor
  2. Type binding of {a:1, b:2 }
  3. Observe

Screenshot 2023-01-11 at 14 47 52

This happens because we're using Monaco editor to edit javascript expressions, but it's handling this input as a declaration, hence it see a block scope instead of an object.

The runtime side of things is addressed by https://github.com/mui/mui-toolpad/pull/1542

The problem can be worked around by adding parentheses around the expression

Possible solution

There doesn't seem to be language services that deal with javascript expressions, but one approach could be to create a variant of the editor that starts with the content:

1   return (
2    // type expression here
3   );

And disables line 1 and 3 from being edited, then strips lines 1 and 3 before firing onChange. (Some inspiration here: https://github.com/microsoft/monaco-editor/issues/953). Another direction could be setHiddenAreas. It seems to be removed from the types, but it's still functional https://github.com/microsoft/monaco-editor/issues/45

mnajdova commented 1 year ago

Upvoting, I expected this to work when binding the body with content-type: json. It worked with strings, arrays, so I kind of expected it to work with JSON too.