Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/react-hook-form/strictly-typed

📋 V6 - Strictly typed Controller
https://github.com/react-hook-form/strictly-typed

forms input-validation strictly-typed typescript

Last synced: 4 months ago
JSON representation

📋 V6 - Strictly typed Controller

Awesome Lists containing this project

README

        




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


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

[![npm downloads](https://img.shields.io/npm/dm/@hookform/strictly-typed.svg?style=for-the-badge)](https://www.npmjs.com/package/@hookform/strictly-typed)
[![npm](https://img.shields.io/npm/dt/@hookform/strictly-typed.svg?style=for-the-badge)](https://www.npmjs.com/package/@hookform/strictly-typed)
[![npm](https://img.shields.io/bundlephobia/minzip/@hookform/strictly-typed?style=for-the-badge)](https://bundlephobia.com/result?p=@hookform/strictly-typed)

## Goal

React Hook Form strictly typed custom hooks.

## Install

```
$ npm install @hookform/strictly-typed
```

## Quickstart

```tsx
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();
const TypedController = useTypedController({ control });

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

return (

}
/>

}
/>

{/* ❌: Type '"notExists"' is not assignable to type 'DeepPath'. */}

{/* ❌: Type 'number' is not assignable to type 'string | undefined'. */}

{/* ❌: Type 'true' is not assignable to type 'string | undefined'. */}



);
}
```

[![Edit React Hook Form - useTypedController](https://codesandbox.io/static/img/play-codesandbox.svg)](https://codesandbox.io/s/react-hook-form-usetypedcontroller-23qv1?fontsize=14&hidenavigation=1&theme=dark)

## Name Reference

| Field Path | Field Name |
| :------------------ | :----------- |
| `foo` | `foo` |
| `['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

| Name | Type | Required |
| :-------- | :------- | :------- |
| `control` | `Object` | |

- TypedController

| Name | Type | Required |
| :------------- | :-------------------------------------------- | :------- |
| `name` | `string \| [string, ...(string \| number)[]]` | ✓ |
| `as` | `'input' \| 'select' \| 'textarea'` | |
| `render` | `Function` | |
| `defaultValue` | `DeepPathValue` | |
| `rules` | `Object` | |
| `onFocus` | `() => void` | |

## Backers

Thanks goes to all our backers! [[Become a backer](https://opencollective.com/react-hook-form#backer)].



## Organizations

Thanks goes to these wonderful organizations! [[Contribute](https://opencollective.com/react-hook-form/contribute)].



## Contributors

Thanks goes to these wonderful people! [[Become a contributor](CONTRIBUTING.md)].