From 53e73b5e57e27a0b991570a2ef27981f56941ccc Mon Sep 17 00:00:00 2001 From: Charles de Dreuille Date: Fri, 6 Dec 2024 17:26:33 +0000 Subject: [PATCH] Add forwardRef everywhere Signed-off-by: Charles de Dreuille --- .../components/LayoutComponents/index.tsx | 2 +- packages/canon/src/components/Box/Box.tsx | 7 ++--- packages/canon/src/components/Grid/Grid.tsx | 27 ++++++++++++++----- .../canon/src/components/Inline/Inline.tsx | 7 ++--- packages/canon/src/components/Stack/Stack.tsx | 7 ++--- 5 files changed, 34 insertions(+), 16 deletions(-) diff --git a/packages/canon/docs/components/LayoutComponents/index.tsx b/packages/canon/docs/components/LayoutComponents/index.tsx index 16a7a1726c..92fab7c47f 100644 --- a/packages/canon/docs/components/LayoutComponents/index.tsx +++ b/packages/canon/docs/components/LayoutComponents/index.tsx @@ -60,7 +60,7 @@ export const LayoutComponents = () => {
Arrange your components in a row
- +
Container
diff --git a/packages/canon/src/components/Box/Box.tsx b/packages/canon/src/components/Box/Box.tsx index d134abff35..a470c0584d 100644 --- a/packages/canon/src/components/Box/Box.tsx +++ b/packages/canon/src/components/Box/Box.tsx @@ -14,13 +14,13 @@ * limitations under the License. */ -import { createElement } from 'react'; +import { createElement, forwardRef } from 'react'; import { boxSprinkles } from './sprinkles.css'; import { base } from './box.css'; import { BoxProps } from './types'; /** @public */ -export const Box = (props: BoxProps) => { +export const Box = forwardRef((props, ref) => { const { as = 'div', className, style, children, ...restProps } = props; // Generate the list of class names @@ -32,8 +32,9 @@ export const Box = (props: BoxProps) => { .join(' '); return createElement(as, { + ref, className: classNames, style, children, }); -}; +}); diff --git a/packages/canon/src/components/Grid/Grid.tsx b/packages/canon/src/components/Grid/Grid.tsx index 8e426ff49b..51f60afe27 100644 --- a/packages/canon/src/components/Grid/Grid.tsx +++ b/packages/canon/src/components/Grid/Grid.tsx @@ -14,12 +14,18 @@ * limitations under the License. */ -import { createElement } from 'react'; +import { createElement, forwardRef } from 'react'; import { GridItemProps, GridProps } from './types'; import { gridItemSprinkles, gridSprinkles } from './sprinkles.css'; +type GridComponent = React.ForwardRefExoticComponent< + GridProps & React.RefAttributes +> & { + Item: typeof GridItem; +}; + /** @public */ -export const Grid = (props: GridProps) => { +export const Grid = forwardRef((props, ref) => { const { children, columns, @@ -42,14 +48,15 @@ export const Grid = (props: GridProps) => { return createElement( 'div', { + ref, className: classNames, style, }, children, ); -}; +}) as GridComponent; -const GridItem = (props: GridItemProps) => { +const GridItem = forwardRef((props, ref) => { const { children, rowSpan, colSpan, start, end, className, style } = props; const sprinklesClassName = gridItemSprinkles({ @@ -63,7 +70,15 @@ const GridItem = (props: GridItemProps) => { .filter(Boolean) .join(' '); - return createElement('div', { className: classNames, style }, children); -}; + return createElement( + 'div', + { + ref, + className: classNames, + style, + }, + children, + ); +}); Grid.Item = GridItem; diff --git a/packages/canon/src/components/Inline/Inline.tsx b/packages/canon/src/components/Inline/Inline.tsx index a46d4dc948..b583b2ae08 100644 --- a/packages/canon/src/components/Inline/Inline.tsx +++ b/packages/canon/src/components/Inline/Inline.tsx @@ -14,7 +14,7 @@ * limitations under the License. */ -import { createElement } from 'react'; +import { createElement, forwardRef } from 'react'; import { inlineSprinkles } from './sprinkles.css'; import type { InlineProps } from './types'; @@ -33,7 +33,7 @@ const alignToFlexAlignY = (align: InlineProps['align']) => { }; /** @public */ -export const Inline = (props: InlineProps) => { +export const Inline = forwardRef((props, ref) => { const { as = 'div', children, @@ -59,8 +59,9 @@ export const Inline = (props: InlineProps) => { .join(' '); return createElement(as, { + ref, className: classNames, style, children, }); -}; +}); diff --git a/packages/canon/src/components/Stack/Stack.tsx b/packages/canon/src/components/Stack/Stack.tsx index 85a67db05d..e0c36f2043 100644 --- a/packages/canon/src/components/Stack/Stack.tsx +++ b/packages/canon/src/components/Stack/Stack.tsx @@ -14,7 +14,7 @@ * limitations under the License. */ -import { createElement } from 'react'; +import { createElement, forwardRef } from 'react'; import { StackProps } from './types'; import { stackSprinkles } from './sprinkles.css'; @@ -26,7 +26,7 @@ const alignToFlexAlign = (align: StackProps['align']) => { }; /** @public */ -export const Stack = (props: StackProps) => { +export const Stack = forwardRef((props, ref) => { const { as = 'div', children, @@ -53,8 +53,9 @@ export const Stack = (props: StackProps) => { .join(' '); return createElement(as, { + ref, className: classNames, style, children, }); -}; +});