Skip to content

Kepler.gl is a powerful open source geospatial analysis tool for large-scale data sets.

[Bug] Fixed issue with geojson fields (#683)

Giuseppe Macrì74c7bf05a4ca
diff --git a/src/components/common/datagrid.js b/src/components/common/datagrid.js
index 91a2aea3d..5d7e1d471 100644
--- a/src/components/common/datagrid.js
+++ b/src/components/common/datagrid.js
@@ -22,9 +22,12 @@ import React, {PureComponent} from 'react';
import PropTypes from 'prop-types';
import {MultiGrid} from 'react-virtualized';
import styled, {withTheme}from 'styled-components';
+import {createSelector} from 'reselect';
+import classnames from 'classnames';
import FieldToken from 'components/common/field-token';
import {Clock} from 'components/common/icons/index';
-import {ALL_FIELD_TYPES} from 'constants/default-settings';
+import {parseFieldValue} from 'utils/data-utils';
+import {ALL_FIELD_TYPES} from 'constants';
const DataGridWrapper = styled.div`
.ReactVirtualized__Grid:focus,
@@ -34,6 +37,28 @@ const DataGridWrapper = styled.div`
.ReactVirtualized__Grid__innerScrollContainer {
${props => props.theme.modalScrollBar};
}
+
+ .ReactVirtualized__Grid {
+ .column-0 .cell {
+ padding-left: 24px;
+ }
+
+ .cell {
+ overflow-y: scroll;
+ overflow-x: hidden;
+
+ &.geojson {
+ span {
+ text-align: left;
+ }
+ }
+ }
+
+
+ .last .cell {
+ padding-right: 24px;
+ }
+ }
`;
const StyledFieldHeader = styled.div`
@@ -54,26 +79,30 @@ const StyledFieldHeader = styled.div`
}
.icon-wrapper {
- marginRight: ${props => props.type === 'timestamp' ? '2px' : '0'};
+ margin-right: ${props => props.type === 'timestamp' ? '2px' : '0'};
height: 16px;
}
`;
-const FieldHeaderTemplate = ({name, type}) => (
- <StyledFieldHeader type={type}>
- <div className="label-wrapper">
- <div className="icon-wrapper">
- {type === 'timestamp' ? <Clock height="16px" /> : null}
+export const FieldHeaderFactory = () => {
+ const Header = ({className, value, type}) => (
+ <StyledFieldHeader className={className || ''} type={type} title={value}>
+ <div className="label-wrapper">
+ <div className="icon-wrapper">
+ {type === 'timestamp' ? <Clock height="16px" /> : null}
+ </div>
+ <span>{value}</span>
+ </div>
+ <div className="field-wrapper">
+ <FieldToken type={type} />
</div>
- <span>{name}</span>
- </div>
- <div className="field-wrapper">
- <FieldToken type={type} />
- </div>
- </StyledFieldHeader>
-);
+ </StyledFieldHeader>
+ );
-export const FieldHeaderFactory = () => FieldHeaderTemplate;
+ Header.displayName = 'Header';
+
+ return Header;
+};
const StyledCell = styled.div`
display: flex;
@@ -85,17 +114,29 @@ const StyledCell = styled.div`
border-bottom: ${props => props.theme.panelBorderLT};
color: ${props => props.theme.labelColorLT};
text-overflow: ellipsis;
- white-space: pre-wrap;
- word-wrap: break-spaces;
height: 100%;
+ width: 100%;
+
+ span {
+ overflow: scroll;
+ text-overflow: ellipsis;
+ white-space: pre-wrap;
+ word-wrap: break-word;
+ width: 100%;
+ }
`;
-const CellTemplate = ({name, type}) => {
- const value = type === ALL_FIELD_TYPES.boolean ? String(name) : name;
- return (<StyledCell title={value}><span>{value}</span></StyledCell>);
-};
+export const CellFactory = () => {
+ const Cell = ({className, value}) => (
+ <StyledCell className={className || ''} title={value}>
+ <span>{value}</span>
+ </StyledCell>
+ );
-export const CellFactory = () => CellTemplate;
+ Cell.displayName = 'Cell';
+
+ return Cell;
+};
DataGridFactory.deps = [
FieldHeaderFactory,
@@ -115,21 +156,30 @@ function DataGridFactory(
width: PropTypes.number.isRequired
};
+ columnsSelector = props => props.columns;
+ hasGeojson = createSelector(this.columnsSelector, columns =>
+ columns.some(c => c.type === ALL_FIELD_TYPES.geojson)
+ );
+
_cellRenderer = ({columnIndex, key, rowIndex, style}) => {
const {columns, rows} = this.props;
- // rowIndex -1 because data rows start rendering at index 1 and we normalize back using the -1 param
- const className = `${rowIndex === 0
- ? `header-${columnIndex}`
- : `row-${rowIndex-1}`} column-${columnIndex}`;
+ const isLast = columnIndex === columns.length - 1
const type = columns[columnIndex].type;
+ // rowIndex -1 because data rows start rendering at index 1 and we normalize back using the -1 param
+ const className = classnames({
+ last: isLast,
+ [`header-${columnIndex}`]: rowIndex === 0,
+ [`row-${rowIndex-1} column-${columnIndex}`]: rowIndex > 0
+ });
+
return (
<div key={key} style={style} className={className}>
{rowIndex === 0
- ? (<FieldHeader className={`header-cell ${type}`} name={columns[columnIndex].name} type={type} />)
- : (<Cell className={`cell ${type}`} name={rows[rowIndex - 1][columnIndex]} type={type} />)
+ ? (<FieldHeader className={`header-cell ${type}`} value={columns[columnIndex].name} type={type} />)
+ : (<Cell className={`cell ${type}`} value={parseFieldValue(rows[rowIndex - 1][columnIndex], type)} type={type} />)
}
</div>
);
@@ -137,7 +187,12 @@ function DataGridFactory(
_rowHeight = ({index}) => index === 0
? this.props.theme.cellHeaderHeight
- : this.props.theme.cellHeight;
+ : this.hasGeojson(this.props) ? this.props.theme.extendCellHeight : this.props.theme.cellHeight;
+
+ _columnWidth = ({index}) => {
+ const isGeojsonField = this.props.columns[index].type === ALL_FIELD_TYPES.geojson;
+ return isGeojsonField ? this.props.theme.extendColumnWidth : this.props.theme.columnWidth;
+ };
render() {
const {columns, height, rows, theme, width} = this.props;
@@ -146,7 +201,7 @@ function DataGridFactory(
<DataGridWrapper className="datagrid-wrapper">
<MultiGrid
cellRenderer={this._cellRenderer}
- columnWidth={theme.columnWidth}
+ columnWidth={this._columnWidth}
columnCount={columns.length}
fixedRowCount={1}
enableFixedRowScroll={true}
diff --git a/src/components/modals/data-table-modal.js b/src/components/modals/data-table-modal.js
index 98c218d08..016344001 100644
--- a/src/components/modals/data-table-modal.js
+++ b/src/components/modals/data-table-modal.js
@@ -76,7 +76,7 @@ DataTableModalFactory.deps = [
];
function DataTableModalFactory(DataGrid) {
- const DataTableModal = ({
+ const DataTableModal = React.memo(({
datasets,
dataId,
height,
@@ -90,9 +90,6 @@ function DataTableModalFactory(DataGrid) {
const activeDataset = datasets[dataId];
const rows = activeDataset.data;
- const columns = activeDataset.fields
- .filter(({name}) => name !== '_geojson');
-
return (
<StyledModal className="dataset-modal" >
<DatasetTabs
@@ -104,12 +101,13 @@ function DataTableModalFactory(DataGrid) {
width={width}
height={height}
rows={rows}
- columns={columns}
+ columns={activeDataset.fields}
/>
</StyledModal>
);
- };
+ });
+ DataTableModal.displayName = 'DataTableModal';
return DataTableModal;
}
diff --git a/src/styles/base.js b/src/styles/base.js
index 07079bfd3..aa41140eb 100644
--- a/src/styles/base.js
+++ b/src/styles/base.js
@@ -229,6 +229,8 @@ export const notificationPanelItemHeight = 60;
const columnWidth = 200;
const cellHeaderHeight = 72;
const cellHeight = 48;
+const extendCellHeight = 2 * cellHeight;
+const extendColumnWidth = 2 * columnWidth;
const gridDefaultWidth = 800;
const gridDefaultHeight = 600;
@@ -958,8 +960,10 @@ export const theme = {
// datagrid
columnWidth,
+ extendColumnWidth,
cellHeaderHeight,
cellHeight,
+ extendCellHeight,
gridDefaultWidth,
gridDefaultHeight,
diff --git a/src/utils/data-utils.js b/src/utils/data-utils.js
index 921d465f0..db94dc23b 100644
--- a/src/utils/data-utils.js
+++ b/src/utils/data-utils.js
@@ -220,3 +220,26 @@ export function roundValToStep(minValue, step, val) {
return Number(rounded);
}
+
+/**
+ * Parse field value and type and return a string representation
+ * @param {string} value the field value
+ * @param {string} type the field type
+ * @return {*}
+ */
+export const parseFieldValue = (value, type) => {
+ if (!notNullorUndefined(value)) {
+ return '';
+ }
+
+ switch (type) {
+ case ALL_FIELD_TYPES.boolean:
+ return String(value);
+ case ALL_FIELD_TYPES.geojson:
+ return typeof value === 'string' ? value :
+ isPlainObject(value) ? JSON.stringify(value) :
+ Array.isArray(value) ? `[${String(value)}]` : '';
+ default:
+ return value;
+ }
+};
diff --git a/test/browser/components/data-table-modal-test.js b/test/browser/components/data-table-modal-test.js
index 04daa2209..fc0912c6e 100644
--- a/test/browser/components/data-table-modal-test.js
+++ b/test/browser/components/data-table-modal-test.js
@@ -69,9 +69,9 @@ test('Components -> DataTableModal.render', t => {
t.equal(wrapper.find(DataTableModal).length, 1, 'should render DataTableModal data');
t.equal(wrapper.find(DatasetTabs).length, 1, 'should render DatasetTabs');
t.equal(wrapper.find(DatasetModalTab).length, 1, 'should render 1 dataset-modal-catalog');
- t.equal(wrapper.find('.header-cell').length, testFields.length, `should render ${testFields.length} headers`);
- t.equal(wrapper.find('.cell').length, testFields.length * rows, `should render ${testFields.length * rows} cells`);
- t.equal(wrapper.find('.cell.boolean').length, 10, 'should render 10 cells');
+ t.equal(wrapper.find('Header').length, testFields.length, `should render ${testFields.length} headers`);
+ t.equal(wrapper.find('Cell').length, testFields.length * rows, `should render ${testFields.length * rows} cells`);
+ t.equal(wrapper.find('StyledComponent.cell.boolean').length, 10, 'should render 10 cells');
const expectedRows = {
0: [