Skip to content

Commit

Permalink
Add defaultValue on each form component
Browse files Browse the repository at this point in the history
  • Loading branch information
clement-faure committed Mar 28, 2021
1 parent 37338c1 commit 8163d00
Show file tree
Hide file tree
Showing 8 changed files with 67 additions and 8 deletions.
28 changes: 28 additions & 0 deletions src/components/MuiRhfAutocompleteMultiple.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import * as React from "react";

import { TextFieldProps } from "@material-ui/core";

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

import MuiRhfAutocomplete from "./MuiRhfAutocomplete";

type MuiRhfAutocompleteProps = MuiRhfFieldProps & {
defaultValue?: unknown;
textField?: Omit<TextFieldProps, "onChange">;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
autocomplete?: any;
};

const MuiRhfAutocompleteMultiple: React.FC<MuiRhfAutocompleteProps> = ({
defaultValue = [],
autocomplete: autocompleteProps = {},
...rest
}) => (
<MuiRhfAutocomplete
{...rest}
autocomplete={{ ...autocompleteProps, multiple: true }}
defaultValue={defaultValue}
/>
);

export default MuiRhfAutocompleteMultiple;
28 changes: 28 additions & 0 deletions src/components/MuiRhfAutocompleteSingle.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import * as React from "react";

import { TextFieldProps } from "@material-ui/core";

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

import MuiRhfAutocomplete from "./MuiRhfAutocomplete";

type MuiRhfAutocompleteProps = MuiRhfFieldProps & {
defaultValue?: unknown;
textField?: Omit<TextFieldProps, "onChange">;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
autocomplete?: any;
};

const MuiRhfAutocompleteSingle: React.FC<MuiRhfAutocompleteProps> = ({
defaultValue = null,
autocomplete: autocompleteProps = {},
...rest
}) => (
<MuiRhfAutocomplete
{...rest}
autocomplete={{ ...autocompleteProps, multiple: false }}
defaultValue={defaultValue}
/>
);

export default MuiRhfAutocompleteSingle;
2 changes: 1 addition & 1 deletion src/components/MuiRhfCheckbox.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const MuiRhfCheckbox: React.FC<MuiRhfCheckboxProps> = ({
control,
errors,
name,
defaultValue,
defaultValue = false,
label,
required,
disabled,
Expand Down
2 changes: 1 addition & 1 deletion src/components/MuiRhfSelect.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const MuiRhfSelect: React.FC<MuiRhfSelectProps> = ({
errors,
name,
label,
defaultValue,
defaultValue = "",
options,
select: selectProps,
}) => (
Expand Down
2 changes: 1 addition & 1 deletion src/components/MuiRhfTextField.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const MuiRhfTextField: React.FC<MuiRhfTextFieldProps> = ({
control,
errors,
name,
defaultValue,
defaultValue = "",
...rest
}) => (
<Controller
Expand Down
4 changes: 2 additions & 2 deletions src/components/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ import MuiRhfForm from "./MuiRhfForm";
import MuiRhfTextField from "./MuiRhfTextField";
import MuiRhfSelect from "./MuiRhfSelect";
import MuiRhfCheckbox from "./MuiRhfCheckbox";
import MuiRhfAutocomplete from "./MuiRhfAutocomplete";
import MuiRhfAutocompleteSingle from "./MuiRhfAutocompleteSingle";

export {
MuiRhfForm,
MuiRhfTextField,
MuiRhfSelect,
MuiRhfCheckbox,
MuiRhfAutocomplete,
MuiRhfAutocompleteSingle,
};
6 changes: 4 additions & 2 deletions src/models/form/consts.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import MuiRhfTextField from "~/components/MuiRhfTextField";
import MuiRhfSelect from "~/components/MuiRhfSelect";
import MuiRhfCheckbox from "~/components/MuiRhfCheckbox";
import MuiRhfAutocomplete from "~/components/MuiRhfAutocomplete";
import MuiRhfAutocompleteSingle from "~/components/MuiRhfAutocompleteSingle";
import MuiRhfAutocompleteMultiple from "~/components/MuiRhfAutocompleteMultiple";

import { MuiRhfFieldComponentMap } from "./typing";

export const fieldComponentMap: MuiRhfFieldComponentMap = {
textField: MuiRhfTextField,
select: MuiRhfSelect,
checkbox: MuiRhfCheckbox,
autocomplete: MuiRhfAutocomplete,
autocompleteSingle: MuiRhfAutocompleteSingle,
autocompleteMultiple: MuiRhfAutocompleteMultiple,
};
3 changes: 2 additions & 1 deletion src/models/form/typing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ export type MuiRhfFieldComponentMap = {
textField: React.FC<MuiRhfTextFieldProps>;
select: React.FC<MuiRhfSelectProps>;
checkbox: React.FC<MuiRhfCheckboxProps>;
autocomplete: React.FC<MuiRhfAutocompleteProps>;
autocompleteSingle: React.FC<MuiRhfAutocompleteProps>;
autocompleteMultiple: React.FC<MuiRhfAutocompleteProps>;
};

/** Form */
Expand Down

0 comments on commit 8163d00

Please sign in to comment.