Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feat] Add hasStats prop to data table adjust first cell size #2040

Merged
merged 2 commits into from
Dec 5, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
28 changes: 23 additions & 5 deletions src/components/src/common/data-table/cell-size.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ import {DataContainerInterface, parseFieldValue} from '@kepler.gl/utils';

const MIN_GHOST_CELL_SIZE: number = 200;
const MIN_CELL_SIZE = 45;
// first column have padding on the left
const EDGE_COLUMN_PADDING = 10;

type RenderSizeParam = {
text: {dataContainer: DataContainerInterface; column: string};
Expand Down Expand Up @@ -185,6 +187,21 @@ function expandCellSize(
};
}

function addPaddingToFirstColumn(cellSizeCache: CellSizeCache, columnOrder: string[] = []): CellSizeCache {
const firstCol = columnOrder[0];

if (firstCol && cellSizeCache[firstCol]) {
return {
...cellSizeCache,
[firstCol]: {
header: cellSizeCache[firstCol].header + EDGE_COLUMN_PADDING,
row: cellSizeCache[firstCol].row + EDGE_COLUMN_PADDING
}
};
}
return cellSizeCache;
}

/**
* Adjust cell size based on container width
* @param {number} containerWidth
Expand All @@ -201,13 +218,14 @@ export function adjustCellsToContainer(
cellSizeCache: CellSizeCache;
ghost?: number | null;
} {
const minRowSum = getSizeSum(cellSizeCache, 'row');
const columnOrder = getColumnOrder(pinnedColumns, unpinnedColumns);
const paddedCellSize = addPaddingToFirstColumn(cellSizeCache, columnOrder);
const minRowSum = getSizeSum(paddedCellSize, 'row');

if (minRowSum >= containerWidth) {
// we apply the min Width to all cells
return {cellSizeCache: getMinCellSize(cellSizeCache)};
return {cellSizeCache: getMinCellSize(paddedCellSize)};
}

// if we have some room to expand
const columnOrder = getColumnOrder(pinnedColumns, unpinnedColumns);
return expandCellSize(cellSizeCache, columnOrder, containerWidth, containerWidth - minRowSum);
return expandCellSize(paddedCellSize, columnOrder, containerWidth, containerWidth - minRowSum);
}
37 changes: 25 additions & 12 deletions src/components/src/common/data-table/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ const getRowCell = ({

type StatsControlProps = {
top: number;
showStats: boolean;
showStats?: boolean;
};

const StyledStatsControl = styled.div<StatsControlProps>`
Expand Down Expand Up @@ -246,7 +246,7 @@ const StyledStatsControl = styled.div<StatsControlProps>`
}
`;

const StatsControl = ({top, showStats, toggleShowStats}) => (
const StatsControl = ({top, showStats, toggleShowStats}: {top: number, showStats?: boolean, toggleShowStats: () => void}) => (
<StyledStatsControl top={top} showStats={showStats}>
<div onClick={toggleShowStats}>
{showStats ? 'Hide Column Stats' : 'Show Column Stats'}
Expand Down Expand Up @@ -339,7 +339,7 @@ export const TableSection = ({

export interface DataTableProps {
dataId?: string;
showStats?: boolean;
hasStats?: boolean;
cellSizeCache?: CellSizeCache;
pinnedColumns?: string[];
columns: (string & {ghost?: boolean})[];
Expand All @@ -353,6 +353,7 @@ export interface DataTableProps {
pinTableColumn: (column: string) => void;
copyTableColumn: (column: string) => void;
sortOrder?: number[] | null;
showStats?: boolean;
}

interface DataTableState {
Expand All @@ -373,7 +374,8 @@ function DataTableFactory(HeaderCell: ReturnType<typeof HeaderCellFactory>) {
sortColumn: {},
fixedWidth: null,
fixedHeight: null,
theme: {}
theme: {},
hasStats: false
};

pinnedGrid = false;
Expand Down Expand Up @@ -507,7 +509,8 @@ function DataTableFactory(HeaderCell: ReturnType<typeof HeaderCellFactory>) {
pinnedColumns = [],
theme = {},
fixedWidth,
fixedHeight = 0
fixedHeight = 0,
hasStats
} = this.props;
const unpinnedColumns = this.unpinnedColumns(this.props);

Expand All @@ -531,9 +534,17 @@ function DataTableFactory(HeaderCell: ReturnType<typeof HeaderCellFactory>) {
const headerGridProps = {
cellSizeCache,
className: 'header-grid',
height: showStats ? headerRowWStatsHeight : headerRowHeight + headerStatsControlHeight,
height: !hasStats
? headerRowHeight
: showStats
? headerRowWStatsHeight
: headerRowHeight + headerStatsControlHeight,
rowCount: 1,
rowHeight: showStats ? headerRowWStatsHeight : headerRowHeight + headerStatsControlHeight
rowHeight: !hasStats
? headerRowHeight
: showStats
? headerRowWStatsHeight
: headerRowHeight + headerStatsControlHeight
};

const dataGridProps = {
Expand Down Expand Up @@ -624,11 +635,13 @@ function DataTableFactory(HeaderCell: ReturnType<typeof HeaderCellFactory>) {
);
}}
</ScrollSync>
<StatsControl
top={headerRowHeight}
showStats={showStats}
toggleShowStats={this.toggleShowStats}
/>
{hasStats ? (
<StatsControl
top={headerRowHeight}
showStats={showStats}
toggleShowStats={this.toggleShowStats}
/>
) : null}
</>
) : null}
</Container>
Expand Down
4 changes: 2 additions & 2 deletions src/components/src/modals/data-table-modal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ function DataTableModalFactory(
[field.name]: renderedSize({
text: {
dataContainer,
column: field.name
column: field.displayName
},
colIdx,
type: field.type,
Expand Down Expand Up @@ -235,7 +235,7 @@ function DataTableModalFactory(
copyTableColumn={this.copyTableColumn}
pinTableColumn={this.pinTableColumn}
sortTableColumn={this.sortTableColumn}
showStats
hasStats
/>
) : null}
</TableContainer>
Expand Down
12 changes: 8 additions & 4 deletions test/browser/components/modals/data-table-modal-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ const expectedCellSizeCache = {

const expectedExpandedCellSize = {
cellSizeCache: {
'gps_data.utc_timestamp': 145,
'gps_data.utc_timestamp': 155,
'gps_data.lat': 96,
'gps_data.lng': 96,
'gps_data.types': 125,
Expand Down Expand Up @@ -527,7 +527,11 @@ test('Components -> cellSize -> renderedSize', t => {
smoothie: {
id: 'smoothie',
dataContainer,
fields,
fields: [
{name: testColumns[0], type: 'geojson', displayName: testColumns[0]},
{name: testColumns[1], type: 'real', displayName: testColumns[1]},
{name: testColumns[2], type: 'string', displayName: testColumns[2]}
],
color: [113, 113, 113]
}
}}
Expand Down Expand Up @@ -595,14 +599,14 @@ test('Components -> DataTableModal.render: csv 2', t => {

const expectedExpandedCellSizeGeo = {
cellSizeCache: {
_geojson: 400,
_geojson: 410,
fillColor: 90,
lineColor: 94,
lineWidth: 98,
elevation: 94,
radius: 90
},
ghost: 334
ghost: 324
};
const wrapper2 = mountWithTheme(<DataTable {...enriched} />);
const componentInstance = wrapper2.find('DataTable').instance();
Expand Down