Coder Social home page Coder Social logo

Comments (26)

 avatar commented on May 5, 2024 1

No, if the value is not an array still not works. However, I change array with function provided by useState. So it should understand the change.

from react-hook-form.

bluebill1049 avatar bluebill1049 commented on May 5, 2024

value is array may be the issue, cause it does shallow compare with value

can you share bit more code or a codesandbox would really help?

from react-hook-form.

bluebill1049 avatar bluebill1049 commented on May 5, 2024

oh ok, this is an example which I did for custom input. hopefully, it will help you abit

https://codesandbox.io/s/72j69vnk1x

from react-hook-form.

 avatar commented on May 5, 2024

Thanks. But, my component is not a html form component. It has four inputs and the final value should be sum of all inputs.

from react-hook-form.

bluebill1049 avatar bluebill1049 commented on May 5, 2024

Oh ok.

Sounds like your four inputs should be one Component and register as one field, and use setValue to update value once it's finished the calculation.

share your code bit more, see if I can assist :)

from react-hook-form.

 avatar commented on May 5, 2024

this is my custom component:

import React, {
  useState,
  useRef,
  forwardRef,
  useImperativeHandle
} from "react";
import styles from "./splitInput.module.scss";

const SplitInput = forwardRef((props: Props, ref: any) => {
  const [value, setValue] = useState([...Array(props.length)]);
  const elRef = useRef([...Array(props.length)].map(() => React.createRef()));

  useImperativeHandle(
    ref,
    () => {
      return {
        type: "text",
        name: props.name,
        value: value.join("")
      };
    },
    [value]
  );

  const onInputChange = (e, i) => {
    e.persist();
    if (e.target) {
      setValue(value => {
        value[i] = e.target.value;
        return value;
      });
      if (i < props.length - 1 && e.target.value !== "") {
        elRef.current[i + 1].current.focus();
      }

      props.onChange(value.join(""));
    }
  };

  const onKeyDown = (e, i) => {
    e.persist();
    if (i > 0 && e.key === "Backspace") {
      e.preventDefault();
      var input = elRef.current[i].current;

      input.value = "";
      input = elRef.current[i - 1].current;

      input.focus();
    }
  };

  const renderInputs = () => {
    let rows = [];
    for (let i = 0; i < props.length; i++) {
      rows.push(
        <input
          ref={elRef.current[i]}
          key={i}
          type="number"
          autoFocus={i == 0}
          onChange={e => onInputChange(e, i)}
          onKeyDown={e => onKeyDown(e, i)}
          className={styles.verficationCode}
        />
      );
    }
    return rows;
  };
  return <div className={styles.verficationCodeBox}>{renderInputs()}</div>;
});
SplitInput.defaultProps = {
  length: 4
};
export default SplitInput;

from react-hook-form.

bluebill1049 avatar bluebill1049 commented on May 5, 2024
import React, {
  useState,
  useRef,
  forwardRef,
  useImperativeHandle,
  useEffect,
} from "react";
import styles from "./splitInput.module.scss";

const SplitInput = forwardRef((props: Props, ref: any) => {
  const [value, setValue] = useState([...Array(props.length)]);
  const elRef = useRef([...Array(props.length)].map(() => React.createRef()));

  useEffect(() => {
    // register a filed in react hook form
    props.regsiter({
      name: 'total'
    })
  }, []);

  useEffect(() => {
    // set field value when value is changing
    props.setValue('total', value.join(""))
  }, [value]);

  const onInputChange = (e, i) => {
    e.persist();
    if (e.target) {
      setValue(value => {
        value[i] = e.target.value;
        return value;
      });
      if (i < props.length - 1 && e.target.value !== "") {
        elRef.current[i + 1].current.focus();
      }

      props.onChange(value.join(""));
    }
  };

  const onKeyDown = (e, i) => {
    e.persist();
    if (i > 0 && e.key === "Backspace") {
      e.preventDefault();
      var input = elRef.current[i].current;

      input.value = "";
      input = elRef.current[i - 1].current;

      input.focus();
    }
  };

  const renderInputs = () => {
    let rows = [];
    for (let i = 0; i < props.length; i++) {
      rows.push(
        <input
          ref={elRef.current[i]}
          key={i}
          type="number"
          autoFocus={i == 0}
          onChange={e => onInputChange(e, i)}
          onKeyDown={e => onKeyDown(e, i)}
          className={styles.verficationCode}
        />
      );
    }
    return rows;
  };
  return <div className={styles.verficationCodeBox}>{renderInputs()}</div>;
});
SplitInput.defaultProps = {
  length: 4
};
export default SplitInput;

what about this?

from react-hook-form.

 avatar commented on May 5, 2024

It registers the field as the methods I did before. But, changing the value does not change the validation errors. And I cannot submit the form.

from react-hook-form.

bluebill1049 avatar bluebill1049 commented on May 5, 2024

You can use ‘setError(‘total’)’ to remove the error if value is valid .

from react-hook-form.

 avatar commented on May 5, 2024

It adds an error like this to error list:
phone: {type: undefined, message: undefined, ref: undefined, isManual: true}

I have also another problem. I see an error in console:

index.es.js:364 Uncaught TypeError: Failed to execute 'contains' on 'Node': parameter 1 is not of type 'Node'.
at findRemovedFieldAndRemoveListener (index.es.js:364)
at index.es.js:738
at MutationObserver. (index.es.js:514)

from react-hook-form.

bluebill1049 avatar bluebill1049 commented on May 5, 2024

which version of react-hook-from do you have? 3.7.4 ?

from react-hook-form.

 avatar commented on May 5, 2024

Yes, I have 3.7.4

from react-hook-form.

bluebill1049 avatar bluebill1049 commented on May 5, 2024

hmmm I will patch this bug tonight.

from react-hook-form.

 avatar commented on May 5, 2024

Thanks. Great!
The problem with ‘setError(‘total’)’ is that I check it like this:
if (total) {
props.setError("total");
}
To remove required error.

But, when I type a char, the error disappears and with the next char error appears!

from react-hook-form.

bluebill1049 avatar bluebill1049 commented on May 5, 2024

and error is : phone: {type: undefined, message: undefined, ref: undefined, isManual: true}?

from react-hook-form.

 avatar commented on May 5, 2024

Yes!

from react-hook-form.

bluebill1049 avatar bluebill1049 commented on May 5, 2024

thanks, are you using validationSchema? (sorry many questions, I try to drill down the issue)

from react-hook-form.

 avatar commented on May 5, 2024

Thanks for great support :). No, I am not using validationSchema.
I use it this way:

  useEffect(() => {
    // register a filed in react hook form
    props.register(
      {
        value: "",
        name: "total",
        type: "custom"
      },
      { required: true }
    );
  }, []);

Note: It didn't work without type and value.

from react-hook-form.

bluebill1049 avatar bluebill1049 commented on May 5, 2024

no worries :) you only need name, because it's not a real DOM element.

register({ name: 'total' }) will do

from react-hook-form.

bluebill1049 avatar bluebill1049 commented on May 5, 2024

I have patched https://github.com/bluebill1049/react-hook-form/releases/tag/3.7.5
let me know if that solved the issue. Thanks 👍

from react-hook-form.

 avatar commented on May 5, 2024

Thanks, The last problem is solved greatly.
Now, there is another error at the console:

index.es.js:333 Uncaught TypeError: ref.removeEventListener is not a function
    at removeAllEventListeners (index.es.js:333)
    at findRemovedFieldAndRemoveListener (index.es.js:365)
    at index.es.js:736
    at MutationObserver.<anonymous> (index.es.js:514)

Is there any way to automatically check for errors on setValue? I mean without setting error manually.

from react-hook-form.

bluebill1049 avatar bluebill1049 commented on May 5, 2024

hmmm, are you still using this register with value and type?

try to do manual register with just name.

normally you shouldn't have to do things manually.

patched again with 3.7.8

from react-hook-form.

 avatar commented on May 5, 2024

Looks like everything goes well :) Thank youuu

from react-hook-form.

bluebill1049 avatar bluebill1049 commented on May 5, 2024

❤️ can u show me how u register your custom input?

from react-hook-form.

 avatar commented on May 5, 2024
  useEffect(() => {
    // register a filed in react hook form
    props.register(
      {
        name: "total"
      },
      { required: true }
    );
  }, []);

from react-hook-form.

bluebill1049 avatar bluebill1049 commented on May 5, 2024

sweet thanks :) happy coding. Are you happy to close the issue?

from react-hook-form.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.