From 52fc677ce5142165865a1fd15a696c2870dc0eaf Mon Sep 17 00:00:00 2001 From: poli0981 <127664709+poli0981@users.noreply.github.com> Date: Thu, 22 Oct 2026 14:53:14 +0700 Subject: [PATCH] refactor(ui): take refs as a prop instead of forwardRef MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit React 19 passes `ref` to function components as an ordinary prop, and the React team has said forwardRef will be deprecated in a future release. Button, IconButton, Input, Select and Textarea become plain function components whose props extend ComponentProps<"button" | "input" | …>, which already carries `ref`; each passes it on (Button to both Slot.Root and + ); - }, -); - -Button.displayName = "Button"; + } + return ( + + ); +} diff --git a/src/components/ui/IconButton.tsx b/src/components/ui/IconButton.tsx index 4228a0d..5a6150e 100644 --- a/src/components/ui/IconButton.tsx +++ b/src/components/ui/IconButton.tsx @@ -1,29 +1,35 @@ -import { forwardRef } from "react"; +import type { ReactNode } from "react"; import { Button, type ButtonProps } from "./Button"; import { Tooltip } from "./Tooltip"; interface IconButtonProps extends Omit { /** Accessible name AND tooltip text — an icon-only control needs both. */ label: string; - children: React.ReactNode; + children: ReactNode; size?: "icon" | "icon-sm"; /** Hide the hover tooltip (the name stays for screen readers). */ noTooltip?: boolean; tooltipSide?: "top" | "right" | "bottom" | "left"; } -/** A square, icon-only button that cannot be rendered without a name. */ -export const IconButton = forwardRef( - ( - { label, children, variant = "ghost", size = "icon", noTooltip, tooltipSide, ...props }, - ref, - ) => ( +/** + * A square, icon-only button that cannot be rendered without a name. A `ref` + * reaches the button through `props`. + */ +export function IconButton({ + label, + children, + variant = "ghost", + size = "icon", + noTooltip, + tooltipSide, + ...props +}: IconButtonProps) { + return ( - - ), -); - -IconButton.displayName = "IconButton"; + ); +} diff --git a/src/components/ui/Input.tsx b/src/components/ui/Input.tsx index c116bb7..a63eff3 100644 --- a/src/components/ui/Input.tsx +++ b/src/components/ui/Input.tsx @@ -1,8 +1,8 @@ -import { forwardRef, type InputHTMLAttributes, type ReactNode } from "react"; +import type { ComponentProps, ReactNode } from "react"; import clsx from "clsx"; import { Field, controlClasses } from "./Field"; -interface InputProps extends InputHTMLAttributes { +interface InputProps extends ComponentProps<"input"> { label?: string; error?: boolean; /** Inline error message shown below the input (also forces the red border). */ @@ -15,35 +15,42 @@ interface InputProps extends InputHTMLAttributes { leading?: ReactNode; } -export const Input = forwardRef( - ({ label, error, errorText, helpText, labelExtra, leading, className, id, ...props }, ref) => { - const isError = Boolean(error) || Boolean(errorText); - return ( - - {(control) => ( -
- {leading && ( - - {leading} - +export function Input({ + label, + error, + errorText, + helpText, + labelExtra, + leading, + className, + id, + ref, + ...props +}: InputProps) { + const isError = Boolean(error) || Boolean(errorText); + return ( + + {(control) => ( +
+ {leading && ( + + {leading} + + )} + -
- )} -
- ); - }, -); - -Input.displayName = "Input"; + {...props} + /> +
+ )} +
+ ); +} diff --git a/src/components/ui/Select.tsx b/src/components/ui/Select.tsx index bb21489..01b8052 100644 --- a/src/components/ui/Select.tsx +++ b/src/components/ui/Select.tsx @@ -1,4 +1,4 @@ -import { forwardRef, type SelectHTMLAttributes } from "react"; +import type { ComponentProps } from "react"; import { ChevronsUpDown } from "lucide-react"; import clsx from "clsx"; import { Field, controlClasses } from "./Field"; @@ -8,7 +8,7 @@ interface SelectOption { label: string; } -interface SelectProps extends Omit, "onChange"> { +interface SelectProps extends Omit, "onChange"> { label?: string; options: readonly SelectOption[]; onChange: (value: string) => void; @@ -21,8 +21,19 @@ interface SelectProps extends Omit, "onC * Android and iOS, type-ahead, and full keyboard support for free — and it * now matches the other controls' height (it used to be ~6px shorter). */ -export const Select = forwardRef( - ({ label, options, value, onChange, helpText, errorText, className, id, ...props }, ref) => ( +export function Select({ + label, + options, + value, + onChange, + helpText, + errorText, + className, + id, + ref, + ...props +}: SelectProps) { + return ( {(control) => ( // `className` sizes the control box (e.g. max-w-40), so the chevron @@ -52,7 +63,5 @@ export const Select = forwardRef( )} - ), -); - -Select.displayName = "Select"; + ); +} diff --git a/src/components/ui/Textarea.tsx b/src/components/ui/Textarea.tsx index 7006ae8..2e508d3 100644 --- a/src/components/ui/Textarea.tsx +++ b/src/components/ui/Textarea.tsx @@ -1,8 +1,8 @@ -import { forwardRef, type ReactNode, type TextareaHTMLAttributes } from "react"; +import type { ComponentProps, ReactNode } from "react"; import clsx from "clsx"; import { Field, controlClasses } from "./Field"; -interface TextareaProps extends TextareaHTMLAttributes { +interface TextareaProps extends ComponentProps<"textarea"> { label?: string; error?: boolean; errorText?: string; @@ -10,27 +10,33 @@ interface TextareaProps extends TextareaHTMLAttributes { labelExtra?: ReactNode; } -export const Textarea = forwardRef( - ({ label, error, errorText, helpText, labelExtra, className, id, ...props }, ref) => { - const isError = Boolean(error) || Boolean(errorText); - return ( - - {(control) => ( -