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

fix(charts): Table Chart doesn't twitch when resizing #20833

Merged
merged 1 commit into from
Jul 27, 2022
Merged
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
58 changes: 54 additions & 4 deletions superset-frontend/plugins/plugin-chart-table/src/TableChart.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,13 @@
* specific language governing permissions and limitations
* under the License.
*/
import React, { CSSProperties, useCallback, useMemo, useState } from 'react';
import React, {
CSSProperties,
useCallback,
useLayoutEffect,
useMemo,
useState,
} from 'react';
import {
ColumnInstance,
ColumnWithLooseAccessor,
Expand Down Expand Up @@ -50,9 +56,15 @@ import Styles from './Styles';
import { formatColumnValue } from './utils/formatValue';
import { PAGE_SIZE_OPTIONS } from './consts';
import { updateExternalFormData } from './DataTable/utils/externalAPIs';
import getScrollBarSize from './DataTable/utils/getScrollBarSize';

type ValueRange = [number, number];

interface TableSize {
width: number;
height: number;
}

/**
* Return sortType based on data type
*/
Expand Down Expand Up @@ -198,7 +210,10 @@ export default function TableChart<D extends DataRecord = DataRecord>(
value => getTimeFormatterForGranularity(timeGrain)(value),
[timeGrain],
);

const [tableSize, setTableSize] = useState<TableSize>({
width: 0,
height: 0,
});
// keep track of whether column order changed, so that column widths can too
const [columnOrderToggle, setColumnOrderToggle] = useState(false);

Expand Down Expand Up @@ -526,6 +541,41 @@ export default function TableChart<D extends DataRecord = DataRecord>(
[setDataMask],
);

const handleSizeChange = useCallback(
({ width, height }: { width: number; height: number }) => {
setTableSize({ width, height });
},
[],
);

useLayoutEffect(() => {
// After initial load the table should resize only when the new sizes
// Are not only scrollbar updates, otherwise, the table would twicth
Copy link
Member

Choose a reason for hiding this comment

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

Appreciate the comments! This helps SOOOO much.

const scrollBarSize = getScrollBarSize();
const { width: tableWidth, height: tableHeight } = tableSize;
// Table is increasing its original size
if (
width - tableWidth > scrollBarSize ||
height - tableHeight > scrollBarSize
) {
handleSizeChange({
width: width - scrollBarSize,
height: height - scrollBarSize,
});
} else if (
tableWidth - width > scrollBarSize ||
tableHeight - height > scrollBarSize
) {
// Table is decreasing its original size
handleSizeChange({
width,
height,
});
}
}, [width, height, handleSizeChange, tableSize]);

const { width: widthFromState, height: heightFromState } = tableSize;

return (
<Styles>
<DataTable<D>
Expand All @@ -536,8 +586,8 @@ export default function TableChart<D extends DataRecord = DataRecord>(
pageSize={pageSize}
serverPaginationData={serverPaginationData}
pageSizeOptions={pageSizeOptions}
width={width}
height={height}
width={widthFromState}
height={heightFromState}
serverPagination={serverPagination}
onServerPaginationChange={handleServerPaginationChange}
onColumnOrderChange={() => setColumnOrderToggle(!columnOrderToggle)}
Expand Down