Skip to content
This repository has been archived by the owner on Jun 25, 2020. It is now read-only.

fix(legacy-table): avoid React DOM #392

Merged
merged 9 commits into from
Mar 9, 2020
Merged
Show file tree
Hide file tree
Changes from 6 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
8 changes: 7 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
"fast-glob": "^3.0.1",
"fs-extra": "^8.0.1",
"husky": "^4.2.1",
"identity-obj-proxy": "^3.0.0",
"jest-mock-console": "^1.0.0",
"lerna": "^3.15.0",
"lint-staged": "^10.0.7",
Expand Down Expand Up @@ -102,7 +103,8 @@
"jest": {
"timers": "real",
"setupFilesAfterEnv": [
"@airbnb/config-jest/enzyme"
"@airbnb/config-jest/enzyme",
"./scripts/setupJest.js"
],
"coverageThreshold": {
"global": {
Expand All @@ -111,6 +113,10 @@
"lines": 1,
"statements": 1
}
},
"moduleNameMapper": {
"\\.(jpg|jpeg|png|gif|eot|otf|webp|svg|ttf|woff|woff2|mp4|webm|wav|mp3|m4a|aac|oga)$": "<rootDir>/__mocks__/fileMock.js",
"\\.(css|less)$": "identity-obj-proxy"
}
},
"eslint": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
"@superset-ui/time-format": "^0.12.0",
"@superset-ui/translation": "^0.12.0",
"jquery": "^3.4.1",
"react": "^16.8.0"
"react": "^16.8.0",
"react-dom": "^16.8.0"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
*/
import { t } from '@superset-ui/translation';
import React, { useEffect, createRef } from 'react';
import ReactDOMServer from 'react-dom/server';
import { formatNumber, NumberFormats } from '@superset-ui/number-format';
import { getTimeFormatter } from '@superset-ui/time-format';
import { filterXSS } from 'xss';
Expand Down Expand Up @@ -202,49 +203,58 @@ export default function ReactDataTable(props: DataTableProps) {
};
});

return (
<div ref={rootElem} className="superset-legacy-chart-table">
<table className="table table-striped table-condensed table-hover">
<thead>
<tr>
{columns.map(col => (
// by default all columns will have sorting
<th key={col.key} className="sorting" title={col.label}>
{col.label}
</th>
))}
</tr>
</thead>
<tbody>
{data.map((record, i) => (
// hide rows after first page makes the initial render faster (less layout computation)
// eslint-disable-next-line react/no-array-index-key
<tr key={i} style={{ display: pageLength > 0 && i >= pageLength ? 'none' : undefined }}>
{columns.map(({ key, format }) => {
const val = record[key];
const keyIsMetric = metricsSet.has(key);
const text = cellText(key, format, val);
const isHtml = !keyIsMetric && isProbablyHTML(text);
return (
<td
key={key}
// only set innerHTML for actual html content, this saves time
dangerouslySetInnerHTML={isHtml ? { __html: text } : undefined}
data-sort={val}
className={keyIsMetric ? 'text-right' : ''}
style={{
backgroundImage: keyIsMetric ? cellBar(key, val as number) : undefined,
}}
title={keyIsMetric || percentMetricsSet.has(key) ? (val as string) : ''}
>
{isHtml ? null : text}
</td>
);
})}
</tr>
const tableElement = (
<table className="table table-striped table-condensed table-hover">
<thead>
<tr>
{columns.map(col => (
// by default all columns will have sorting
<th key={col.key} className="sorting" title={col.label}>
{col.label}
</th>
))}
</tbody>
</table>
</div>
</tr>
</thead>
<tbody>
{data.map((record, i) => (
<tr
// eslint-disable-next-line react/no-array-index-key
key={i}
// hide rows after first page makes the initial render faster (less layout computation)
style={{ display: pageLength > 0 && i >= pageLength ? 'none' : undefined }}
>
{columns.map(({ key, format }) => {
const val = record[key];
const keyIsMetric = metricsSet.has(key);
const text = cellText(key, format, val);
const isHtml = !keyIsMetric && isProbablyHTML(text);
return (
<td
key={key}
// only set innerHTML for actual html content, this saves time
dangerouslySetInnerHTML={isHtml ? { __html: text } : undefined}
data-sort={val}
className={keyIsMetric ? 'text-right' : ''}
style={{
backgroundImage: keyIsMetric ? cellBar(key, val as number) : undefined,
}}
title={keyIsMetric || percentMetricsSet.has(key) ? (val as string) : ''}
>
{isHtml ? null : text}
</td>
);
})}
</tr>
))}
</tbody>
</table>
);

return (
<div
dangerouslySetInnerHTML={{ __html: ReactDOMServer.renderToStaticMarkup(tableElement) }}
ref={rootElem}
className="superset-legacy-chart-table"
/>
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,11 @@ export default function transformProps(chartProps: ChartProps): DataTableProps {
} = formData;
const { columnFormats, verboseMap } = datasource;
const { records, columns: columns_ } = queryData.data;
const metrics = metrics_.map(consolidateMetricShape);
const metrics = (metrics_ ?? []).map(consolidateMetricShape);
// percent metrics always starts with a '%' sign.
const percentMetrics = percentMetrics_.map(consolidateMetricShape).map((x: string) => `%${x}`);
const percentMetrics = (percentMetrics_ ?? [])
.map(consolidateMetricShape)
.map((x: string) => `%${x}`);
const columns = columns_.map((key: string) => {
let label = verboseMap[key] || key;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
import React from 'react';
import { mount } from 'enzyme';
import ReactDataTable from '../src/ReactDataTable';
import transformProps from '../src/transformProps';
import * as testData from './test_data';
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps use specific import to enable warning if we later do not use some of these.
import { basic, advance } from ...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wanted to have a namespace so it's more explicit in later code, but also wanted to keep variable names simple.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added a default export for testData. I think it looks better than import *.

In the future, maybe all plugins can reuse the same test data---similar to switching visualization type in Superset.


describe('legacy-table', () => {
// Can test more prop transformation here. Not needed for now.
describe('transformProps', () => {});

describe('ReactDataTable', () => {
// Jest throw an error at `console.warn`, this disables that behavior
ktmud marked this conversation as resolved.
Show resolved Hide resolved
beforeAll(() => {
console.warn = console.log;
});
afterAll(() => {
console.warn = console.error;
});

let wrap: any; // the SuperChart wraper

it('render basic data', () => {
wrap = mount(<ReactDataTable {...transformProps(testData.basic)} />);
const tree = wrap.render(); // returns a CheerioWrapper with jQuery-like API
const cells = tree.find('td');
expect(tree.hasClass('superset-legacy-chart-table')).toEqual(true);
expect(cells).toHaveLength(4);
expect(cells.eq(0).text()).toEqual('Michael');
expect(cells.eq(3).attr('data-sort')).toEqual('2467');
});

it('render advanced data', () => {
// should successfull rerender with new props
wrap.setProps(transformProps(testData.advanced));
const tree = wrap.render();
const cells = tree.find('td');
expect(
tree
.find('th')
.eq(1)
.text(),
).toEqual('Sum of Num');
expect(cells.eq(2).text()).toEqual('12.346%');
expect(cells.eq(4).text()).toEqual('2.47k');
});
});
});
103 changes: 103 additions & 0 deletions packages/superset-ui-legacy-plugin-chart-table/test/test_data.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
/**
ktmud marked this conversation as resolved.
Show resolved Hide resolved
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
import { ChartProps } from '@superset-ui/chart';

const basicFormData = {
alignPn: false,
colorPn: false,
includeSearch: false,
orderDesc: true,
pageLength: 0,
metrics: [],
percentMetrics: null,
timeseriesLimitMetric: null,
tableFilter: false,
tableTimestampFormat: '%Y-%m-%d %H:%M:%S',
};

const basicChartProps = {
width: 200,
height: 500,
annotationData: {},
datasource: {
columnFormats: {},
verboseMap: {},
},
rawDatasource: {},
rawFormData: {},
hooks: {},
initialValues: {},
queryData: {
data: {
columns: [],
records: [],
},
},
formData: basicFormData,
};

/**
* Basic data input
*/
export const basic: ChartProps = {
...basicChartProps,
queryData: {
data: {
columns: ['name', 'sum__num'],
records: [
{
name: 'Michael',
sum__num: 2467063,
'%pct_nice': 0.123456,
},
{
name: 'Joe',
sum__num: 2467,
'%pct_nice': 0.00001,
},
],
},
},
};

/**
* Advanced data input with
* - verbose map
* - metric columns
*/
export const advanced: ChartProps = {
...basic,
datasource: {
columnFormats: {},
verboseMap: {
sum__num: 'Sum of Num',
},
},
formData: {
...basicFormData,
metrics: ['sum__num'],
percentMetrics: ['pct_nice'],
},
queryData: {
data: {
columns: ['name', 'sum__num', '%pct_nice'],
records: [...basic.queryData.data.records],
},
},
};
Loading