pablo-abc / felte

An extensible form library for Svelte, Solid and React
https://felte.dev
MIT License
1.01k stars 44 forks source link

Felte does not assign initial values to custom controls #202

Open bogie67 opened 1 year ago

bogie67 commented 1 year ago

Describe the bug

First of all, thank you for this great library and also sorry for my bad english...

Anyway, you can check this, using the REPL https://svelte.dev/repl/65f12cbc18cb46e2bd2a49e325dc1e34?version=3.47.0 and changing line 10 of App.svelte from

foo: null,

to

foo: "bar",

and see that the custom control is not initialized to the value "bar".

Which package/s are you using?

felte (Svelte)

Environment

To reproduce

No response

Small reproduction example

No response

Screenshots

No response

Additional context

No response

bogie67 commented 1 year ago

@pablo-abc any news? Do you agree with this bug, or is it my mistake

pablo-abc commented 1 year ago

I'd call this a bug, yes. And a bit of an oversight on my part. For the moment the only way to set a default value would be by setting it as a prop and passing it to the 'defaultValue' option of 'createField'.

Thanks for the report! And I really appreciate you using Felte!

bogie67 commented 1 year ago

Hi @pablo-abc ,

thanks for the reply.

I'm sorry but it seems that the solution you proposed does not solve the problem....

Let me explain: the store (and the hidden field of the control) are set correctly but the value is not shown in the element.

For example, in the "Using createField" paragraph of the "Custom Control" document, when the component is mounted, the editable div content is not set with initialValues (nor with defaultValue). The value (in the DOM) is set only when the user types (and, given the code, it couldn't be otherwise....).

I could certainly pass a value as a prop, but that way I'd lose the beauty of using custom controls like traditional fields, which just need the name to be filled with store values (like what you indicate in the "Nested Form").

Wouldn't it be possible to pass a function to createField, type onInitField which is called from the library passing the value at the time of field valorisation? This way you can create custom controls however complex...

I managed to modify the createField file like this and created a Mutation Observer which monitors the change of the "value" attribute and calls the function passed in the constructor. It works but it doesn't look too clean to me...... Do you have any suggestions?

Thank you for your attention and for the excellent work done