Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: adds support for raw and content object in ExpandableTable #122

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion packages/react-expandable-table/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,11 @@ export const ItemWrapper = styled(
key={column.key}
bold={column.bold}
>
<Ellipsis>{data[column.key]}</Ellipsis>
<Ellipsis>
{typeof data[column.key] === 'object'
? data[column.key].content
: data[column.key]}
</Ellipsis>
</ColumnCell>
))}
<ColumnCell width={`${ARROW_CELL_WIDTH}px`} align="right">
Expand Down
5 changes: 4 additions & 1 deletion packages/react-expandable-table/src/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,10 @@ initialState = {
},
{
id: '5',
name: 'Hulk',
name: {
raw: 'Hulk',
content: <span style={{ color: 'gray' }}>Hulk</span>,
},
rank: '5',
mentions: '35',
kol_score: '99.10%',
Expand Down
52 changes: 49 additions & 3 deletions packages/react-expandable-table/src/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -288,16 +288,35 @@ it('clicking on name in the header should sort data according to names in descen
...data,
{
id: '5',
name: 'Jamie Dimon',
name: {
raw: 'Iron Manuel',
content: <span>Iron Manuel</span>,
},
rank: '6',
mentions: '34',
kol_score: '0.00%',
reach: '0.00%',
score: 300,
},
{
id: '6',
name: {
raw: 'Jamie Dimon',
content: <span>Jamie Dimon</span>,
},
rank: '5',
mentions: '35',
kol_score: '99.10%',
reach: '99.10%',
score: 250,
},

{
id: '6',
name: 'Agustín Carstens',
id: '7',
name: {
raw: 'Agustín Carstens',
content: <span>Agustín Carstens</span>,
},
rank: '6',
mentions: '34',
kol_score: '0.00%',
Expand All @@ -316,6 +335,7 @@ it('clicking on name in the header should sort data according to names in descen
expect(wrapper.find('Row').map(node => node.text())).toMatchInlineSnapshot(`
Array [
"Jamie Dimon53599.10%99.10%angle_down",
"Iron Manuel6340.00%0.00%angle_down",
"Iron Man1993.70%93.60%angle_down",
"Captain America23899.70%99.45%angle_down",
"Agustín Carstens6340.00%0.00%angle_down",
Expand Down Expand Up @@ -502,3 +522,29 @@ it('checks for tooltip presence', () => {
infoIcon.find('button').simulate('mouseenter');
expect(open).toHaveBeenCalled();
});

it('content should be rendered when provided', () => {
const wrapper = mount(
<ExpandableTable
maxBodyHeight={300}
renderRow={props => <ExampleExtendedComponent {...props} />}
columns={columns}
data={[
...data,
{
id: '2',
name: {
raw: 'Hulk',
content: <span data-context="hulk">Hulk</span>,
},
rank: '2',
mentions: '38',
kol_score: '99.70%',
reach: '99.45%',
},
]}
/>
);

expect(wrapper.find('[data-context="hulk"]').exists()).toBe(true);
});
10 changes: 8 additions & 2 deletions packages/react-expandable-table/src/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,19 @@
* LICENSE file in the root directory of this source tree.
*/
// @flow
import * as React from 'react';
export type ID = string | number;

export type Label = string | number;
export const ASC: 'asc' = 'asc';
export const DESC: 'desc' = 'desc';
export type SortOrder = Array<typeof ASC | typeof DESC | null>;

export type Cell = {
raw: Label,
content: React.Node,
};

export type Data = {
id: ID,
[string]: any,
[string]: Label | Cell,
};
8 changes: 6 additions & 2 deletions packages/react-expandable-table/src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,12 @@ export const getSortedData = (
): Array<Data> => {
if (data.length && sortOrder && sortBy) {
return [...data].sort((a, b) => {
const valueA = a[sortBy];
const valueB = b[sortBy];
const valueA = String(
typeof a[sortBy] === 'object' ? a[sortBy].raw : a[sortBy]
);
const valueB = String(
typeof b[sortBy] === 'object' ? b[sortBy].raw : b[sortBy]
);
const parsedA = parseFloat(valueA);
const parsedB = parseFloat(valueB);
if (isNaN(parsedA) === false && isNaN(parsedB) === false) {
Expand Down