Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

svelte / setFields not working #226

Open
tiagoapp opened this issue Mar 4, 2023 · 6 comments
Open

svelte / setFields not working #226

tiagoapp opened this issue Mar 4, 2023 · 6 comments
Assignees
Labels
bug Something isn't working

Comments

@tiagoapp
Copy link

tiagoapp commented Mar 4, 2023

Describe the bug

Essentially I'm having troubles get the data back in the inputs.

const { form, data, setFields, reset, setData, setInitialValues, handleSubmit } = createForm({
initialValues: {
     name: '',
}
})

const onClickEdit = async (id: string) => {

	const { data, error } = await categories.find(id);

        // this doesn't change the data on the input
	setData({
		name: data?.name,
	});

        // this doesn't change the data on the input
       setFields('name', data?.name, true)

       // this does work and I get data on inputs, but can't use the reset() afterwards to get back to blank state
       setInitialValues({
		name: data?.name,
	});
}

Basically trying to get the field to load the data on editing

<form class="flex flex-col" use:form>
	<label for="name">Name</label>
        <input type="text" name="name" />
</form>

Which package/s are you using?

felte (Svelte)

@tiagoapp tiagoapp added the bug Something isn't working label Mar 4, 2023
@pablo-abc
Copy link
Owner

Hey! Do you have a REPL reproduction? This works for me in the REPL: https://svelte.dev/repl/bb45eaf5b54745d98ea074167e278bdd?version=3.55.1

What are you trying to do?

@canastro
Copy link

@pablo-abc here is a modified version of something I'm trying to do with felte's setFields and havent been able to do it gracefully.

https://svelte.dev/repl/284ff1438c6a4990ac370b2db27efaee?version=3.55.1

To workaround this I had to call setFields for both inputs.

@aryzing
Copy link

aryzing commented Oct 19, 2023

Seems setFields is also broken for Solid. Not sure whether worth opening a separate issue since all libs use the same core functionality?

@aryzing
Copy link

aryzing commented Oct 19, 2023

Seems the functionality is broken when the form isn't yet rendered. Attempting to set the fields of a form that's not yet connected to the DOM will throw no errors, but have no effect.

It may be necessary to create the form before connecting it to the dom, for example, to pass to other components or to prefill it with data before rendering it to the user.

@pablo-abc
Copy link
Owner

That is correct! A form needs to exist and be connected before anything else happens.

This has been on my head for a while and it seems I just found why this is happening. The original issue seems to be due to a race condition. Both felte itself and the code in the example are waiting for input at around the same time. If you force the input handler to wait a bit (e.g. setTimeout), or, more cleanly use a $: to guarantee the value has already been updated internally it works.

For this specific example, using $: setFields('test', $data.name?.toUpperCase()); instead of the handleChange function works as expected.

Checking if there's a way to handle this scenario is taking me a while so apologies for that....

@aryzing
Copy link

aryzing commented Oct 19, 2023

A form needs to exist and be connected before anything else happens.

Thanks for clarifying. How does the lack of DOM elements prevent the state from being updated? Couldn't the fields read their values from the store once they're connected to the DOM?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

4 participants