From f9fe675208cae521ce2b215afb81bae1452bcfa3 Mon Sep 17 00:00:00 2001 From: Quentin Gliech Date: Wed, 2 Aug 2023 10:00:26 +0200 Subject: [PATCH] Forward refs to most components (#47) --- .../ActionControl/ActionControl.tsx | 27 ++++++++++--------- src/components/Checkbox/Checkbox.tsx | 16 ++++++----- src/components/Form/Control.tsx | 12 ++++----- .../Form/Controls/Password/Password.tsx | 8 +++--- src/components/Form/Field.tsx | 12 ++++----- src/components/Form/Label.tsx | 12 ++++----- src/components/Form/Message.tsx | 12 ++++----- src/components/Form/Root.tsx | 12 ++++----- src/components/Link/Link.tsx | 13 +++++---- src/components/Radio/Radio.tsx | 13 +++++---- src/components/Toggle/Toggle.tsx | 12 ++++----- 11 files changed, 76 insertions(+), 73 deletions(-) diff --git a/src/components/ActionControl/ActionControl.tsx b/src/components/ActionControl/ActionControl.tsx index 08f2c789..f476758b 100644 --- a/src/components/ActionControl/ActionControl.tsx +++ b/src/components/ActionControl/ActionControl.tsx @@ -28,19 +28,19 @@ type ActionControlProps = { disabled?: boolean; } & React.ComponentProps; -export const ActionControl: React.FC> = ({ - children, - Icon, - className, - actionLabel, - onActionClick, - ...props -}) => { +export const ActionControl = React.forwardRef< + HTMLInputElement, + PropsWithChildren +>(function ActionControl( + { children, Icon, className, actionLabel, onActionClick, ...props }, + ref, +) { const id = useId(); const classes = classnames(styles.actioncontrol, className); return (
> = ({ />
); -}; +}); -export const StandaloneActionControl: React.FC< +export const StandaloneActionControl = React.forwardRef< + HTMLInputElement, PropsWithChildren -> = (props) => { +>(function StandaloneActionControl(props, ref) { return ( - + ); -}; +}); diff --git a/src/components/Checkbox/Checkbox.tsx b/src/components/Checkbox/Checkbox.tsx index bde1a28a..5ad727b0 100644 --- a/src/components/Checkbox/Checkbox.tsx +++ b/src/components/Checkbox/Checkbox.tsx @@ -25,16 +25,18 @@ type CheckboxProps = { onMouseDown?: (e: React.MouseEvent) => void; } & React.ComponentPropsWithoutRef<"input">; -export const Checkbox: React.FC> = ({ - kind = "primary", - className, - onMouseDown, - ...props -}) => { +export const Checkbox = React.forwardRef< + HTMLInputElement, + PropsWithChildren +>(function Checkbox( + { kind = "primary", className, onMouseDown, ...props }, + ref, +) { const classes = classnames(styles.checkbox, className); return (
{ @@ -49,4 +51,4 @@ export const Checkbox: React.FC> = ({
); -}; +}); diff --git a/src/components/Form/Control.tsx b/src/components/Form/Control.tsx index ca8696ff..90798464 100644 --- a/src/components/Form/Control.tsx +++ b/src/components/Form/Control.tsx @@ -28,14 +28,14 @@ type ControlProps = { * Thin wrapper around Radix UI Control component * https://www.radix-ui.com/docs/primitives/components/form#control */ -export const Control: React.FC> = ({ - children, - ...props -}) => { +export const Control = React.forwardRef< + HTMLInputElement, + PropsWithChildren +>(function Control({ children, ...props }, ref) { const classes = classNames(styles.control, props.className); return ( - + {children} ); -}; +}); diff --git a/src/components/Form/Controls/Password/Password.tsx b/src/components/Form/Controls/Password/Password.tsx index 69c786a5..0422d305 100644 --- a/src/components/Form/Controls/Password/Password.tsx +++ b/src/components/Form/Controls/Password/Password.tsx @@ -39,15 +39,17 @@ const hideState = { * Thin wrapper around Radix UI Control component * https://www.radix-ui.com/docs/primitives/components/form#control */ -export const PasswordControl: React.FC< +export const PasswordControl = React.forwardRef< + HTMLInputElement, PropsWithChildren> -> = (props) => { +>(function PasswordControl(props, ref) { const [{ icon, label, type }, togglePasswordVisibility] = useReducer( (state) => (!state.isHidden ? showState : hideState), showState, ); return ( ); -}; +}); diff --git a/src/components/Form/Field.tsx b/src/components/Form/Field.tsx index a50f7156..d9c84514 100644 --- a/src/components/Form/Field.tsx +++ b/src/components/Form/Field.tsx @@ -28,14 +28,14 @@ type FieldProps = { * Thin wrapper around Radix UI Field component * https://www.radix-ui.com/docs/primitives/components/form#field */ -export const Field: React.FC> = ({ - children, - ...props -}) => { +export const Field = React.forwardRef< + HTMLDivElement, + PropsWithChildren +>(function Field({ children, ...props }, ref) { const classes = classNames(styles.field, props.className); return ( - + {children} ); -}; +}); diff --git a/src/components/Form/Label.tsx b/src/components/Form/Label.tsx index 33978838..886ef719 100644 --- a/src/components/Form/Label.tsx +++ b/src/components/Form/Label.tsx @@ -28,14 +28,14 @@ type LabelProps = { * Thin wrapper around Radix UI Label component * https://www.radix-ui.com/docs/primitives/components/form#label */ -export const Label: React.FC> = ({ - children, - ...props -}) => { +export const Label = React.forwardRef< + HTMLLabelElement, + PropsWithChildren +>(function Label({ children, ...props }, ref) { const classes = classNames(styles.label, props.className); return ( - + {children} ); -}; +}); diff --git a/src/components/Form/Message.tsx b/src/components/Form/Message.tsx index fb597266..08258e28 100644 --- a/src/components/Form/Message.tsx +++ b/src/components/Form/Message.tsx @@ -28,16 +28,16 @@ type MessageProps = { * Thin wrapper around Radix UI Message component * https://www.radix-ui.com/docs/primitives/components/form#message */ -export const Message: React.FC> = ({ - children, - ...props -}) => { +export const Message = React.forwardRef< + HTMLSpanElement, + PropsWithChildren +>(function Message({ children, ...props }, ref) { const classes = classNames(styles.message, props.className); return ( - + {/* Pending to be replaced by the alert component, see https://github.com/vector-im/compound-web/pull/6 */} {children} ); -}; +}); diff --git a/src/components/Form/Root.tsx b/src/components/Form/Root.tsx index 545a3547..fd6e729a 100644 --- a/src/components/Form/Root.tsx +++ b/src/components/Form/Root.tsx @@ -28,14 +28,14 @@ type RootProps = { * Thin wrapper around Radix UI Root component * https://www.radix-ui.com/docs/primitives/components/form#root */ -export const Root: React.FC> = ({ - children, - ...props -}) => { +export const Root = React.forwardRef< + HTMLFormElement, + PropsWithChildren +>(function Root({ children, ...props }, ref) { const classes = classNames(styles.root, props.className); return ( - + {children} ); -}; +}); diff --git a/src/components/Link/Link.tsx b/src/components/Link/Link.tsx index b3756fda..7131c499 100644 --- a/src/components/Link/Link.tsx +++ b/src/components/Link/Link.tsx @@ -23,14 +23,13 @@ type LinkProps = { kind?: "primary" | "critical"; } & Omit, "rel">; -export const Link: React.FC> = ({ - children, - className, - kind = "primary", - ...props -}) => { +export const Link = React.forwardRef< + HTMLAnchorElement, + PropsWithChildren +>(function Link({ children, className, kind = "primary", ...props }, ref) { return ( > = ({ {children} ); -}; +}); diff --git a/src/components/Radio/Radio.tsx b/src/components/Radio/Radio.tsx index f24ec5cc..5999577e 100644 --- a/src/components/Radio/Radio.tsx +++ b/src/components/Radio/Radio.tsx @@ -27,16 +27,15 @@ type RadioProps = { /** * Radio form control */ -export const Radio: React.FC> = ({ - kind = "primary", - className, - onMouseDown, - ...props -}) => { +export const Radio = React.forwardRef< + HTMLInputElement, + PropsWithChildren +>(function Radio({ kind = "primary", className, onMouseDown, ...props }, ref) { const classes = classnames(styles.radio, className); return (
{ @@ -49,4 +48,4 @@ export const Radio: React.FC> = ({
); -}; +}); diff --git a/src/components/Toggle/Toggle.tsx b/src/components/Toggle/Toggle.tsx index e0d49df1..c1148fe7 100644 --- a/src/components/Toggle/Toggle.tsx +++ b/src/components/Toggle/Toggle.tsx @@ -27,15 +27,15 @@ type ToggleProps = { * Standalone toggle component to be used with a Radix form control * See https://www.radix-ui.com/docs/primitives/components/form#composing-with-your-own-components */ -export const Toggle: React.FC> = ({ - className, - onMouseDown, - ...props -}) => { +export const Toggle = React.forwardRef< + HTMLInputElement, + PropsWithChildren +>(function Toggle({ className, onMouseDown, ...props }, ref) { const classes = classnames(styles.toggle, className); return (
{ @@ -48,4 +48,4 @@ export const Toggle: React.FC> = ({
); -}; +});