From 9ec347c8e9161ab28d876cd813e55284ef5d9a0c Mon Sep 17 00:00:00 2001 From: Brian Ingles Date: Tue, 2 Apr 2024 12:34:17 -0500 Subject: [PATCH] forwardedRefs (#1909) --- packages/components/src/spectrum/Heading.tsx | 14 +++++++--- packages/components/src/spectrum/Text.tsx | 14 +++------- packages/components/src/spectrum/View.tsx | 29 ++++++++++++-------- 3 files changed, 31 insertions(+), 26 deletions(-) diff --git a/packages/components/src/spectrum/Heading.tsx b/packages/components/src/spectrum/Heading.tsx index 38682e6ca7..f29e7b2729 100644 --- a/packages/components/src/spectrum/Heading.tsx +++ b/packages/components/src/spectrum/Heading.tsx @@ -1,9 +1,10 @@ /* eslint-disable react/jsx-props-no-spreading */ -import { useMemo } from 'react'; +import { forwardRef, useMemo } from 'react'; import { Heading as SpectrumHeading, type HeadingProps as SpectrumHeadingProps, } from '@adobe/react-spectrum'; +import type { DOMRefValue } from '@react-types/shared'; import { type ColorValue, colorValueStyle } from '../theme/colorUtils'; export type HeadingProps = SpectrumHeadingProps & { @@ -20,7 +21,10 @@ export type HeadingProps = SpectrumHeadingProps & { * */ -export function Heading(props: HeadingProps): JSX.Element { +export const Heading = forwardRef< + DOMRefValue, + HeadingProps +>((props, forwardedRef): JSX.Element => { const { color, UNSAFE_style, ...rest } = props; const style = useMemo( () => ({ @@ -30,7 +34,9 @@ export function Heading(props: HeadingProps): JSX.Element { [color, UNSAFE_style] ); - return ; -} + return ; +}); + +Heading.displayName = 'Heading'; export default Heading; diff --git a/packages/components/src/spectrum/Text.tsx b/packages/components/src/spectrum/Text.tsx index da6c7348eb..d0467f275d 100644 --- a/packages/components/src/spectrum/Text.tsx +++ b/packages/components/src/spectrum/Text.tsx @@ -4,7 +4,7 @@ import { Text as SpectrumText, type TextProps as SpectrumTextProps, } from '@adobe/react-spectrum'; -import type { DOMRef, DOMRefValue } from '@react-types/shared'; +import type { DOMRefValue } from '@react-types/shared'; import { type ColorValue, colorValueStyle } from '../theme/colorUtils'; export type TextProps = SpectrumTextProps & { @@ -20,8 +20,8 @@ export type TextProps = SpectrumTextProps & { * @returns The Text component * */ -export const Text = forwardRef, SpectrumTextProps>( - (props: TextProps, ref): JSX.Element => { +export const Text = forwardRef, TextProps>( + (props, forwardedRef): JSX.Element => { const { color, UNSAFE_style, ...rest } = props; const style = useMemo( () => ({ @@ -31,13 +31,7 @@ export const Text = forwardRef, SpectrumTextProps>( [color, UNSAFE_style] ); - return ( - } - UNSAFE_style={style} - /> - ); + return ; } ); diff --git a/packages/components/src/spectrum/View.tsx b/packages/components/src/spectrum/View.tsx index 847900541e..03c28a193e 100644 --- a/packages/components/src/spectrum/View.tsx +++ b/packages/components/src/spectrum/View.tsx @@ -1,9 +1,10 @@ /* eslint-disable react/jsx-props-no-spreading */ -import { useMemo } from 'react'; +import { forwardRef, useMemo } from 'react'; import { View as SpectrumView, type ViewProps as SpectrumViewProps, } from '@adobe/react-spectrum'; +import type { DOMRefValue } from '@react-types/shared'; import { type ColorValue, colorValueStyle } from '../theme/colorUtils'; export type ViewProps = Omit, 'backgroundColor'> & { @@ -20,17 +21,21 @@ export type ViewProps = Omit, 'backgroundColor'> & { * */ -export function View(props: ViewProps): JSX.Element { - const { backgroundColor, UNSAFE_style, ...rest } = props; - const style = useMemo( - () => ({ - ...UNSAFE_style, - backgroundColor: colorValueStyle(backgroundColor), - }), - [backgroundColor, UNSAFE_style] - ); +export const View = forwardRef, ViewProps>( + (props, forwardedRef): JSX.Element => { + const { backgroundColor, UNSAFE_style, ...rest } = props; + const style = useMemo( + () => ({ + ...UNSAFE_style, + backgroundColor: colorValueStyle(backgroundColor), + }), + [backgroundColor, UNSAFE_style] + ); - return ; -} + return ; + } +); + +View.displayName = 'View'; export default View;