Skip to content

Commit 7c79bb2

Browse files
kraenhansenTheSonOfThompshaneeza
authored
[LG-5589] fix(table): add type assert to allow passing row to ExtendedContent (#3176)
* Add type assert to allow passing row to ExtendedContent * Update stories.testutils.tsx * Update getTestUtils.spec.tsx * fix(table): Enhance ExpandedContent tests with type assertions for styled components --------- Co-authored-by: Adam Thompson <[email protected]> Co-authored-by: Shaneeza <[email protected]>
1 parent 88e25a1 commit 7c79bb2

File tree

5 files changed

+26
-19
lines changed

5 files changed

+26
-19
lines changed

.changeset/sixty-trees-rescue.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@leafygreen-ui/table': patch
3+
---
4+
5+
Fix ExtendedContent types to allow passing row prop

packages/table/src/ExpandedContent/ExpandedContent.spec.tsx

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { Fragment } from 'react';
1+
import React, { ComponentType, Fragment } from 'react';
22
import styled from '@emotion/styled';
33
import { flexRender } from '@tanstack/react-table';
44
import { render } from '@testing-library/react';
@@ -16,7 +16,7 @@ import {
1616
useMockTestRowData,
1717
useTestHookCall,
1818
} from '../utils/testHookCalls.testutils';
19-
import { Table, TableProps } from '..';
19+
import { ExpandedContentProps, Table, TableProps } from '..';
2020

2121
import ExpandedContent from './ExpandedContent';
2222

@@ -60,7 +60,6 @@ const RowWithExpandableContent = (args: TableProps<Person>) => {
6060
})}
6161
</Row>
6262
)}
63-
{/* @ts-expect-error FIXME: ExpandedContent is incorrectly generic */}
6463
{isExpandedContent && <ExpandedContent row={row} />}
6564
</Fragment>
6665
);
@@ -129,12 +128,15 @@ describe('packages/table/Row/ExpandableContent', () => {
129128
const { result } = renderHook(() => useMockTestRowData());
130129
const mockRow = result.current.firstRow;
131130

132-
const StyledExpandedContent = styled(ExpandedContent)`
131+
// type assertion is needed in R17 with R16 types(@types/react 16.9.56)
132+
// styled is not preserving the required row prop
133+
const StyledExpandedContent = styled(
134+
ExpandedContent as ComponentType<ExpandedContentProps<any>>,
135+
)`
133136
color: #69ffc6;
134-
`;
137+
` as typeof ExpandedContent;
135138

136139
const { getByTestId } = render(
137-
// @ts-expect-error ExpandedContent is incorrectly generic FIXME:
138140
<StyledExpandedContent row={mockRow} data-testid="styled" />,
139141
);
140142

@@ -149,14 +151,17 @@ describe('packages/table/Row/ExpandableContent', () => {
149151
const { result } = renderHook(() => useMockTestRowData());
150152
const mockRow = result.current.firstRow;
151153

152-
const StyledExpandedContent = styled(ExpandedContent)<StyledProps>`
154+
// type assertion is needed in R17 with R16 types(@types/react 16.9.56)
155+
// styled is not preserving the required row prop
156+
const StyledExpandedContent = styled(
157+
ExpandedContent as ComponentType<ExpandedContentProps<any>>,
158+
)<StyledProps>`
153159
color: ${props => props.color};
154-
`;
160+
` as typeof ExpandedContent;
155161

156162
const { getByTestId } = render(
157163
<StyledExpandedContent
158164
data-testid="styled"
159-
// @ts-expect-error ExpandedContent is incorrectly generic FIXME:
160165
row={mockRow}
161166
color="#69ffc6"
162167
/>,
@@ -175,15 +180,9 @@ describe('packages/table/Row/ExpandableContent', () => {
175180
<>
176181
{/* @ts-expect-error - row is missing */}
177182
<ExpandedContent />
178-
179-
{/* @ts-expect-error - ExpandedContent is incorrectly generic */}
180183
<ExpandedContent row={firstRow} />
181-
{/* @ts-expect-error - ExpandedContent is incorrectly generic */}
182184
<ExpandedContent row={firstRow} ref={ref} />
183-
184-
{/* @ts-expect-error - ExpandedContent is incorrectly generic */}
185185
<ExpandedContent row={firstRow} virtualRow={firstVirtualRow} />
186-
{/* @ts-expect-error - ExpandedContent is incorrectly generic */}
187186
<ExpandedContent row={firstRow} virtualRow={firstVirtualRow} ref={ref} />
188187
</>;
189188
});

packages/table/src/ExpandedContent/ExpandedContent.tsx

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,10 @@ import {
1212
baseStyles,
1313
expandedContentThemeStyles,
1414
} from './ExpandedContent.styles';
15-
import { ExpandedContentProps } from './ExpandedContent.types';
15+
import {
16+
ExpandedContentComponentType,
17+
ExpandedContentProps,
18+
} from './ExpandedContent.types';
1619

1720
const ExpandedContentWithRef = <T extends RowData>(
1821
{ row, virtualRow, ...rest }: ExpandedContentProps<T>,
@@ -48,7 +51,9 @@ const ExpandedContentWithRef = <T extends RowData>(
4851

4952
// React.forwardRef can only work with plain function types, i.e. types with a single call signature and no other members.
5053
// This assertion has an interface that restores the original function signature to work with generics.
51-
export const ExpandedContent = React.forwardRef(ExpandedContentWithRef);
54+
export const ExpandedContent = React.forwardRef(
55+
ExpandedContentWithRef,
56+
) as ExpandedContentComponentType;
5257

5358
ExpandedContent.displayName = 'ExpandedContent';
5459

packages/table/src/testing/getTestUtils.spec.tsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,6 @@ function TableWithHook(props: TestTableWithHookProps) {
7878
})}
7979
</Row>
8080
)}
81-
{/* @ts-expect-error FIXME: ExpandedContent is incorrectly generic */}
8281
{isExpandedContent && <ExpandedContent row={row} />}
8382
</Fragment>
8483
);

packages/table/src/utils/stories.testutils.tsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,6 @@ export const DynamicDataComponent: StoryFn<StoryTableProps> = args => {
127127
})}
128128
</Row>
129129
)}
130-
{/* @ts-expect-error FIXME: ExpandedContent is incorrectly generic<unknown> */}
131130
{isExpandedContent && <ExpandedContent row={row} />}
132131
</Fragment>
133132
);

0 commit comments

Comments
 (0)