Skip to content

Commit

Permalink
[DATA GRID] Avoid schema detection on columns which already have a sc…
Browse files Browse the repository at this point in the history
…hema (#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
  • Loading branch information
chandlerprall authored Nov 21, 2019
1 parent d02ae18 commit 7a4efab
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 2 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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**
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 @@ -456,13 +456,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 @@ -474,7 +488,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

0 comments on commit 7a4efab

Please sign in to comment.