BearStudio / formiz

šŸœ React forms with ease! Composable, headless & with built-in multi steps
https://formiz-react.com/
MIT License
390 stars 50 forks source link

Async initialValues didn't work with useCollection #188

Closed hugo-lovighi closed 1 year ago

hugo-lovighi commented 1 year ago

Hello, I'm testing Formiz v2 beta and I have some issues with useCollection hook.

Issue

It works fine with static data like:

const form = useForm({
    onValidSubmit(values, form) {
        console.log(values)
    },
    initialValues: {
        members: [
            { name: 'user1', email: 'user1@gmail.com' },
            { name: 'user2', email: 'user2@gmail.com' },
        ],
    },
})

const collection = useCollection({ connect: form, name: 'members' })

Thats ok, i got 2 items in collection.keys

But it doesn't work when I fetch data:

const { data, isLoading, isFetched } = useGroupQuery({ id: 1 })

const form = useForm({
    onValidSubmit(values, form) {
       console.log(values)
    },
    ready: isFetched,
    initialValues: data,
})

const collection = useCollection({ connect: form, name: 'members' })

collection.keys is empty

My returned data object looks like:

{
    group: { id: 1, name: 'group1' },
    members: [
        { name: 'user1', email: 'user1@gmail.com' },
        { name: 'user2', email: 'user2@gmail.com' },
    ],
}

I've seen that you remove the initialValues params from useCollection()

Did i missed something or Am i doing something wrong ?

HugoPerard commented 1 year ago

Yes you're right there is an issue, I just look at it and find a fix. I will release a new beta version with the fix. Thanks for your feedback, don't hesitate if you have other issues.

HugoPerard commented 1 year ago

I just release @formiz/core 2.0.0-beta.9 version, that should solves your problem šŸ˜„