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

ref not working correctly with react-hook-form #129

Open
Git-Hub-User-0 opened this issue Feb 19, 2024 · 0 comments
Open

ref not working correctly with react-hook-form #129

Git-Hub-User-0 opened this issue Feb 19, 2024 · 0 comments

Comments

@Git-Hub-User-0
Copy link

Git-Hub-User-0 commented Feb 19, 2024

In the below code, when I call handleSubmit it throws e.focus not a function error.

import React from "react";
import { useForm, Controller } from "react-hook-form";
import CurrencyInput from "react-currency-input";

export default function App() {
  const {
    handleSubmit,
    control,
    reset,
    formState: { errors },
  } = useForm({
    defaultValues: {
      currencyInput: "",
    },
    resolver: (values) => {
      const _errors = {};
      console.log(Number(values.currencyInput));
      if (Number(values.currencyInput) <= 0) {
        _errors.currencyInput = { message: "Required" };
      }
      return { errors: _errors };
    },
  });
  const onSubmit = (data) => console.log(data);
  const handleOnChange = (data) => console.log("onChange: ", data);
  const ref = React.createRef();
  const value = ref.current?.value;
  console.log(errors);

  return (
    <form className="App" onSubmit={handleSubmit(onSubmit)}>
      <Controller
        name="currencyInput"
        control={control}
        rules={{ required: true }}
        render={({ field }) => (
          <>
            <CurrencyInput
              onChange={handleOnChange(field.onChange)}
              ref={field.ref}
              value={field.value}
            />
            <p style={{ color: "red" }}>{errors?.currencyInput?.message}</p>
          </>
        )}
      />
      <input type="submit" />
    </form>
  );
}

This works perfectly with html inputs, but not with this library?
Can anyone please help with this urgently?
@jsharpe @jsillitoe @sevos @panupan @zhuangya

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

1 participant