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

useForm setData not working as expected #1545

Closed
moham96 opened this issue May 9, 2023 · 2 comments
Closed

useForm setData not working as expected #1545

moham96 opened this issue May 9, 2023 · 2 comments
Labels
react Related to the react adapter

Comments

@moham96
Copy link

moham96 commented May 9, 2023

Version:

  • @inertiajs/react version: 1.0.2

Describe the problem:

Below is an example for the bug/issue,in theory when I change the input1 select to some value other than 0 it should reset the input2 to 0 but what I'm seeing is that it clears the changed input itself input1

Steps to reproduce:

1- run this example using inertiajs/react

import { useForm } from "@inertiajs/react";
const options = [
    { title: "zero", value: 0 },
    { title: "one", value: 1 },
];
function Text() {
    const { setData, data, reset } = useForm<{
        input1: number;
        input2: number;
        input3: number;
    }>({
        input1: 0,
        input2: 0,
        input3: 0,
    });

    console.log({ data });
    return (
        <div>
            <form>
                <div>
                    <label htmlFor="input1">Input1</label>
                    <select
                        id="input1"
                        onChange={(e) => {
                            setData("input1", parseInt(e.target.value));
                            if (parseInt(e.target.value) !== 0) {
                                console.log(
                                    `input1 is set so  reset input2 to 0`
                                );
                                setTimeout(() => {
                                    setData("input2", 0);
                                }, 1000);
                            }
                        }}
                        value={data.input1}
                    >
                        {options.map((option, index) => (
                            <option key={index} value={option.value}>
                                {option.title}
                            </option>
                        ))}
                    </select>
                </div>
                <div>
                    <label htmlFor="input2">Input2</label>
                    <select
                        id="input2"
                        onChange={(e) => {
                            setData("input2", parseInt(e.target.value));
                            if (parseInt(e.target.value) !== 0) {
                                console.log(
                                    `input2 is set so  reset input1 to 0`
                                );

                                setTimeout(() => {
                                    reset("input1");
                                }, 1000);
                            }
                        }}
                        value={data.input2}
                    >
                        {options.map((option, index) => (
                            <option key={index} value={option.value}>
                                {option.title}
                            </option>
                        ))}
                    </select>
                </div>
                <div>
                    <label htmlFor="input3">Input3</label>
                    <select
                        id="input3"
                        onChange={(e) => {
                            setData("input3", parseInt(e.target.value));
                        }}
                        value={data.input3}
                    >
                        {options.map((option, index) => (
                            <option key={index} value={option.value}>
                                {option.title}
                            </option>
                        ))}
                    </select>
                </div>
            </form>
        </div>
    );
}
export default Text;

2- in the browser change the select of input1 to one you will notice that it does set the input to 1 but after the timeout that I have 1000ms it will reset the input itself and not the input2 as it's supposed to do

Is there a bug in my code or in inertiajs?

@moham96 moham96 added the react Related to the react adapter label May 9, 2023
@moham96
Copy link
Author

moham96 commented May 21, 2023

This might be similar/related to #1086

@reinink
Copy link
Member

reinink commented Jul 28, 2023

Hey! Thanks so much for your interest in Inertia.js and for sharing this issue/suggestion.

In an attempt to get on top of the issues and pull requests on this project I am going through all the older issues and PRs and closing them, as there's a decent chance that they have since been resolved or are simply not relevant any longer. My hope is that with a "clean slate" me and the other project maintainers will be able to better keep on top of issues and PRs moving forward.

Of course there's a chance that this issue is still relevant, and if that's the case feel free to simply submit a new issue. The only thing I ask is that you please include a super minimal reproduction of the issue as a Git repo. This makes it much easier for us to reproduce things on our end and ultimately fix it.

Really not trying to be dismissive here, I just need to find a way to get this project back into a state that I am able to maintain it. Hope that makes sense! ❤️

@reinink reinink closed this as completed Jul 28, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
react Related to the react adapter
Projects
None yet
Development

No branches or pull requests

2 participants