mantinedev / mantine

A fully featured React components library
https://mantine.dev
MIT License
26.87k stars 1.9k forks source link

Type inference issue in deeply nested form validation #4735

Closed sammiya closed 1 year ago

sammiya commented 1 year ago

What package has an issue

@mantine/form

Describe the bug

In the following code, during the validation function for users.tags.tag, the values parameter should contain data of type FormValues. However, it's incorrectly inferred as { tag: string; key: string; }.

import { randomId } from "@mantine/hooks";
import { useForm } from "@mantine/form";

type FormValues = {
  users: {
    name: string;
    key: string;
    tags: {
      tag: string;
      key: string;
    }[];
  }[];
};

export default function App() {
  const initialValues: FormValues = {
    users: [
      {
        name: "Dan Abramov",
        key: randomId(),
        tags: [{ tag: "React", key: randomId() }],
      },
    ],
  };

  const form = useForm({
    initialValues,
    validate: {
      users: {
        name: (value, values, path) => {
          console.log("validate users.name", { value, values, path });
          if (value === "") return "Name is required";
          return null;
        },
        tags: {
          tag: (value, values, path) => {
            // Here, the 'values' actually contains a value of type FormValues, but for some reason, it's inferred as { tag: string; key: string; }
            console.log("validate users.tags.tag", { value, values, path });
            if (value === "") return "Tag is required";

            return null;
          },
        },
      },
    },
  });

  return <>{/* Omitted for brevity */}</>;
}

What version of @mantine/hooks page do you have in package.json?

6.0.20

If possible, please include a link to a codesandbox with the reproduced problem

https://codesandbox.io/s/nostalgic-wood-9dnyr6?file=/src/App.tsx

Do you know how to fix the issue

None

Are you willing to participate in fixing this issue and create a pull request with the fix

None

Possible fix

No response

rtivital commented 1 year ago

The issue is resolved in 7.0.1