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

[DATA GRID] Avoid schema detection on columns which already have a schema #2550

Merged
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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
- Added `badge` prop and new styles `EuiHeaderAlert` ([#2506](https://github.com/elastic/eui/pull/2506))
- Added `disabled` prop to the `EuiCheckboxGroup` definition ([#2545](https://github.com/elastic/eui/pull/2545))

**Bug fixes**

- Fixed `EuiDataGrid` schema detection on already defined column schemas ([#2550](https://github.com/elastic/eui/pull/2550))

## [`16.0.1`](https://github.com/elastic/eui/tree/v16.0.1)

**Bug fixes**
Expand Down
2 changes: 1 addition & 1 deletion src-docs/src/views/datagrid/datagrid_schema_example.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ export const DataGridSchemaExample = {
</EuiCode>{' '}
to each matching cell.
</p>
<h4>Defining expansio</h4>
<h4>Defining expansiom</h4>
<p>
Likewise, you can inject custom content into any of the popovers a
cell expands into. Add <EuiCode>popoverContents</EuiCode> functions
Expand Down
16 changes: 15 additions & 1 deletion src/components/datagrid/data_grid.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -413,13 +413,27 @@ export const EuiDataGrid: FunctionComponent<EuiDataGridProps> = props => {

const [inMemoryValues, onCellRender] = useInMemoryValues(inMemory, rowCount);

const definedColumnSchemas = useMemo(() => {
return columns.reduce<{ [key: string]: string }>(
(definedColumnSchemas, { id, schema }) => {
if (schema != null) {
definedColumnSchemas[id] = schema;
}
return definedColumnSchemas;
},
{}
);
}, [columns]);

const allSchemaDetectors = useMemo(
() => [...providedSchemaDetectors, ...(schemaDetectors || [])],
[schemaDetectors]
);
const detectedSchema = useDetectSchema(
inMemory,
inMemoryValues,
allSchemaDetectors,
definedColumnSchemas,
inMemory != null
);
const mergedSchema = getMergedSchema(detectedSchema, columns);
Expand All @@ -431,7 +445,7 @@ export const EuiDataGrid: FunctionComponent<EuiDataGridProps> = props => {
const columnSorting = useColumnSorting(
orderedVisibleColumns,
sorting,
detectedSchema,
mergedSchema,
allSchemaDetectors
);
const [styleSelector, gridStyles] = useStyleSelector(gridStyleWithDefaults);
Expand Down
9 changes: 9 additions & 0 deletions src/components/datagrid/data_grid_schema.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import React, { useMemo, ReactNode } from 'react';
import {
EuiDataGridColumn,
EuiDataGridInMemory,
EuiDataGridInMemoryValues,
} from './data_grid_types';

Expand Down Expand Up @@ -254,8 +255,10 @@ function scoreValueBySchemaType(
const MINIMUM_SCORE_MATCH = 0.5;

export function useDetectSchema(
inMemory: EuiDataGridInMemory | undefined,
inMemoryValues: EuiDataGridInMemoryValues,
schemaDetectors: EuiDataGridSchemaDetector[] | undefined,
definedColumnSchemas: { [key: string]: string },
autoDetectSchema: boolean
) {
const schema = useMemo(() => {
Expand All @@ -271,13 +274,19 @@ export function useDetectSchema(
// for each row, score each value by each detector and put the results on `columnSchemas`
const rowIndices = Object.keys(inMemoryValues);

const columnIdsWithDefinedSchemas = new Set<string>([
...((inMemory && inMemory.skipColumns) || []),
...Object.keys(definedColumnSchemas),
]);

for (let i = 0; i < rowIndices.length; i++) {
const rowIndex = rowIndices[i];
const rowData = inMemoryValues[rowIndex];
const columnIds = Object.keys(rowData);

for (let j = 0; j < columnIds.length; j++) {
const columnId = columnIds[j];
if (columnIdsWithDefinedSchemas.has(columnId)) continue;

const schemaColumn = (columnSchemas[columnId] =
columnSchemas[columnId] || {});
Expand Down