Skip to content

Commit 8e4084b

Browse files
author
Gabriel Mičko
authored
feat: adds support for raw and content object in ExpandableTable (#122)
* feat: adds support for raw and content object in ExpandableTable
1 parent 7cf0e74 commit 8e4084b

File tree

5 files changed

+72
-9
lines changed

5 files changed

+72
-9
lines changed

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,11 @@ export const ItemWrapper = styled(
124124
key={column.key}
125125
bold={column.bold}
126126
>
127-
<Ellipsis>{data[column.key]}</Ellipsis>
127+
<Ellipsis>
128+
{typeof data[column.key] === 'object'
129+
? data[column.key].content
130+
: data[column.key]}
131+
</Ellipsis>
128132
</ColumnCell>
129133
))}
130134
<ColumnCell width={`${ARROW_CELL_WIDTH}px`} align="right">

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,10 @@ initialState = {
8686
},
8787
{
8888
id: '5',
89-
name: 'Hulk',
89+
name: {
90+
raw: 'Hulk',
91+
content: <span style={{ color: 'gray' }}>Hulk</span>,
92+
},
9093
rank: '5',
9194
mentions: '35',
9295
kol_score: '99.10%',

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

Lines changed: 49 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -288,16 +288,35 @@ it('clicking on name in the header should sort data according to names in descen
288288
...data,
289289
{
290290
id: '5',
291-
name: 'Jamie Dimon',
291+
name: {
292+
raw: 'Iron Manuel',
293+
content: <span>Iron Manuel</span>,
294+
},
295+
rank: '6',
296+
mentions: '34',
297+
kol_score: '0.00%',
298+
reach: '0.00%',
299+
score: 300,
300+
},
301+
{
302+
id: '6',
303+
name: {
304+
raw: 'Jamie Dimon',
305+
content: <span>Jamie Dimon</span>,
306+
},
292307
rank: '5',
293308
mentions: '35',
294309
kol_score: '99.10%',
295310
reach: '99.10%',
296311
score: 250,
297312
},
313+
298314
{
299-
id: '6',
300-
name: 'Agustín Carstens',
315+
id: '7',
316+
name: {
317+
raw: 'Agustín Carstens',
318+
content: <span>Agustín Carstens</span>,
319+
},
301320
rank: '6',
302321
mentions: '34',
303322
kol_score: '0.00%',
@@ -316,6 +335,7 @@ it('clicking on name in the header should sort data according to names in descen
316335
expect(wrapper.find('Row').map(node => node.text())).toMatchInlineSnapshot(`
317336
Array [
318337
"Jamie Dimon53599.10%99.10%angle_down",
338+
"Iron Manuel6340.00%0.00%angle_down",
319339
"Iron Man1993.70%93.60%angle_down",
320340
"Captain America23899.70%99.45%angle_down",
321341
"Agustín Carstens6340.00%0.00%angle_down",
@@ -502,3 +522,29 @@ it('checks for tooltip presence', () => {
502522
infoIcon.find('button').simulate('mouseenter');
503523
expect(open).toHaveBeenCalled();
504524
});
525+
526+
it('content should be rendered when provided', () => {
527+
const wrapper = mount(
528+
<ExpandableTable
529+
maxBodyHeight={300}
530+
renderRow={props => <ExampleExtendedComponent {...props} />}
531+
columns={columns}
532+
data={[
533+
...data,
534+
{
535+
id: '2',
536+
name: {
537+
raw: 'Hulk',
538+
content: <span data-context="hulk">Hulk</span>,
539+
},
540+
rank: '2',
541+
mentions: '38',
542+
kol_score: '99.70%',
543+
reach: '99.45%',
544+
},
545+
]}
546+
/>
547+
);
548+
549+
expect(wrapper.find('[data-context="hulk"]').exists()).toBe(true);
550+
});

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

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,19 @@
55
* LICENSE file in the root directory of this source tree.
66
*/
77
// @flow
8+
import * as React from 'react';
89
export type ID = string | number;
9-
10+
export type Label = string | number;
1011
export const ASC: 'asc' = 'asc';
1112
export const DESC: 'desc' = 'desc';
1213
export type SortOrder = Array<typeof ASC | typeof DESC | null>;
1314

15+
export type Cell = {
16+
raw: Label,
17+
content: React.Node,
18+
};
19+
1420
export type Data = {
1521
id: ID,
16-
[string]: any,
22+
[string]: Label | Cell,
1723
};

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,12 @@ export const getSortedData = (
1414
): Array<Data> => {
1515
if (data.length && sortOrder && sortBy) {
1616
return [...data].sort((a, b) => {
17-
const valueA = a[sortBy];
18-
const valueB = b[sortBy];
17+
const valueA = String(
18+
typeof a[sortBy] === 'object' ? a[sortBy].raw : a[sortBy]
19+
);
20+
const valueB = String(
21+
typeof b[sortBy] === 'object' ? b[sortBy].raw : b[sortBy]
22+
);
1923
const parsedA = parseFloat(valueA);
2024
const parsedB = parseFloat(valueB);
2125
if (isNaN(parsedA) === false && isNaN(parsedB) === false) {

0 commit comments

Comments
 (0)