Skip to content

Commit

Permalink
✨ add forwardref to input component
Browse files Browse the repository at this point in the history
  • Loading branch information
jerembdn committed Jan 11, 2024
1 parent c596a5d commit 5f1128e
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 30 deletions.
5 changes: 5 additions & 0 deletions .changeset/moody-mirrors-itch.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@tonightpass/kitchen": patch
---

Set forwardRef to Input component
63 changes: 36 additions & 27 deletions packages/kitchen/src/components/Input/index.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React from "react";
import React, { forwardRef, useImperativeHandle } from "react";
import { RiCloseCircleLine } from "react-icons/ri";
import styled from "styled-components";

Expand Down Expand Up @@ -47,31 +47,34 @@ export type InputProps = KitchenComponent<
React.InputHTMLAttributes<HTMLInputElement>
>;

const Input = styled(
({
size = "normal",
prefix,
suffix,
disabled = false,
prefixContainer = true,
suffixContainer = true,
prefixStyling = true,
suffixStyling = true,
clearable = false,
value,
initialValue = "",
readOnly = false,
onChange,
width,
onClearClick,
onFocus,
onBlur,
error,
type,
label,
...props
}: InputProps) => {
const inputRef = React.useRef<HTMLInputElement>();
const ForwardedInput = forwardRef<HTMLInputElement, InputProps>(
(
{
size = "normal",
prefix,
suffix,
disabled = false,
prefixContainer = true,
suffixContainer = true,
prefixStyling = true,
suffixStyling = true,
clearable = false,
value,
initialValue = "",
readOnly = false,
onChange,
width,
onClearClick,
onFocus,
onBlur,
error,
type,
label,
...props
}: InputProps,
ref: React.Ref<HTMLInputElement>,
) => {
const inputRef = React.useRef<HTMLInputElement>(null);
const [selfValue, setSelfValue] = React.useState<string>(initialValue);
const [focus, setFocus] = React.useState<boolean>(false);
const [clearIconHover, setClearIconHover] = React.useState<boolean>(false);
Expand All @@ -80,6 +83,8 @@ const Input = styled(
[value],
);

useImperativeHandle(ref, () => inputRef.current as HTMLInputElement);

const Wrapper = label ? "label" : React.Fragment;

const controlledValue = isControlledComponent
Expand Down Expand Up @@ -223,7 +228,11 @@ const Input = styled(
</Wrapper>
);
},
)`
);

ForwardedInput.displayName = "Input";

const Input = styled(ForwardedInput)`
font: inherit;
width: 100%;
min-width: 0;
Expand Down
33 changes: 30 additions & 3 deletions workshop/pages/input.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,19 @@ type FormData = {
};

const InputPage: NextPage = () => {
const { control, handleSubmit } = useForm<FormData>();
const { control, handleSubmit: handleSubmitControl } = useForm<FormData>();

const onSubmit = handleSubmit((data) => {
const onSubmitControl = handleSubmitControl((data) => {
console.log(data);
});

const {
register,
handleSubmit: handleSubmitRegistered,
formState: { isValid: isValidRegistered },
} = useForm<FormData>();

const onSubmitRegistered = handleSubmitRegistered((data) => {
console.log(data);
});

Expand Down Expand Up @@ -305,11 +315,28 @@ const InputPage: NextPage = () => {
</Container>
</Container>

<Container gap={5}>
<Text>{"registered"}</Text>
<Text>{`is valid : ${isValidRegistered}`}</Text>
<Container row>
<Container align={"flex-start"}>
<form onSubmit={onSubmitRegistered}>
<Input
{...register("name", {
required: true,
maxLength: 20,
})}
/>
</form>
</Container>
</Container>
</Container>

<Container gap={5}>
<Text>{"controlled"}</Text>
<Container row>
<Container align={"flex-start"}>
<form onSubmit={onSubmit}>
<form onSubmit={onSubmitControl}>
<ControlledInput name={"name"} control={control} />
</form>
</Container>
Expand Down

2 comments on commit 5f1128e

@vercel
Copy link

@vercel vercel bot commented on 5f1128e Jan 11, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

kitchen-workshop – ./workshop

kitchen-workshop-git-master-tonightpass.vercel.app
kitchen-workshop-tonightpass.vercel.app
kitchen-workshop.vercel.app

@vercel
Copy link

@vercel vercel bot commented on 5f1128e Jan 11, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.