Skip to content
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
27 changes: 13 additions & 14 deletions packages/core/src/components/DataTable/DataTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ export class DataTable extends React.Component<DataTableProps & WithStylesProps,
sortDirection: this.props.sortDirectionOverride!,
};

timeoutId: number = 0;

constructor(props: DataTableProps & WithStylesProps) {
super(props);
if (this.props.dataTableRef) {
Expand Down Expand Up @@ -114,10 +116,7 @@ export class DataTable extends React.Component<DataTableProps & WithStylesProps,
const { dynamicRowHeight, data, filterData, width, height, sortByCacheKey } = this.props;
const { sortBy, sortDirection } = this.state;
const dimensionsChanged = width !== prevProps.width || height !== prevProps.height;
const sortChanged =
sortBy !== prevState.sortBy ||
Copy link
Contributor Author

Choose a reason for hiding this comment

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

we already invoke this.cache.clearAll(); when updating sortBy or sortByDirection, so this was doing a double update

sortDirection !== prevState.sortDirection ||
sortByCacheKey !== prevProps.sortByCacheKey;
const sortChanged = sortByCacheKey !== prevProps.sortByCacheKey;
const sortedData: IndexedParentRow[] = this.getData(
data!,
sortBy,
Expand All @@ -134,20 +133,19 @@ export class DataTable extends React.Component<DataTableProps & WithStylesProps,
x.metadata.originalIndex !== oldFilteredData[i].metadata.originalIndex,
));

if (dynamicRowHeight && (filteredDataChanged || dimensionsChanged || sortChanged)) {
// We need to make sure the cache is cleared before React tries to re-render.
window.setTimeout(() => {
this.cache.clearAll();
this.forceUpdate();
}, 0);
}

if (this.props.data !== prevProps.data) {
this.keys = getKeys(this.props.keys!, this.props.data!);

this.setState({
expandedRows: new Set(),
});
} else if (dynamicRowHeight && (filteredDataChanged || dimensionsChanged || sortChanged)) {
// We need to make sure the cache is cleared before React tries to re-render.
if (this.timeoutId) window.clearTimeout(this.timeoutId);
this.timeoutId = window.setTimeout(() => {
this.cache.clearAll();
this.forceUpdate();
});
}
}

Expand Down Expand Up @@ -224,10 +222,11 @@ export class DataTable extends React.Component<DataTableProps & WithStylesProps,

if (dynamicRowHeight) {
// We need to make sure the cache is cleared before React tries to re-render.
window.setTimeout(() => {
if (this.timeoutId) window.clearTimeout(this.timeoutId);
Copy link
Contributor

Choose a reason for hiding this comment

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

If a timer is already set, shouldn't that one just run instead of clearing and creating a new one?

this.timeoutId = window.setTimeout(() => {
this.cache.clearAll();
this.forceUpdate();
}, 0);
});
}
};

Expand Down
5 changes: 3 additions & 2 deletions packages/core/src/components/DataTable/constants.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { WidthProperties } from './types';
import { WidthProperties, RowHeightOptions } from './types';

export const SELECTION_OPTIONS = {
ACTIVE: 'ACTIVE',
Expand All @@ -13,14 +13,15 @@ export const STATUS_OPTIONS = {
};

type HeightMap = {
[key: string]: number;
[key in RowHeightOptions]: number;
Copy link
Contributor

Choose a reason for hiding this comment

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

nice

};

export const HEIGHT_TO_PX: HeightMap = {
micro: 32,
small: 48,
regular: 56,
large: 64,
xLarge: 80,
jumbo: 108,
};

Expand Down
4 changes: 3 additions & 1 deletion packages/core/src/components/DataTable/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { DataTable } from './DataTable';

export type DataTableRef = (instance: DataTable) => void;
export type TableRef = React.RefObject<Table>;
export type RowHeightOptions = string;
export type RowHeightOptions = 'micro' | 'small' | 'regular' | 'large' | 'xLarge' | 'jumbo';
Copy link
Contributor

Choose a reason for hiding this comment

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

Lets just call it xlarge for consistency.

export type HeightOptions = RowHeightOptions | undefined;
export type ColumnLabelCase = 'sentence' | 'title' | 'uppercase' | '';

Expand Down Expand Up @@ -45,6 +45,8 @@ export interface DataTableProps {
height?: number;
/** References row fields to render as columns, infered from data if not specified. */
keys?: string[];
/** If dynamicRowHeight is enabled, this sets the maximum value for measured row height. */
maximumDynamicRowHeight?: number;
/** If dynamicRowHeight is enabled, this sets the minimum value for measured row height. */
minimumDynamicRowHeight?: number;
/**
Expand Down
6 changes: 3 additions & 3 deletions packages/core/test/components/DataTable.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -384,14 +384,14 @@ describe('<DataTable /> does not break with weird props', () => {
width: 500,
tableHeaderLabel: 'My Table',
zebra: true,
rowHeight: 'regular',
rowHeight: 'regular' as const,
columnMetadata,
columnToLabel: {
name: 'CUSTOM NAME',
},
expandable: true,
columnHeaderHeight: 'micro',
tableHeaderHeight: 'large',
columnHeaderHeight: 'micro' as const,
tableHeaderHeight: 'large' as const,
keys: ['name'],
showRowDividers: true,
};
Expand Down