mui / toolpad

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

Add default value support to components #414

Closed oliviertassinari closed 2 years ago

oliviertassinari commented 2 years ago

Duplicates

Latest version

Summary 💡

Add the ability to pick the default value for a component

Examples 🌈

No response

Motivation 🔦

I would like the license key generator to default to a subscription mode. I'm using the Select component, in which it's impossible to implement.

Screenshot 2022-05-14 at 01 25 38
Janpot commented 2 years ago

It's currently possible (as a test I've set your license model to "perpetual" by default), but the UI currently doesn't update with it. The reason is that default values are set on the first render only. I'd consider this a bug.

oliviertassinari commented 2 years ago

It's currently possible (as a test I've set your license model to "perpetual" by default), but the UI currently doesn't update with it. The reason is that default values are set on the first render only. I'd consider this a bug.

Oh, ok I see. I wasn't expecting this. I have set the value as a workaround for now.


I have found a new use case for that default value with a TextField. I would like it to be:

new Date(new Date().getTime() + 1000 * 366 * 60 * 60 * 24).getTime()

based after https://github.com/mui/mui-x/blob/cc09f3788d65f9694a0fbc1381681c701b802e47/packages/x-license-pro/src/cli/license-cli.ts#L86. Without it, it's quite a painful UI to use, also since we don't have a date picker component, I will have to think of a different way to offer options.

oliviertassinari commented 2 years ago

I have landed again on this problem. It behaves strangely. Here is a different angle:

  1. Add a TextField on the page
  2. Set the value to "foo"
Screenshot 2022-08-06 at 19 29 10
  1. 💥 the value is not visible on the text field
  2. 💥 same in the binding view
Screenshot 2022-08-06 at 19 28 13
  1. Reload, now 4 and 5 have "foo" ✅
Janpot commented 2 years ago

It behaves strangely.

The reason it behaves like this is that default value is only used during the first render. When the default value is changed while the component is already being rendered, it doesn't have an effect. But I agree it's strange behavior, we should probably make an exception to it for when the value is changed by the user in the editor.

There may also be a benefit to having a separate defaultValue property.

oliviertassinari commented 2 years ago

The reason it behaves like this is that default value is only used during the first render.

+1 to try to experiment with remounting the component when the value changes. Effectively, to perform what a browser reload would do. If it's not enough, then defaultValue, might be needed.

oliviertassinari commented 2 years ago

I have found a new use case. I would like in https://master--toolpad.mui.com/_toolpad/app/cl4hla83p01949xoizo5uxf2a/editor/pages/cl4hla87800029xoihl7tf4qz to be able to use a page parameter. To be more precise, my use case is to be able to add ?order=121 in the URL of toolpad to prefill the order ID:

Screenshot 2022-08-13 at 00 52 59

It would be equivalent to executing:

npx @material-ui/x-license --order 121

However, it's not possible, I can't bind the URL parameter to the "value":

Screenshot 2022-08-12 at 23 56 01

It seems that I can't workaround the problem with a custom TextField either, it can't propagate the value up.

Janpot commented 2 years ago

I can't bind the URL parameter to the "value":

Are you looking for a way to two-way bind? as in, changes in the text editor should also be reflected in the url?

oliviertassinari commented 2 years ago

Are you looking for a way to two-way bind? as in, changes in the text editor should also be reflected in the URL?

No, one-way binding: URL -> Default value.

oliviertassinari commented 2 years ago

Awesome, a feature now in used for MUI X license key generator https://master--toolpad.mui.com/deploy/cl4hla83p01949xoizo5uxf2a/pages/cl4hla87800029xoihl7tf4qz?order=5225