Socket
Socket
Sign inDemoInstall

@hookform/strictly-typed

Package Overview
Dependencies
7
Maintainers
2
Versions
5
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    @hookform/strictly-typed

React Hook Form strictly typed custom hooks.


Version published
Maintainers
2
Install size
29.5 kB
Created

Readme

Source

React Hook Form Logo - React hook custom hook for form validation

Performant, flexible and extensible forms with easy to use validation.

npm downloads npm npm

Goal

React Hook Form strictly typed custom hooks.

Install

$ npm install @hookform/strictly-typed

Quickstart

import { useTypedController } from '@hookform/strictly-typed';
import { useForm } from 'react-hook-form';
import { TextField, Checkbox } from '@material-ui/core';

type FormValues = {
  flat: string;
  nested: {
    object: { test: string };
    array: { test: boolean }[];
  };
};

export default function App() {
  const { control, handleSubmit } = useForm<FormValues>();
  const TypedController = useTypedController<FormValues>({ control });

  const onSubmit = handleSubmit((data) => console.log(data));

  return (
    <form onSubmit={onSubmit}>
      <TypedController
        name="flat"
        defaultValue=""
        render={(props) => <TextField {...props} />}
      />

      <TypedController
        as="textarea"
        name={['nested', 'object', 'test']}
        defaultValue=""
        rules={{ required: true }}
      />

      <TypedController
        name={['nested', 'array', 0, 'test']}
        defaultValue={false}
        render={(props) => <Checkbox {...props} />}
      />

      {/* ❌: Type '"notExists"' is not assignable to type 'DeepPath<FormValues, "notExists">'. */}
      <TypedController as="input" name="notExists" defaultValue="" />

      {/* ❌: Type 'number' is not assignable to type 'string | undefined'. */}
      <TypedController
        as="input"
        name={['nested', 'object', 0, 'notExists']}
        defaultValue=""
      />

      {/* ❌: Type 'true' is not assignable to type 'string | undefined'. */}
      <TypedController as="input" name="flat" defaultValue={true} />

      <input type="submit" />
    </form>
  );
}

Edit React Hook Form - useTypedController

Name Reference

Field PathField Name
foofoo
['foo', 'bar']foo.bar
['foo', 0]foo[0]
['foo', '0']foo.0
['foo', 1]foo[1]
['foo', 0, 'bar']foo[0].bar
['foo']foo
['foo', 'bar']foo.bar
['foo', 'bar', 0]foo.bar[0]

API

  • useTypedController
NameTypeRequired
controlObject
  • TypedController
NameTypeRequired
namestring | [string, ...(string | number)[]]
as'input' | 'select' | 'textarea'
renderFunction
defaultValueDeepPathValue
rulesObject
onFocus() => void

Backers

Thanks goes to all our backers! [Become a backer].

Organizations

Thanks goes to these wonderful organizations! [Contribute].

Contributors

Thanks goes to these wonderful people! [Become a contributor].

Keywords

FAQs

Last updated on 06 Sep 2020

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc