Skip to content

Commit f712875

Browse files
author
Gabriel Mičko
authored
fix: backwards compatibility support for react node for the cell (#123)
* fix: backwards compatibility support for react node for the cell * chore: adds warning for unsupported type in ET data, adds test
1 parent df9b089 commit f712875

File tree

4 files changed

+44
-11
lines changed

4 files changed

+44
-11
lines changed

packages/react-expandable-table/src/index.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ import {
2929
getSortedData,
3030
onKeyboardSelect,
3131
filterDataForPagination,
32+
isCell,
33+
checkUnsupportedCellType,
3234
} from './utils';
3335

3436
type Column = {
@@ -125,7 +127,7 @@ export const ItemWrapper = styled(
125127
bold={column.bold}
126128
>
127129
<Ellipsis>
128-
{typeof data[column.key] === 'object'
130+
{isCell(data[column.key]) && data[column.key].content != null
129131
? data[column.key].content
130132
: data[column.key]}
131133
</Ellipsis>
@@ -168,6 +170,11 @@ const ExpandableTable = ({
168170
renderRow,
169171
...props
170172
}: Props) => {
173+
if (checkUnsupportedCellType(data)) {
174+
console.warn(
175+
'[react-expandable-table]: Unsupported cell type in data provided. Please use cell.raw & cell.content instead (example: https://ui.quid.com/#!/ExpandableTable).'
176+
);
177+
}
171178
const [sorting, setSorting] = React.useState({
172179
key: null,
173180
sort: null,

packages/react-expandable-table/src/index.test.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -523,7 +523,7 @@ it('checks for tooltip presence', () => {
523523
expect(open).toHaveBeenCalled();
524524
});
525525

526-
it('content should be rendered when provided', () => {
526+
it('content with object or node should be rendered when provided', () => {
527527
const wrapper = mount(
528528
<ExpandableTable
529529
maxBodyHeight={300}
@@ -537,7 +537,7 @@ it('content should be rendered when provided', () => {
537537
raw: 'Hulk',
538538
content: <span data-context="hulk">Hulk</span>,
539539
},
540-
rank: '2',
540+
rank: <span data-context="rank">2</span>,
541541
mentions: '38',
542542
kol_score: '99.70%',
543543
reach: '99.45%',
@@ -547,4 +547,6 @@ it('content should be rendered when provided', () => {
547547
);
548548

549549
expect(wrapper.find('[data-context="hulk"]').exists()).toBe(true);
550+
//NOTE(gabrielmicko): to be deprecated soon
551+
expect(wrapper.find('[data-context="rank"]').exists()).toBe(true);
550552
});

packages/react-expandable-table/src/types.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,18 @@
77
// @flow
88
import * as React from 'react';
99
export type ID = string | number;
10-
export type Label = string | number;
1110
export const ASC: 'asc' = 'asc';
1211
export const DESC: 'desc' = 'desc';
1312
export type SortOrder = Array<typeof ASC | typeof DESC | null>;
1413

15-
export type Cell = {
16-
raw: Label,
14+
export type CellObject = {|
15+
raw: string | number,
1716
content: React.Node,
18-
};
17+
|};
18+
19+
export type Cell = CellObject | React.Node;
1920

2021
export type Data = {
2122
id: ID,
22-
[string]: Label | Cell,
23+
[string]: Cell,
2324
};

packages/react-expandable-table/src/utils.js

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
* LICENSE file in the root directory of this source tree.
66
*/
77
// @flow
8-
import { ASC, DESC, type Data } from './types';
8+
import { ASC, DESC, type Data, type Cell } from './types';
99

1010
export const getSortedData = (
1111
data: Array<Data>,
@@ -15,10 +15,10 @@ export const getSortedData = (
1515
if (data.length && sortOrder && sortBy) {
1616
return [...data].sort((a, b) => {
1717
const valueA = String(
18-
typeof a[sortBy] === 'object' ? a[sortBy].raw : a[sortBy]
18+
isCell(a[sortBy]) && a[sortBy].raw != null ? a[sortBy].raw : a[sortBy]
1919
);
2020
const valueB = String(
21-
typeof b[sortBy] === 'object' ? b[sortBy].raw : b[sortBy]
21+
isCell(b[sortBy]) && b[sortBy].raw != null ? b[sortBy].raw : b[sortBy]
2222
);
2323
const parsedA = parseFloat(valueA);
2424
const parsedB = parseFloat(valueB);
@@ -57,3 +57,26 @@ export const filterDataForPagination = (
5757
}
5858
return data;
5959
};
60+
61+
export const isCell = (value: Cell): boolean %checks => {
62+
return (
63+
value != null &&
64+
typeof value === 'object' &&
65+
value.hasOwnProperty('raw') &&
66+
value.hasOwnProperty('content')
67+
);
68+
};
69+
70+
//NOTE(gabrielmicko): Supporting React.Node for the cell directly is deprecated.
71+
//We have a check that ensures it stays working, but will be removed in the future.
72+
//In case React.Node is needed we encourage using CellObject instead.
73+
export const checkUnsupportedCellType = (data: Array<Data>): boolean =>
74+
data.some(row =>
75+
Object.values(row).some(
76+
value =>
77+
value != null &&
78+
typeof value === 'object' &&
79+
(value.hasOwnProperty('raw') === false ||
80+
value.hasOwnProperty('content') === false)
81+
)
82+
);

0 commit comments

Comments
 (0)