Skip to content

Commit b89f80c

Browse files
committed
[Suggested refactor] DRY out misc popover content utils
- DefaultColumnFormatter is repeated in DataGridBody and DataGridFooterRow - Moving the fns to their own util file lets us write unit tests for them instead of having to dive into props and also removes extra complexity from the already-long DataGridBody file
1 parent b1f86d6 commit b89f80c

5 files changed

Lines changed: 103 additions & 55 deletions

File tree

src/components/datagrid/body/data_grid_body.tsx

Lines changed: 4 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -23,25 +23,25 @@ import {
2323
VariableSizeGridProps,
2424
} from 'react-window';
2525
import tabbable from 'tabbable';
26-
import { EuiCodeBlock } from '../../code';
2726
import {
2827
EuiMutationObserver,
2928
useMutationObserver,
3029
} from '../../observer/mutation_observer';
3130
import { useResizeObserver } from '../../observer/resize_observer';
32-
import { EuiText } from '../../text';
3331
import { EuiDataGridCell } from './data_grid_cell';
3432
import {
3533
DataGridSortingContext,
3634
DataGridWrapperRowsContext,
3735
} from '../data_grid_context';
3836
import { EuiDataGridFooterRow } from './data_grid_footer_row';
3937
import { EuiDataGridHeaderRow } from './header';
38+
import {
39+
DefaultColumnFormatter,
40+
providedPopoverContents,
41+
} from './popover_utils';
4042
import {
4143
EuiDataGridBodyProps,
4244
EuiDataGridInMemoryValues,
43-
EuiDataGridPopoverContent,
44-
EuiDataGridPopoverContents,
4545
EuiDataGridSchemaDetector,
4646
} from '../data_grid_types';
4747

@@ -55,32 +55,6 @@ const defaultComparator: NonNullable<
5555
return 0;
5656
};
5757

58-
const providedPopoverContents: EuiDataGridPopoverContents = {
59-
json: ({ cellContentsElement }) => {
60-
let formattedText = cellContentsElement.innerText;
61-
62-
// attempt to pretty-print the json
63-
try {
64-
formattedText = JSON.stringify(JSON.parse(formattedText), null, 2);
65-
} catch (e) {} // eslint-disable-line no-empty
66-
67-
return (
68-
<EuiCodeBlock
69-
isCopyable
70-
transparentBackground
71-
paddingSize="none"
72-
language="json"
73-
>
74-
{formattedText}
75-
</EuiCodeBlock>
76-
);
77-
},
78-
};
79-
80-
const DefaultColumnFormatter: EuiDataGridPopoverContent = ({ children }) => {
81-
return <EuiText>{children}</EuiText>;
82-
};
83-
8458
const Cell: FunctionComponent<GridChildComponentProps> = ({
8559
columnIndex,
8660
rowIndex: visibleRowIndex,

src/components/datagrid/body/data_grid_footer_row.test.tsx

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -151,20 +151,4 @@ describe('EuiDataGridFooterRow', () => {
151151
.prop('renderCellValue');
152152
expect(renderCellValue()).toEqual(null);
153153
});
154-
155-
describe('DefaultColumnFormatter', () => {
156-
it('wraps content with EuiText', () => {
157-
const component = shallow(<EuiDataGridFooterRow {...requiredProps} />);
158-
const popoverContent: Function = component
159-
.find('EuiDataGridCell')
160-
.first()
161-
.prop('popoverContent');
162-
163-
expect(popoverContent({ children: 'test' })).toMatchInlineSnapshot(`
164-
<EuiText>
165-
test
166-
</EuiText>
167-
`);
168-
});
169-
});
170154
});

src/components/datagrid/body/data_grid_footer_row.tsx

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,9 @@
88

99
import classnames from 'classnames';
1010
import React, { forwardRef, memo } from 'react';
11-
import { EuiText } from '../../text';
1211
import { EuiDataGridCell } from './data_grid_cell';
13-
import {
14-
EuiDataGridFooterRowProps,
15-
EuiDataGridPopoverContent,
16-
} from '../data_grid_types';
17-
18-
const DefaultColumnFormatter: EuiDataGridPopoverContent = ({ children }) => {
19-
return <EuiText>{children}</EuiText>;
20-
};
12+
import { DefaultColumnFormatter } from './popover_utils';
13+
import { EuiDataGridFooterRowProps } from '../data_grid_types';
2114

2215
const EuiDataGridFooterRow = memo(
2316
forwardRef<HTMLDivElement, EuiDataGridFooterRowProps>(
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the Elastic License
4+
* 2.0 and the Server Side Public License, v 1; you may not use this file except
5+
* in compliance with, at your election, the Elastic License 2.0 or the Server
6+
* Side Public License, v 1.
7+
*/
8+
9+
import React from 'react';
10+
import { shallow } from 'enzyme';
11+
12+
import {
13+
DefaultColumnFormatter,
14+
providedPopoverContents,
15+
} from './popover_utils';
16+
17+
describe('popover utils', () => {
18+
const cellContentsElement = document.createElement('div');
19+
cellContentsElement.innerText = '{ "hello": "world" }';
20+
21+
test('DefaultColumnFormatter', () => {
22+
const component = shallow(
23+
<DefaultColumnFormatter cellContentsElement={cellContentsElement}>
24+
Test
25+
</DefaultColumnFormatter>
26+
);
27+
28+
expect(component).toMatchInlineSnapshot(`
29+
<EuiText>
30+
Test
31+
</EuiText>
32+
`);
33+
});
34+
35+
test('providedPopoverContents.json', () => {
36+
const Component = providedPopoverContents.json;
37+
const component = shallow(
38+
<Component cellContentsElement={cellContentsElement}>Test</Component>
39+
);
40+
41+
expect(component).toMatchInlineSnapshot(`
42+
<EuiCodeBlock
43+
isCopyable={true}
44+
language="json"
45+
paddingSize="none"
46+
transparentBackground={true}
47+
>
48+
{
49+
"hello": "world"
50+
}
51+
</EuiCodeBlock>
52+
`);
53+
});
54+
});
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the Elastic License
4+
* 2.0 and the Server Side Public License, v 1; you may not use this file except
5+
* in compliance with, at your election, the Elastic License 2.0 or the Server
6+
* Side Public License, v 1.
7+
*/
8+
9+
import React from 'react';
10+
import { EuiText } from '../../text';
11+
import { EuiCodeBlock } from '../../code';
12+
import {
13+
EuiDataGridPopoverContents,
14+
EuiDataGridPopoverContent,
15+
} from '../data_grid_types';
16+
17+
export const DefaultColumnFormatter: EuiDataGridPopoverContent = ({
18+
children,
19+
}) => {
20+
return <EuiText>{children}</EuiText>;
21+
};
22+
23+
export const providedPopoverContents: EuiDataGridPopoverContents = {
24+
json: ({ cellContentsElement }) => {
25+
let formattedText = cellContentsElement.innerText;
26+
27+
// attempt to pretty-print the json
28+
try {
29+
formattedText = JSON.stringify(JSON.parse(formattedText), null, 2);
30+
} catch (e) {} // eslint-disable-line no-empty
31+
32+
return (
33+
<EuiCodeBlock
34+
isCopyable
35+
transparentBackground
36+
paddingSize="none"
37+
language="json"
38+
>
39+
{formattedText}
40+
</EuiCodeBlock>
41+
);
42+
},
43+
};

0 commit comments

Comments
 (0)