Skip to content

Commit bdcd2ec

Browse files
Replace EuiCodeBlock with JsonCodeEditor in DiscoverGrid (#92442)
* Replace EuiCodeBlock with JsonCodeEditor in DiscoverGrid * Add optional "hasLineNumbers" property to JsonCodeEditor and removed line numbers from the popover * Update json_code_editor snapshot * Add functional test for cell expanded content popover * Remove unused code * Fix geo point case and refactor some code Co-authored-by: Kibana Machine <42973632+kibanamachine@users.noreply.github.com>
1 parent 83c6bcc commit bdcd2ec

File tree

16 files changed

+176
-109
lines changed

16 files changed

+176
-109
lines changed

src/plugins/discover/public/application/components/discover_grid/constants.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,5 @@ export const toolbarVisibility = {
2424
},
2525
showStyleSelector: false,
2626
};
27+
28+
export const defaultMonacoEditorWidth = 370;

src/plugins/discover/public/application/components/discover_grid/discover_grid.tsx

Lines changed: 30 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import {
2222
} from '@elastic/eui';
2323
import { IndexPattern } from '../../../kibana_services';
2424
import { DocViewFilterFn, ElasticSearchHit } from '../../doc_views/doc_views_types';
25-
import { getPopoverContents, getSchemaDetectors } from './discover_grid_schema';
25+
import { getSchemaDetectors } from './discover_grid_schema';
2626
import { DiscoverGridFlyout } from './discover_grid_flyout';
2727
import { DiscoverGridContext } from './discover_grid_context';
2828
import { getRenderCellValueFn } from './get_render_cell_value';
@@ -36,6 +36,7 @@ import {
3636
import { defaultPageSize, gridStyle, pageSizeArr, toolbarVisibility } from './constants';
3737
import { DiscoverServices } from '../../../build_services';
3838
import { getDisplayedColumns } from '../../helpers/columns';
39+
import { KibanaContextProvider } from '../../../../../kibana_react/public';
3940

4041
interface SortObj {
4142
id: string;
@@ -219,7 +220,6 @@ export const DiscoverGrid = ({
219220
[displayedColumns, indexPattern, showTimeCol, settings, defaultColumns]
220221
);
221222
const schemaDetectors = useMemo(() => getSchemaDetectors(), []);
222-
const popoverContents = useMemo(() => getPopoverContents(), []);
223223
const columnsVisibility = useMemo(
224224
() => ({
225225
visibleColumns: getVisibleColumns(displayedColumns, indexPattern, showTimeCol) as string[],
@@ -259,34 +259,35 @@ export const DiscoverGrid = ({
259259
}}
260260
>
261261
<>
262-
<EuiDataGridMemoized
263-
aria-describedby={randomId}
264-
aria-labelledby={ariaLabelledBy}
265-
columns={euiGridColumns}
266-
columnVisibility={columnsVisibility}
267-
data-test-subj="docTable"
268-
gridStyle={gridStyle as EuiDataGridStyle}
269-
leadingControlColumns={lead}
270-
onColumnResize={(col: { columnId: string; width: number }) => {
271-
if (onResize) {
272-
onResize(col);
262+
<KibanaContextProvider services={{ uiSettings: services.uiSettings }}>
263+
<EuiDataGridMemoized
264+
aria-describedby={randomId}
265+
aria-labelledby={ariaLabelledBy}
266+
columns={euiGridColumns}
267+
columnVisibility={columnsVisibility}
268+
data-test-subj="docTable"
269+
gridStyle={gridStyle as EuiDataGridStyle}
270+
leadingControlColumns={lead}
271+
onColumnResize={(col: { columnId: string; width: number }) => {
272+
if (onResize) {
273+
onResize(col);
274+
}
275+
}}
276+
pagination={paginationObj}
277+
renderCellValue={renderCellValue}
278+
rowCount={rowCount}
279+
schemaDetectors={schemaDetectors}
280+
sorting={sorting as EuiDataGridSorting}
281+
toolbarVisibility={
282+
defaultColumns
283+
? {
284+
...toolbarVisibility,
285+
showColumnSelector: false,
286+
}
287+
: toolbarVisibility
273288
}
274-
}}
275-
pagination={paginationObj}
276-
popoverContents={popoverContents}
277-
renderCellValue={renderCellValue}
278-
rowCount={rowCount}
279-
schemaDetectors={schemaDetectors}
280-
sorting={sorting as EuiDataGridSorting}
281-
toolbarVisibility={
282-
defaultColumns
283-
? {
284-
...toolbarVisibility,
285-
showColumnSelector: false,
286-
}
287-
: toolbarVisibility
288-
}
289-
/>
289+
/>
290+
</KibanaContextProvider>
290291

291292
{showDisclaimer && (
292293
<p className="dscDiscoverGrid__footer">
Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@
66
* Side Public License, v 1.
77
*/
88

9-
import React from 'react';
10-
import { EuiCodeBlock, EuiDataGridPopoverContents } from '@elastic/eui';
119
import { kibanaJSON } from './constants';
1210
import { KBN_FIELD_TYPES } from '../../../../../data/common';
1311

@@ -43,18 +41,3 @@ export function getSchemaDetectors() {
4341
},
4442
];
4543
}
46-
47-
/**
48-
* Returns custom popover content for certain schemas
49-
*/
50-
export function getPopoverContents(): EuiDataGridPopoverContents {
51-
return {
52-
[kibanaJSON]: ({ children }) => {
53-
return (
54-
<EuiCodeBlock isCopyable language="json" paddingSize="none" transparentBackground={true}>
55-
{children}
56-
</EuiCodeBlock>
57-
);
58-
},
59-
};
60-
}

src/plugins/discover/public/application/components/discover_grid/get_render_cell_value.test.tsx

Lines changed: 57 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,25 @@
77
*/
88

99
import React from 'react';
10-
import { shallow } from 'enzyme';
10+
import { ReactWrapper, shallow } from 'enzyme';
1111
import { getRenderCellValueFn } from './get_render_cell_value';
1212
import { indexPatternMock } from '../../../__mocks__/index_pattern';
1313

14+
jest.mock('../../../../../kibana_react/public', () => ({
15+
useUiSetting: () => true,
16+
withKibana: (comp: ReactWrapper) => {
17+
return comp;
18+
},
19+
}));
20+
21+
jest.mock('../../../kibana_services', () => ({
22+
getServices: () => ({
23+
uiSettings: {
24+
get: jest.fn(),
25+
},
26+
}),
27+
}));
28+
1429
const rowsSource = [
1530
{
1631
_id: '1',
@@ -139,20 +154,25 @@ describe('Discover grid cell rendering', function () {
139154
setCellProps={jest.fn()}
140155
/>
141156
);
142-
expect(component.html()).toMatchInlineSnapshot(`
143-
"<span>{
144-
&quot;_id&quot;: &quot;1&quot;,
145-
&quot;_index&quot;: &quot;test&quot;,
146-
&quot;_type&quot;: &quot;test&quot;,
147-
&quot;_score&quot;: 1,
148-
&quot;_source&quot;: {
149-
&quot;bytes&quot;: 100,
150-
&quot;extension&quot;: &quot;.gz&quot;
151-
},
152-
&quot;highlight&quot;: {
153-
&quot;extension&quot;: &quot;@kibana-highlighted-field.gz@/kibana-highlighted-field&quot;
157+
expect(component).toMatchInlineSnapshot(`
158+
<JsonCodeEditor
159+
json={
160+
Object {
161+
"_id": "1",
162+
"_index": "test",
163+
"_score": 1,
164+
"_source": Object {
165+
"bytes": 100,
166+
"extension": ".gz",
167+
},
168+
"_type": "test",
169+
"highlight": Object {
170+
"extension": "@kibana-highlighted-field.gz@/kibana-highlighted-field",
171+
},
172+
}
154173
}
155-
}</span>"
174+
width={370}
175+
/>
156176
`);
157177
});
158178

@@ -226,24 +246,30 @@ describe('Discover grid cell rendering', function () {
226246
setCellProps={jest.fn()}
227247
/>
228248
);
229-
expect(component.html()).toMatchInlineSnapshot(`
230-
"<span>{
231-
&quot;_id&quot;: &quot;1&quot;,
232-
&quot;_index&quot;: &quot;test&quot;,
233-
&quot;_type&quot;: &quot;test&quot;,
234-
&quot;_score&quot;: 1,
235-
&quot;fields&quot;: {
236-
&quot;bytes&quot;: [
237-
100
238-
],
239-
&quot;extension&quot;: [
240-
&quot;.gz&quot;
241-
]
242-
},
243-
&quot;highlight&quot;: {
244-
&quot;extension&quot;: &quot;@kibana-highlighted-field.gz@/kibana-highlighted-field&quot;
249+
expect(component).toMatchInlineSnapshot(`
250+
<JsonCodeEditor
251+
json={
252+
Object {
253+
"_id": "1",
254+
"_index": "test",
255+
"_score": 1,
256+
"_source": undefined,
257+
"_type": "test",
258+
"fields": Object {
259+
"bytes": Array [
260+
100,
261+
],
262+
"extension": Array [
263+
".gz",
264+
],
265+
},
266+
"highlight": Object {
267+
"extension": "@kibana-highlighted-field.gz@/kibana-highlighted-field",
268+
},
269+
}
245270
}
246-
}</span>"
271+
width={370}
272+
/>
247273
`);
248274
});
249275

src/plugins/discover/public/application/components/discover_grid/get_render_cell_value.tsx

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,16 @@ import {
1919
import { IndexPattern } from '../../../kibana_services';
2020
import { ElasticSearchHit } from '../../doc_views/doc_views_types';
2121
import { DiscoverGridContext } from './discover_grid_context';
22+
import { JsonCodeEditor } from '../json_code_editor/json_code_editor';
23+
import { defaultMonacoEditorWidth } from './constants';
2224

2325
export const getRenderCellValueFn = (
2426
indexPattern: IndexPattern,
2527
rows: ElasticSearchHit[] | undefined,
2628
rowsFlattened: Array<Record<string, unknown>>,
2729
useNewFieldsApi: boolean
2830
) => ({ rowIndex, columnId, isDetails, setCellProps }: EuiDataGridCellValueElementProps) => {
29-
const row = rows ? (rows[rowIndex] as Record<string, unknown>) : undefined;
31+
const row = rows ? rows[rowIndex] : undefined;
3032
const rowFlattened = rowsFlattened
3133
? (rowsFlattened[rowIndex] as Record<string, unknown>)
3234
: undefined;
@@ -106,10 +108,18 @@ export const getRenderCellValueFn = (
106108
);
107109
}
108110

111+
if (typeof rowFlattened[columnId] === 'object' && isDetails) {
112+
return (
113+
<JsonCodeEditor
114+
json={rowFlattened[columnId] as Record<string, any>}
115+
width={defaultMonacoEditorWidth}
116+
/>
117+
);
118+
}
119+
109120
if (field && field.type === '_source') {
110121
if (isDetails) {
111-
// nicely formatted JSON for the expanded view
112-
return <span>{JSON.stringify(row, null, 2)}</span>;
122+
return <JsonCodeEditor json={row} width={defaultMonacoEditorWidth} />;
113123
}
114124
const formatted = indexPattern.formatHit(row);
115125

src/plugins/discover/public/application/components/json_code_editor/__snapshots__/json_code_editor.test.tsx.snap

Lines changed: 3 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/plugins/discover/public/application/components/json_code_editor/json_code_editor.test.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,5 @@ it('returns the `JsonCodeEditor` component', () => {
1818
_score: 1,
1919
_source: { test: 123 },
2020
};
21-
expect(shallow(<JsonCodeEditor hit={value} />)).toMatchSnapshot();
21+
expect(shallow(<JsonCodeEditor json={value} />)).toMatchSnapshot();
2222
});

src/plugins/discover/public/application/components/json_code_editor/json_code_editor.tsx

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ import { i18n } from '@kbn/i18n';
1313
import { monaco, XJsonLang } from '@kbn/monaco';
1414
import { EuiButtonEmpty, EuiCopy, EuiFlexGroup, EuiFlexItem, EuiSpacer } from '@elastic/eui';
1515
import { CodeEditor } from '../../../../../kibana_react/public';
16-
import { DocViewRenderProps } from '../../../application/doc_views/doc_views_types';
1716

1817
const codeEditorAriaLabel = i18n.translate('discover.json.codeEditorAriaLabel', {
1918
defaultMessage: 'Read only JSON view of an elasticsearch document',
@@ -22,8 +21,14 @@ const copyToClipboardLabel = i18n.translate('discover.json.copyToClipboardLabel'
2221
defaultMessage: 'Copy to clipboard',
2322
});
2423

25-
export const JsonCodeEditor = ({ hit }: DocViewRenderProps) => {
26-
const jsonValue = JSON.stringify(hit, null, 2);
24+
interface JsonCodeEditorProps {
25+
json: Record<string, any>;
26+
width?: string | number;
27+
hasLineNumbers?: boolean;
28+
}
29+
30+
export const JsonCodeEditor = ({ json, width, hasLineNumbers }: JsonCodeEditorProps) => {
31+
const jsonValue = JSON.stringify(json, null, 2);
2732

2833
// setting editor height based on lines height and count to stretch and fit its content
2934
const setEditorCalculatedHeight = useCallback((editor) => {
@@ -43,7 +48,7 @@ export const JsonCodeEditor = ({ hit }: DocViewRenderProps) => {
4348

4449
return (
4550
<EuiFlexGroup className="dscJsonCodeEditor" direction="column" gutterSize="s">
46-
<EuiFlexItem grow={true}>
51+
<EuiFlexItem>
4752
<EuiSpacer size="s" />
4853
<div className="eui-textRight">
4954
<EuiCopy textToCopy={jsonValue}>
@@ -55,16 +60,18 @@ export const JsonCodeEditor = ({ hit }: DocViewRenderProps) => {
5560
</EuiCopy>
5661
</div>
5762
</EuiFlexItem>
58-
<EuiFlexItem grow={true}>
63+
<EuiFlexItem>
5964
<CodeEditor
6065
languageId={XJsonLang.ID}
66+
width={width}
6167
value={jsonValue}
6268
onChange={() => {}}
6369
editorDidMount={setEditorCalculatedHeight}
6470
aria-label={codeEditorAriaLabel}
6571
options={{
6672
automaticLayout: true,
6773
fontSize: 12,
74+
lineNumbers: hasLineNumbers ? 'on' : 'off',
6875
minimap: {
6976
enabled: false,
7077
},

src/plugins/discover/public/plugin.ts renamed to src/plugins/discover/public/plugin.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
*/
88

99
import { i18n } from '@kbn/i18n';
10+
import React from 'react';
1011
import angular, { auto } from 'angular';
1112
import { BehaviorSubject } from 'rxjs';
1213
import { filter, map } from 'rxjs/operators';
@@ -187,7 +188,7 @@ export class DiscoverPlugin
187188
defaultMessage: 'JSON',
188189
}),
189190
order: 20,
190-
component: JsonCodeEditor,
191+
component: ({ hit }) => <JsonCodeEditor json={hit} hasLineNumbers />,
191192
});
192193

193194
const {

0 commit comments

Comments
 (0)