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

[React] Reset doesn't work with raw input #241

Open
muhajirdev opened this issue Aug 23, 2024 · 5 comments
Open

[React] Reset doesn't work with raw input #241

muhajirdev opened this issue Aug 23, 2024 · 5 comments
Assignees
Labels
question Further information is requested

Comments

@muhajirdev
Copy link

muhajirdev commented Aug 23, 2024

export function Sampleform() {
  // Use login form
  const [loginForm, { Form, Field }] = useForm<LoginForm>();

  return (
    <Form
      className="space-y-12 md:space-y-14 lg:space-y-16"
      onSubmit={(values) => console.log(values)}
    >
      <Field name="email">
        {(field, props) => <input className="border" {...props} type="email" />}
      </Field>
      <Field name="email">
        {(field, props) => (
          <TextInput
            {...props}
            value={field.value}
            error={field.error}
            type="email"
            label="Email"
            placeholder="[email protected]"
            required
          />
        )}
      </Field>
      <Field name="password">
        {(field, props) => (
          <input {...props} className="border" type="password" />
        )}
      </Field>
      <button onClick={() => reset(loginForm)}>reset</button>
    </Form>
  );
}

This

        {(field, props) => <input className="border" {...props} type="email" />}

Doesn't work

But TextInput does. The code is from react playground https://stackblitz.com/edit/modular-forms-react-qqpshg?file=src/routes/login.tsx,src/components/TextInput.tsx

@muhajirdev
Copy link
Author

muhajirdev commented Aug 23, 2024

Originally I was trying to work with ArrayFields, trying to insert. But found out that the library doesn't work well with react.

I think there should be "Gotchas" documented in the docs

@fabian-hiller
Copy link
Owner

fabian-hiller commented Aug 23, 2024

What exactly does not work with React? I think this is fixed. I think the problem is that your field is not controlled: https://modularforms.dev/react/guides/controlled-fields

@fabian-hiller fabian-hiller self-assigned this Aug 23, 2024
@fabian-hiller fabian-hiller added the question Further information is requested label Aug 23, 2024
@muhajirdev
Copy link
Author

Ahh, i see, thank you @fabian-hiller

Then I think it would be nice if it's mentioned here https://modularforms.dev/react/guides/add-fields-to-form

@muhajirdev
Copy link
Author

muhajirdev commented Aug 24, 2024

      <FieldArray name="test">
        {(fieldArray) => (
          <div>
            {fieldArray.items.value.map((item, index) => {
              return (
                <div key={item}>
                  <Field name={`test.${index}`}>
                    {(field, props) => (
                      <div>
                        <Input {...props} value={field.value.value} />
                        {field.error && <FieldError>{field.error}</FieldError>}
                      </div>
                    )}
                  </Field>
                </div>
              );
            })}
            <Button
              type="button"
              onClick={() => insert(profileForm, "test", { value: "aa" })}
            >
              Add item
            </Button>
          </div>
        )}
      </FieldArray>

This also seems to be not reactive, how do I make it reactive. It does nothing when I insert

@fabian-hiller
Copy link
Owner

I need a minimal StackBlitz reproduction. Feel free to use our template. There is also a field array example included: https://stackblitz.com/edit/modular-forms-react?file=src%2Froutes%2Ftodos.tsx

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

No branches or pull requests

2 participants