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

form.watch doesn't work with arrays #6996

Open
1 of 2 tasks
jrozbicki opened this issue Oct 18, 2024 · 1 comment
Open
1 of 2 tasks

form.watch doesn't work with arrays #6996

jrozbicki opened this issue Oct 18, 2024 · 1 comment

Comments

@jrozbicki
Copy link
Contributor

Dependencies check up

  • I have verified that I use latest version of all @mantine/* packages

What version of @mantine/* packages do you have in package.json?

7.13.2

What package has an issue?

@mantine/form

What framework do you use?

Vite

In which browsers you can reproduce the issue?

All

Describe the bug

Shopping cart scenario - we have few items with prices, we can change amount of items in that shopping cart and we wanna display total value.

When we use "uncontrolled" form, we should use form.watch to track specific values.

Our products form field is an array [{ name: "phone", price: 1000, numberOfUnits: 1}].

When we use form.watch("products", console.log) nothing is happening.

image image

I found that previously it was mentioned as "not planned" #6280 (comment)

Currently if I want to track this, I had to use use-force-update hook and force rerender on onChange of numberOfUnits

I wanted to ask if we could revisit thinking about this issue or maybe there is different workaround to this.

If possible, include a link to a codesandbox with a minimal reproduction

No response

Possible fix

No response

Self-service

  • I would be willing to implement a fix for this issue
@yshterev
Copy link

yshterev commented Oct 30, 2024

@jrozbicki I worked around this by calling setFieldValue for the array fields after reset. This way form.watch('products', ... gets triggered. Maybe It is a good idea for watch docs to be updated

const handleFormReset = () => {
    form.setFieldValue('products', []); <-- call this first to get correct previousValue in watch callback
    form.reset();
  };

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants