How to use the react-hook-form function in react-hook-form

To help you get started, we’ve selected a few react-hook-form examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github react-hook-form / react-hook-form / examples / restrictValidationFields.tsx View on Github external
function App() {
  const { register, errors, handleSubmit } = useForm({
    validationFields: ["lastName"] // will only validate lastName onSubmit
  });

  const onSubmit = data => {
    alert(JSON.stringify(data));
  };
  console.log("errors", errors);

  return (
    <form>
      <h1>validationFields</h1>
      <label>First name: </label>
      <input name="firstName">

      <label>Last name: </label>
      <input name="lastName"></form>
github react-hook-form / react-hook-form / website / src / Setting.tsx View on Github external
function Setting({ settingButton, toggleSetting, showSetting, setting, setConfig }) {
  const buttonRef = useRef(null);
  const { register, handleSubmit } = useForm();
  const onSubmit = data =&gt; {
    setConfig(data);
    toggleSetting(false);
    settingButton.current.focus();
  };
  const tabIndex = showSetting ? 0 : -1;

  if (showSetting &amp;&amp; buttonRef.current) {
    // @ts-ignore
    buttonRef.current.focus();
  }

  return (
github react-hook-form / react-hook-form-website / src / components / Setting.tsx View on Github external
function Setting({ settingButton, toggleSetting, showSetting, setting, setConfig }) {
  const buttonRef = useRef(null);
  const { register, handleSubmit } = useForm()
  const onSubmit = data =&gt; {
    setConfig(data);
    toggleSetting(false);
    settingButton.current.focus();
  };
  const tabIndex = showSetting ? 0 : -1;

  if (showSetting &amp;&amp; buttonRef.current) {
    // @ts-ignore
    buttonRef.current.focus();
  }

  return (
github opticdev / optic / webapp / src / components / loaders / ExampleDrivenSpecLoader.js View on Github external
function ExampleBuilderBase(props) {

  const { register, handleSubmit, setValue, getValues, watch } = useForm({
    defaultValues: {
      request: {
        method: 'GET',
        url: '/'
      },
      response: {
        statusCode: 200
      }
    },
  });


  // need watch() to update getValues() on change
  const watchAll = watch();
  const formValues = getValues({ nest: true });
github react-hook-form / react-hook-form / website / src / Builder.tsx View on Github external
function Builder({
  formData,
  updateFormData,
  showBuilder,
  toggleBuilder,
  editFormData,
  setFormData,
  builderButton,
  HomeRef,
  isMobile,
}) {
  const { register, handleSubmit, errors = {}, watch } = useForm();
  const [editIndex, setEditIndex] = useState(-1);
  const copyFormData = useRef([]);
  const closeButton = useRef(null);
  const [showValidation, toggleValidation] = useState(false);
  const onSubmit = (data, event) => {
    if (editIndex >= 0) {
      formData[editIndex] = data;
      updateFormData([...formData]);
      setFormData({});
      setEditIndex(-1);
    } else {
      // @ts-ignore
      updateFormData([...formData, ...[data]]);
    }
    event.target.reset();
  };
github react-hook-form / react-hook-form / website / src / App.tsx View on Github external
showTouch: boolean;
    showSubmit: boolean;
  }>({
    mode: 'onChange',
    showError: true,
    showWatch: true,
    showTouch: true,
    showSubmit: true,
  });
  const {
    register,
    errors,
    handleSubmit,
    watch,
    formState: { touched },
  } = useForm({
    mode: setting.mode,
  });
  const tabIndex = showApi || showBuilder ? -1 : 0;

  const onSubmit = data => {
    updateSubmitData(data);
  };

  useEffect(() => {
    window.addEventListener('popstate', function() {
      const pathName = window.location.pathname;
      if (pathName === '/api') {
        toggleApi(true);
        toggleBuilder(false);
        document.body.style.overflow = 'hidden';
      } else if (pathName === '/builder') {
github dooboolab / hackatalk-mobile / src / components / screen / SignUp / index.tsx View on Github external
function SignUpPage(): ReactElement {
  const {
    register,
    errors,
    setValue,
    handleSubmit,
    formState: { touched },
    watch,
    reset,
  } = useForm({
    validationSchema: signUpValidationSchema,
  });
  const onTextChanged = useCallback(
    (label: keyof SignUpFormValues, text: string): void | Promise =&gt; {
      setValue(label, text, true);
    },
    [setValue],
  );
  const onSignUpSubmit = useCallback(
    ({ email, password, name, status }): void =&gt; {
      Alert.alert(
        'Signed Up',
        `You've signed up with 
        email: ${email},
        password: ${password},
        name: ${name},
github dooboolab / hackatalk-mobile / src / components / screen / FindPw / index.tsx View on Github external
function FindPw(): ReactElement {
  const {
    register,
    errors,
    setValue,
    handleSubmit,
    formState: { touched },
    watch,
    reset,
  } = useForm({
    validationSchema: schema,
  });
  const onSubmit = useCallback(({ email }) =&gt; {
    Alert.alert(
      'Find Password',
      `password reset link has been sent to your email: ${email}.`,
    );
    reset({ email: '' });
  }, []);
  const onTextChanged = useCallback((text: string): void | Promise =&gt; {
    setValue('email', text, true);
  }, []);
  const errorMessage = errors.email &amp;&amp; errors.email.message;

  return (
github Talend / ui / packages / forms / src / UIForm-v3 / schema / SchemaForm / SchemaForm.component.js View on Github external
export default function SchemaForm({
	customValidation,
	data,
	displayMode,
	onSubmit,
	onTrigger,
	widgets,
	...restProps
}) {
	const { properties } = data;
	const { mergedSchema } = useSchemaForm(data);
	const { handleSubmit, ...rhf } = useForm({ mode: 'onBlur', defaultValues: properties });

	const handleTrigger =
		onTrigger &&
		(({ schema, trigger }) =>
			onTrigger({
				errors: rhf.errors,
				properties: rhf.getValues({ nest: true }),
				schema,
				trigger,
			}));

	const contextValue = {
		customValidation,
		displayMode,
		onTrigger: handleTrigger,
		rhf,
github react-hook-form / react-hook-form-website / src / components / Form.tsx View on Github external
currentLanguage,
}: {
  onSubmit: any
  submitData: Object
  toggleBuilder: (state: boolean) =&gt; void
  formUpdated: boolean
  currentLanguage: string
}) {
  const {
    register,
    errors,
    handleSubmit,
    watch,
    formState: { touched },
    reset,
  } = useForm({
    mode: "onChange",
  })
  const {
    state: { formData },
  } = useStateMachine()

  return (
    &lt;&gt;
      
        <h1>{home.liveDemo[currentLanguage].title}</h1>
        {formUpdated &amp;&amp; (

react-hook-form

Performant, flexible and extensible forms library for React Hooks

MIT
Latest version published 13 days ago

Package Health Score

95 / 100
Full package analysis