Skip to content

Commit

Permalink
Update components and typings according to react-hook-form update
Browse files Browse the repository at this point in the history
  • Loading branch information
clement-faure committed Jun 15, 2021
1 parent f405860 commit 2679d94
Show file tree
Hide file tree
Showing 10 changed files with 27 additions and 42 deletions.
9 changes: 2 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,15 @@ yarn add mui-rhf
## Usage

```
import * as React from 'react';
import React from 'react';
import { useForm } from "react-hook-form";
import { MuiRhfForm } from "mui-rhf";
import { Button } from "@material-ui/core";
const Form = ({ onSubmit }) => {
// Initialize form
const { control, errors, handleSubmit } = useForm();
const { control, handleSubmit } = useForm();
const onSubmit = (data) => {
// Play with retrieved data
Expand All @@ -42,7 +42,6 @@ const Form = ({ onSubmit }) => {
<form onSubmit={handleSubmit(onSubmit)} noValidate>
<MuiRhfForm
control={control}
errors={errors}
fields={[
[{ name: "firstName" }, { name: "lastName" }],
[{ name: "email" }],
Expand Down Expand Up @@ -104,10 +103,6 @@ Provided by [React Hook Form](https://react-hook-form.com/api#watch)

Provided by [React Hook Form](https://react-hook-form.com/api#control)

#### errors: FieldErrors;

Provided by [React Hook Form](https://react-hook-form.com/api#errors)

## Contributors ✨

This project follows the
Expand Down
9 changes: 4 additions & 5 deletions src/components/MuiRhfAutocomplete.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as React from "react";
import React from "react";

import { Controller } from "react-hook-form";

Expand All @@ -9,7 +9,6 @@ import { MuiRhfAutocompleteProps } from "~/models/fields";

const MuiRhfAutocomplete: React.FC<MuiRhfAutocompleteProps> = ({
control,
errors,
name,
defaultValue,
textFieldProps,
Expand All @@ -19,15 +18,15 @@ const MuiRhfAutocomplete: React.FC<MuiRhfAutocompleteProps> = ({
name={name}
control={control}
defaultValue={defaultValue}
render={({ field }) => (
render={({ field, fieldState: { error } }) => (
<Autocomplete
fullWidth
{...field}
{...autocompleteProps}
renderInput={(params) => (
<TextField
error={!!errors?.[name]}
helperText={errors?.[name]}
error={!!error}
helperText={error?.message}
{...params}
{...textFieldProps}
/>
Expand Down
2 changes: 1 addition & 1 deletion src/components/MuiRhfAutocompleteMultiple.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as React from "react";
import React from "react";

import { MuiRhfAutocompleteProps } from "~/models/fields";

Expand Down
2 changes: 1 addition & 1 deletion src/components/MuiRhfAutocompleteSingle.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as React from "react";
import React from "react";

import { MuiRhfAutocompleteProps } from "~/models/fields";

Expand Down
10 changes: 4 additions & 6 deletions src/components/MuiRhfCheckbox.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as React from "react";
import React from "react";

import { Controller } from "react-hook-form";

Expand All @@ -13,7 +13,6 @@ import { MuiRhfCheckboxProps } from "~/models/fields";

const MuiRhfCheckbox: React.FC<MuiRhfCheckboxProps> = ({
control,
errors,
name,
defaultValue = false,
label,
Expand All @@ -25,10 +24,9 @@ const MuiRhfCheckbox: React.FC<MuiRhfCheckboxProps> = ({
name={name}
defaultValue={defaultValue}
control={control}
render={({ field: { onChange, /*onBlur, */ value, ref } }) => {
const error = !!errors?.[name];
render={({ field: { onChange, value, ref }, fieldState: { error } }) => {
return (
<FormControl required={required} disabled={disabled} error={error}>
<FormControl required={required} disabled={disabled} error={!!error}>
<FormControlLabel
inputRef={ref}
checked={value}
Expand All @@ -38,7 +36,7 @@ const MuiRhfCheckbox: React.FC<MuiRhfCheckboxProps> = ({
control={<Checkbox {...rest} />}
label={label}
/>
{error && <FormHelperText>{error}</FormHelperText>}
{error && <FormHelperText>{error.message}</FormHelperText>}
</FormControl>
);
}}
Expand Down
7 changes: 3 additions & 4 deletions src/components/MuiRhfForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ const MuiRhfForm: React.FC<MuiRhfFormProps> = ({
headers,
watch,
control,
errors,
spacing,
}) => {
return (
Expand Down Expand Up @@ -115,8 +114,9 @@ const MuiRhfForm: React.FC<MuiRhfFormProps> = ({

// Check hideConditions
conditionalKeys.forEach((conditionalKey) => {
const [path, customCondition] =
conditionalProps[conditionalKey];
const [path, customCondition] = conditionalProps[
conditionalKey
];

_set(
extra,
Expand All @@ -142,7 +142,6 @@ const MuiRhfForm: React.FC<MuiRhfFormProps> = ({
label={label}
name={name}
control={control}
errors={errors}
/>
</Grid>
);
Expand Down
10 changes: 4 additions & 6 deletions src/components/MuiRhfSelect.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as React from "react";
import React from "react";

import { Controller } from "react-hook-form";

Expand All @@ -14,7 +14,6 @@ import { MuiRhfSelectProps } from "~/models/fields";

const MuiRhfSelect: React.FC<MuiRhfSelectProps> = ({
control,
errors,
name,
label,
defaultValue = "",
Expand All @@ -25,10 +24,9 @@ const MuiRhfSelect: React.FC<MuiRhfSelectProps> = ({
name={name}
defaultValue={defaultValue}
control={control}
render={({ field }) => {
const error = errors?.[name];
render={({ field, fieldState: { error } }) => {
return (
<FormControl error={error}>
<FormControl error={!!error}>
<InputLabel id={name}>{label}</InputLabel>
<Select
labelId={name}
Expand All @@ -43,7 +41,7 @@ const MuiRhfSelect: React.FC<MuiRhfSelectProps> = ({
))
)}
</Select>
{error && <FormHelperText>{error}</FormHelperText>}
{error && <FormHelperText>{error.message}</FormHelperText>}
</FormControl>
);
}}
Expand Down
9 changes: 4 additions & 5 deletions src/components/MuiRhfTextField.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as React from "react";
import React from "react";

import { Controller } from "react-hook-form";

Expand All @@ -8,7 +8,6 @@ import { MuiRhfTextFieldProps } from "~/models/fields";

const MuiRhfTextField: React.FC<MuiRhfTextFieldProps> = ({
control,
errors,
name,
defaultValue = "",
...rest
Expand All @@ -17,11 +16,11 @@ const MuiRhfTextField: React.FC<MuiRhfTextFieldProps> = ({
name={name}
defaultValue={defaultValue}
control={control}
render={({ field }) => (
render={({ field, fieldState: { error } }) => (
<TextField
fullWidth
error={!!errors?.[name]}
helperText={errors?.[name]}
error={!!error}
helperText={error?.message}
{...field}
{...rest}
/>
Expand Down
4 changes: 1 addition & 3 deletions src/models/fields/typing.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import { Control, FieldErrors } from "react-hook-form";
import { Control } from "react-hook-form";
import { TextFieldProps, SelectProps, CheckboxProps } from "@material-ui/core";
import { AutocompleteProps } from "@material-ui/lab";

/** Common fields props */
export type MuiRhfFieldProps = {
control: Control;
errors: FieldErrors;
name: string;
label?: string;
};
Expand Down
7 changes: 3 additions & 4 deletions src/models/form/typing.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Control, FieldErrors } from "react-hook-form";
import { Control } from "react-hook-form";
import { GridSpacing, GridProps } from "@material-ui/core";
import {
MuiRhfTextFieldProps,
Expand Down Expand Up @@ -27,7 +27,7 @@ type MuiRhfFormFieldConditionalProps = {
[key: string]: [string, (value: unknown) => unknown];
};

type MuiRhfFormField = {
export type MuiRhfFormField = {
name: string;
label?: string;
props?: any;
Expand All @@ -38,7 +38,7 @@ type MuiRhfFormField = {
conditionalProps?: MuiRhfFormFieldConditionalProps; // Props applied when hideCondition is satisfied
};

type MuiRhfFormHeader = {
export type MuiRhfFormHeader = {
title?: string;
};

Expand All @@ -48,5 +48,4 @@ export type MuiRhfFormProps = {
spacing?: GridSpacing;
watch?: RhfWatch;
control: Control;
errors: FieldErrors;
};

0 comments on commit 2679d94

Please sign in to comment.