ciscoheat / sveltekit-superforms

Making SvelteKit forms a pleasure to use!
https://superforms.rocks
MIT License
2.03k stars 60 forks source link

Usability problem of an array of superforms #396

Open unlocomqx opened 3 months ago

unlocomqx commented 3 months ago

Description I tried retuning multiple superforms in an array, but I could not use it in the frontend. The only convenient way was to destructure each superform in the each tag but svelte doesn't support "declaring" stores out of the top-level of the component

If applicable, a MRE https://www.sveltelab.dev/9tdm9ul193m3tz5

unlocomqx commented 3 months ago

It didn't occur to me to extract the form into a separate component 🤦 So this would work

{#each superforms as superform}
    <Comp {superform} />
{/each}

Closing.

unlocomqx commented 3 months ago

I'm reopening this issue because I encountered a problem

When I write this code

<script>
const superforms = data.my_forms.map((form) =>
    superForm(form, {
        dataType: 'json'
    })
);
</script>
{#each superforms as superform (superform.formId)}
    <ConditionItem {superform} />
{/each}

It works well but when a new form is added, then that's where I run into a problem

If I try to use $derived

const superforms = $derived(
    data.condition_forms.map((form) =>
        superForm(form, {
            dataType: 'json'
        })
    )
);

I get this error

Uncaught Error: onDestroy can only be used during component initialisation.

Repro https://www.sveltelab.dev/9tdm9ul193m3tz5

unlocomqx commented 3 months ago

I found a way to fix it, I pushed my code here

If you'd like, I can make a PR for this

Cheers

ciscoheat commented 3 months ago

A PR for this would be very nice!

unlocomqx commented 3 months ago

Okay, I will send a PR for this

ciscoheat commented 2 months ago

The problem is bigger than I anticipated, it'll require an overhaul of the onDestroy/cleanup handling. It'll have to wait until later. For now, using calls to superForm within $derived cannot be used.

unlocomqx commented 2 months ago

Okay, it will be good to have dynamic forms in the future. I will try to find a workaround for now.