Skip to content

Commit

Permalink
forwardedRefs (deephaven#1909)
Browse files Browse the repository at this point in the history
  • Loading branch information
bmingles committed Apr 2, 2024
1 parent 2f55e91 commit e3b3857
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 26 deletions.
14 changes: 10 additions & 4 deletions packages/components/src/spectrum/Heading.tsx
Original file line number Diff line number Diff line change
@@ -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 & {
Expand All @@ -20,7 +21,10 @@ export type HeadingProps = SpectrumHeadingProps & {
*
*/

export function Heading(props: HeadingProps): JSX.Element {
export const Heading = forwardRef<
DOMRefValue<HTMLHeadingElement>,
HeadingProps
>((props, forwardedRef): JSX.Element => {
const { color, UNSAFE_style, ...rest } = props;
const style = useMemo(
() => ({
Expand All @@ -30,7 +34,9 @@ export function Heading(props: HeadingProps): JSX.Element {
[color, UNSAFE_style]
);

return <SpectrumHeading {...rest} UNSAFE_style={style} />;
}
return <SpectrumHeading {...rest} ref={forwardedRef} UNSAFE_style={style} />;
});

Heading.displayName = 'Heading';

export default Heading;
14 changes: 4 additions & 10 deletions packages/components/src/spectrum/Text.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 & {
Expand All @@ -20,8 +20,8 @@ export type TextProps = SpectrumTextProps & {
* @returns The Text component
*
*/
export const Text = forwardRef<DOMRefValue<HTMLSpanElement>, SpectrumTextProps>(
(props: TextProps, ref): JSX.Element => {
export const Text = forwardRef<DOMRefValue<HTMLSpanElement>, TextProps>(
(props, forwardedRef): JSX.Element => {
const { color, UNSAFE_style, ...rest } = props;
const style = useMemo(
() => ({
Expand All @@ -31,13 +31,7 @@ export const Text = forwardRef<DOMRefValue<HTMLSpanElement>, SpectrumTextProps>(
[color, UNSAFE_style]
);

return (
<SpectrumText
{...rest}
ref={ref as unknown as DOMRef<HTMLDivElement>}
UNSAFE_style={style}
/>
);
return <SpectrumText {...rest} ref={forwardedRef} UNSAFE_style={style} />;
}
);

Expand Down
29 changes: 17 additions & 12 deletions packages/components/src/spectrum/View.tsx
Original file line number Diff line number Diff line change
@@ -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<SpectrumViewProps<6>, 'backgroundColor'> & {
Expand All @@ -20,17 +21,21 @@ export type ViewProps = Omit<SpectrumViewProps<6>, '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<DOMRefValue<HTMLElement>, ViewProps>(
(props, forwardedRef): JSX.Element => {
const { backgroundColor, UNSAFE_style, ...rest } = props;
const style = useMemo(
() => ({
...UNSAFE_style,
backgroundColor: colorValueStyle(backgroundColor),
}),
[backgroundColor, UNSAFE_style]
);

return <SpectrumView {...rest} UNSAFE_style={style} />;
}
return <SpectrumView {...rest} ref={forwardedRef} UNSAFE_style={style} />;
}
);

View.displayName = 'View';

export default View;

0 comments on commit e3b3857

Please sign in to comment.