Skip to content

Commit

Permalink
Table visualization with column named "children" renders +/- buttons (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
kravets-levko authored Nov 26, 2019
1 parent be56035 commit c70a48d
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 15 deletions.
7 changes: 2 additions & 5 deletions client/app/visualizations/table/Renderer.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,11 @@ import Table from 'antd/lib/table';
import Input from 'antd/lib/input';
import { RendererPropTypes } from '@/visualizations';

import { prepareColumns, filterRows, sortRows } from './utils';
import { prepareColumns, initRows, filterRows, sortRows } from './utils';

import './renderer.less';

export default function Renderer({ options, data, context }) {
const [rowKeyPrefix, setRowKeyPrefix] = useState(`row:1:${options.itemsPerPage}:`);
const [searchTerm, setSearchTerm] = useState('');
const [orderBy, setOrderBy] = useState([]);

Expand Down Expand Up @@ -37,7 +36,7 @@ export default function Renderer({ options, data, context }) {
);

const preparedRows = useMemo(
() => sortRows(filterRows(data.rows, searchTerm, searchColumns), orderBy),
() => sortRows(filterRows(initRows(data.rows), searchTerm, searchColumns), orderBy),
[data.rows, searchTerm, searchColumns, orderBy],
);

Expand Down Expand Up @@ -65,13 +64,11 @@ export default function Renderer({ options, data, context }) {
data-test="TableVisualization"
columns={tableColumns}
dataSource={preparedRows}
rowKey={(record, index) => rowKeyPrefix + index}
pagination={{
size: context === 'widget' ? 'small' : '',
position: 'bottom',
pageSize: options.itemsPerPage,
hideOnSinglePage: true,
onChange: (page, pageSize) => setRowKeyPrefix(`row:${page}:${pageSize}:`),
}}
/>
</div>
Expand Down
20 changes: 10 additions & 10 deletions client/app/visualizations/table/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,8 @@ export function prepareColumns(columns, searchInput, orderBy, onOrderByChange) {
const sortColumnIndex = isMultiColumnSort && orderByInfo[column.name] ? orderByInfo[column.name].index : null;

const result = {
key: column.name, // set this because we don't use `dataIndex`
// Column name may contain any characters (or be empty at all), therefore
// we cannot use `dataIndex` because it has special syntax and will not work
// for all possible column names. Instead, we'll generate row key dynamically
// based on row index
dataIndex: null,
key: column.name,
dataIndex: `record[${JSON.stringify(column.name)}]`,
align: column.alignContent,
title: (
<React.Fragment>
Expand Down Expand Up @@ -96,7 +92,7 @@ export function prepareColumns(columns, searchInput, orderBy, onOrderByChange) {
const initColumn = ColumnTypes[column.displayAs];
const Component = initColumn(column);
result.render = (unused, row) => ({
children: <Component row={row} />,
children: <Component row={row.record} />,
props: { className: `display-as-${column.displayAs}` },
});

Expand Down Expand Up @@ -135,6 +131,10 @@ export function prepareColumns(columns, searchInput, orderBy, onOrderByChange) {
return tableColumns;
}

export function initRows(rows) {
return map(rows, (record, index) => ({ key: `record${index}`, record }));
}

export function filterRows(rows, searchTerm, searchColumns) {
if ((searchTerm !== '') && (searchColumns.length > 0)) {
searchTerm = searchTerm.toUpperCase();
Expand All @@ -147,7 +147,7 @@ export function filterRows(rows, searchTerm, searchColumns) {
};
});

return filter(rows, row => some(matchFields, match => match(row)));
return filter(rows, row => some(matchFields, match => match(row.record)));
}
return rows;
}
Expand All @@ -164,8 +164,8 @@ export function sortRows(rows, orderBy) {
let va;
let vb;
for (let i = 0; i < orderBy.length; i += 1) {
va = a[orderBy[i].name];
vb = b[orderBy[i].name];
va = a.record[orderBy[i].name];
vb = b.record[orderBy[i].name];
if (isNil(va) || va < vb) {
// if a < b - we should return -1, but take in account direction
return -1 * directions[orderBy[i].direction];
Expand Down

0 comments on commit c70a48d

Please sign in to comment.