From 7a4efab08c76528b2c05caf974dd4d6ca4c0b87a Mon Sep 17 00:00:00 2001
From: Chandler Prall
Date: Wed, 20 Nov 2019 23:01:42 -0700
Subject: [PATCH] [DATA GRID] Avoid schema detection on columns which already
have a schema (#2550)
* Don't try to auto-detect schemas of columns which already have a known schema
* changelog
* Fix docs typo; pass merged schema through to column sorter instead of only detected schema
---
CHANGELOG.md | 4 ++++
.../views/datagrid/datagrid_schema_example.js | 2 +-
src/components/datagrid/data_grid.tsx | 16 +++++++++++++++-
src/components/datagrid/data_grid_schema.tsx | 9 +++++++++
4 files changed, 29 insertions(+), 2 deletions(-)
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 67de00a67af..ff02f437e26 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -5,6 +5,10 @@
- Added `disabled` prop to the `EuiCheckboxGroup` definition ([#2545](https://github.com/elastic/eui/pull/2545))
- Added `disabled` option to the `option` attribute of the `options` object that is passed to the `EuiCheckboxGroup` so that checkboxes in a group can be individually disabled ([#2548](https://github.com/elastic/eui/pull/2548))
+**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**
diff --git a/src-docs/src/views/datagrid/datagrid_schema_example.js b/src-docs/src/views/datagrid/datagrid_schema_example.js
index 754fbd6a866..807bef581a8 100644
--- a/src-docs/src/views/datagrid/datagrid_schema_example.js
+++ b/src-docs/src/views/datagrid/datagrid_schema_example.js
@@ -66,7 +66,7 @@ export const DataGridSchemaExample = {
{' '}
to each matching cell.
- Defining expansio
+ Defining expansiom
Likewise, you can inject custom content into any of the popovers a
cell expands into. Add popoverContents functions
diff --git a/src/components/datagrid/data_grid.tsx b/src/components/datagrid/data_grid.tsx
index 8c5548195da..69ed8a3f227 100644
--- a/src/components/datagrid/data_grid.tsx
+++ b/src/components/datagrid/data_grid.tsx
@@ -456,13 +456,27 @@ export const EuiDataGrid: FunctionComponent = 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);
@@ -474,7 +488,7 @@ export const EuiDataGrid: FunctionComponent = props => {
const columnSorting = useColumnSorting(
orderedVisibleColumns,
sorting,
- detectedSchema,
+ mergedSchema,
allSchemaDetectors
);
const [styleSelector, gridStyles] = useStyleSelector(gridStyleWithDefaults);
diff --git a/src/components/datagrid/data_grid_schema.tsx b/src/components/datagrid/data_grid_schema.tsx
index df1bc5c1bb8..9eccafd6f62 100644
--- a/src/components/datagrid/data_grid_schema.tsx
+++ b/src/components/datagrid/data_grid_schema.tsx
@@ -1,6 +1,7 @@
import React, { useMemo, ReactNode } from 'react';
import {
EuiDataGridColumn,
+ EuiDataGridInMemory,
EuiDataGridInMemoryValues,
} from './data_grid_types';
@@ -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(() => {
@@ -271,6 +274,11 @@ 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([
+ ...((inMemory && inMemory.skipColumns) || []),
+ ...Object.keys(definedColumnSchemas),
+ ]);
+
for (let i = 0; i < rowIndices.length; i++) {
const rowIndex = rowIndices[i];
const rowData = inMemoryValues[rowIndex];
@@ -278,6 +286,7 @@ export function useDetectSchema(
for (let j = 0; j < columnIds.length; j++) {
const columnId = columnIds[j];
+ if (columnIdsWithDefinedSchemas.has(columnId)) continue;
const schemaColumn = (columnSchemas[columnId] =
columnSchemas[columnId] || {});