-
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨ add controlled input to use it in forms
- Loading branch information
Showing
7 changed files
with
14,036 additions
and
6,631 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 60 additions & 0 deletions
60
packages/kitchen/src/components/Input/Controlled/index.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import React from "react"; | ||
import { | ||
FieldValues, | ||
ControllerProps, | ||
Control, | ||
Controller, | ||
Path, | ||
} from "react-hook-form"; | ||
import Input, { type InputProps } from ".."; | ||
|
||
type ControllerPropsEx<F extends FieldValues> = Omit< | ||
ControllerProps<F>, | ||
"render" | "control" | "name" | ||
>; | ||
|
||
type ControlledType<F extends FieldValues, T> = { | ||
name: Path<F>; | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
control: Control<any>; | ||
controllerProps?: ControllerPropsEx<F>; | ||
} & T; | ||
|
||
type ControlledTextInputProps<F extends FieldValues> = ControlledType< | ||
F, | ||
Omit<InputProps, "onChange" | "onBlur" | "value" | "errorMessage"> | ||
> & | ||
InputProps; | ||
|
||
const ControlledInput = React.forwardRef( | ||
<F extends FieldValues>( | ||
{ name, control, controllerProps, ...props }: ControlledTextInputProps<F>, | ||
ref: React.Ref<HTMLInputElement> | ||
) => ( | ||
<Controller | ||
name={name} | ||
control={control} | ||
{...controllerProps} | ||
render={({ | ||
field: { onBlur, onChange, value, name }, | ||
fieldState: { error }, | ||
formState: { isSubmitting }, | ||
}) => ( | ||
<Input | ||
{...props} | ||
ref={ref} | ||
name={name} | ||
value={value} | ||
onChange={onChange} | ||
onBlur={onBlur} | ||
errorMessage={error?.message} | ||
disabled={isSubmitting} | ||
/> | ||
)} | ||
/> | ||
) | ||
); | ||
|
||
ControlledInput.displayName = "ControlledInput"; | ||
|
||
export default ControlledInput; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.