Skip to content

Commit

Permalink
✨ add controlled input to use it in forms
Browse files Browse the repository at this point in the history
  • Loading branch information
jerembdn committed Aug 9, 2023
1 parent 720f1a1 commit ec6a999
Show file tree
Hide file tree
Showing 7 changed files with 14,036 additions and 6,631 deletions.
1 change: 1 addition & 0 deletions packages/kitchen/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
"@fontsource/figtree": "^5.0.1",
"@fontsource/fira-code": "^5.0.0",
"js-cookie": "^3.0.5",
"react-hook-form": "^7.45.4",
"react-icons": "^4.7.1",
"styled-components": "^6.0.0"
},
Expand Down
60 changes: 60 additions & 0 deletions packages/kitchen/src/components/Input/Controlled/index.tsx
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;
2 changes: 1 addition & 1 deletion packages/kitchen/src/components/Input/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ const Input = styled(
label,
...props
}: InputProps) => {
const inputRef = React.useRef<HTMLInputElement>(null);
const inputRef = React.useRef<HTMLInputElement>();
const [selfValue, setSelfValue] = React.useState<string>(initialValue);
const [focus, setFocus] = React.useState<boolean>(false);
const [clearIconHover, setClearIconHover] = React.useState<boolean>(false);
Expand Down
2 changes: 2 additions & 0 deletions packages/kitchen/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ export type { IconProps } from "./components/Icon";
export { default as Input } from "./components/Input";
export type { InputProps } from "./components/Input";

export { default as ControlledInput } from "./components/Input/Controlled";

export { default as Image } from "./components/Image";
export type { ImageProps } from "./components/Image";

Expand Down
Loading

0 comments on commit ec6a999

Please sign in to comment.