Skip to content

Commit 6176f03

Browse files
Gururajj77heloiseluiriddhybansaltay1orjones
authored
feat: added polymorphic props support for column (#18846)
* feat: added polymorphic props sucpport for column * chore: removed ref from cssgrid * chore: changed names * fix: changed polymorphic props implementation * feat: added test story * Update packages/react/src/components/Grid/Column.tsx --------- Co-authored-by: Heloise Lui <71858203+heloiselui@users.noreply.github.com> Co-authored-by: Riddhi Bansal <41935566+riddhybansal@users.noreply.github.com> Co-authored-by: Taylor Jones <tay1orjones@users.noreply.github.com>
1 parent 1357352 commit 6176f03

File tree

3 files changed

+58
-20
lines changed

3 files changed

+58
-20
lines changed

packages/react/__tests__/__snapshots__/PublicAPI-test.js.snap

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -899,6 +899,7 @@ Map {
899899
},
900900
},
901901
"Column" => Object {
902+
"$$typeof": Symbol(react.forward_ref),
902903
"propTypes": Object {
903904
"as": Object {
904905
"args": Array [
@@ -1246,6 +1247,7 @@ Map {
12461247
"type": "oneOfType",
12471248
},
12481249
},
1250+
"render": [Function],
12491251
},
12501252
"ColumnHang" => Object {
12511253
"propTypes": Object {

packages/react/src/components/Grid/Column.tsx

Lines changed: 26 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ import React from 'react';
1212
import { usePrefix } from '../../internal/usePrefix';
1313
import { PolymorphicProps } from '../../types/common';
1414
import { useGridSettings } from './GridContext';
15+
import {
16+
PolymorphicComponentPropWithRef,
17+
PolymorphicRef,
18+
} from '../../internal/PolymorphicProps';
1519

1620
type ColumnSpanPercent = '25%' | '50%' | '75%' | '100%';
1721

@@ -87,10 +91,8 @@ export interface ColumnBaseProps {
8791
span?: ColumnSpan;
8892
}
8993

90-
export type ColumnProps<T extends React.ElementType> = PolymorphicProps<
91-
T,
92-
ColumnBaseProps
93-
>;
94+
export type ColumnProps<T extends React.ElementType> =
95+
PolymorphicComponentPropWithRef<T, ColumnBaseProps>;
9496

9597
export interface ColumnComponent {
9698
<T extends React.ElementType>(
@@ -99,19 +101,25 @@ export interface ColumnComponent {
99101
): React.ReactElement<any, any> | null;
100102
}
101103

102-
function Column<T extends React.ElementType>({
103-
as: BaseComponent = 'div' as T,
104-
children,
105-
className: customClassName,
106-
sm,
107-
md,
108-
lg,
109-
xlg,
110-
max,
111-
...rest
112-
}: ColumnProps<T>) {
104+
const Column = React.forwardRef(function Column<
105+
T extends React.ElementType = 'div',
106+
>(
107+
{
108+
as,
109+
children,
110+
className: customClassName,
111+
sm,
112+
md,
113+
lg,
114+
xlg,
115+
max,
116+
...rest
117+
}: ColumnProps<T>,
118+
ref?: PolymorphicRef<T>
119+
) {
113120
const { mode } = useGridSettings();
114121
const prefix = usePrefix();
122+
const BaseComponent = as || 'div';
115123

116124
if (mode === 'css-grid') {
117125
return (
@@ -137,14 +145,12 @@ function Column<T extends React.ElementType>({
137145
[`${prefix}--col`]: columnClassName.length === 0,
138146
});
139147

140-
// cast as any to let TypeScript allow passing in attributes to base component
141-
const BaseComponentAsAny: any = BaseComponent;
142148
return (
143-
<BaseComponentAsAny className={className} {...rest}>
149+
<BaseComponent className={className} ref={ref} {...rest}>
144150
{children}
145-
</BaseComponentAsAny>
151+
</BaseComponent>
146152
);
147-
}
153+
});
148154

149155
const percentSpanType = PropTypes.oneOf(['25%', '50%', '75%', '100%']);
150156

packages/react/src/components/Grid/FlexGrid.stories.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -418,6 +418,36 @@ export const Default = (args) => {
418418
);
419419
};
420420

421+
export const Test1 = (args) => {
422+
// Grab the style from here to see the visual example
423+
//https://github.com/carbon-design-system/carbon/blob/main/packages/react/src/components/Grid/FlexGrid.stories.scss
424+
function DemoContent({ children }) {
425+
return (
426+
<div className="outside">
427+
<div className="inside">{children}</div>
428+
</div>
429+
);
430+
}
431+
return (
432+
<FlexGrid {...args} as="section">
433+
<Row>
434+
<Column as="section">
435+
<DemoContent>1/4</DemoContent>
436+
</Column>
437+
<Column>
438+
<DemoContent>1/4</DemoContent>
439+
</Column>
440+
<Column>
441+
<DemoContent>1/4</DemoContent>
442+
</Column>
443+
<Column>
444+
<DemoContent>1/4</DemoContent>
445+
</Column>
446+
</Row>
447+
</FlexGrid>
448+
);
449+
};
450+
421451
Default.args = {
422452
as: 'div',
423453
fullWidth: false,

0 commit comments

Comments
 (0)