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

Inserting new item in field list clears the values of the previous list item #707

Open
joejemmely opened this issue Jul 14, 2024 · 1 comment
Labels
bug Something isn't working

Comments

@joejemmely
Copy link

joejemmely commented Jul 14, 2024

Describe the bug and the expected behavior

Inserting a new item in field list clears the values of the previous list item.

To illustrate the bug, here's an example of a simple UI for adding variables to multiple environments.

Using a deeply nested schema like:

const schema = z.object({
  environments: z.array(
    z.array(
      z.object({
        name: z.string(),
        description: z.string(),
        value: z.string(),
      })
    )
  ),
});

Inserting a new environment clears the values of the previous environment when defining defaultValue as follow:

<button
  {...form.insert.getButtonProps({
    name: fields.environments.name,
    defaultValue: [{ name: "", description: "", value: "" }],
    )}
>
  Add environment
</button>

The same thing happens when inserting a new variable within an environment.

The expected behavior is that values previously entered are persisted when a new list item is inserted.

Conform version

v1.1.5

Steps to Reproduce the Bug or Issue

Sandbox: https://stackblitz.com/edit/remix-run-remix-7ybrqe?file=app%2Froutes%2F_index.tsx

  1. Go to https://stackblitz.com/edit/remix-run-remix-7ybrqe?file=app%2Froutes%2F_index.tsx
  2. Add a an environment by clicking on Add environment
  3. Fill in values for name, description, and value for the newly created environment
  4. Add a new environment by clicking on Add environment
  5. Values from step 3 have been cleared. The expected behavior is that values from step 3 are persisted.

Alternatively:

  1. Go to https://stackblitz.com/edit/remix-run-remix-7ybrqe?file=app%2Froutes%2F_index.tsx
  2. Add a an environment by clicking on Add environment
  3. Fill in values for name, description, and value for the newly created environment
  4. Add a new variable by clicking on Add variable
  5. Values from step 3 have been cleared. The expected behavior is that values from step 3 are persisted.

What browsers are you seeing the problem on?

Chrome, Firefox, Microsoft Edge, Safari, Others

Screenshots or Videos

No response

Additional context

No response

@marcomuser
Copy link
Contributor

Might have something to do with your nested array data structure. I would have expected it to have a nested object for the variables. Something like this:

const schema = z.object({
  environments: z.array(
    z.object({
      variables: z.array(
        z.object({
          name: z.string(),
          description: z.string(),
          value: z.string(),
        })
      ),
    })
  ),
});

Not sure if that is the issue but I have a suspicion that this is why it is not able to keep track of the keys in your form when inserting a new entry.

@edmundhung edmundhung added the bug Something isn't working label Aug 12, 2024
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

3 participants