Skip to content

Commit ebab32b

Browse files
authored
new(DataTable): Add xLarge row size. Fix forceUpdate logic (#367)
1 parent 300637f commit ebab32b

File tree

4 files changed

+22
-20
lines changed

4 files changed

+22
-20
lines changed

packages/core/src/components/DataTable/DataTable.tsx

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,8 @@ export class DataTable extends React.Component<DataTableProps & WithStylesProps,
6666
sortDirection: this.props.sortDirectionOverride!,
6767
};
6868

69+
timeoutId: number = 0;
70+
6971
constructor(props: DataTableProps & WithStylesProps) {
7072
super(props);
7173
if (this.props.dataTableRef) {
@@ -114,10 +116,7 @@ export class DataTable extends React.Component<DataTableProps & WithStylesProps,
114116
const { dynamicRowHeight, data, filterData, width, height, sortByCacheKey } = this.props;
115117
const { sortBy, sortDirection } = this.state;
116118
const dimensionsChanged = width !== prevProps.width || height !== prevProps.height;
117-
const sortChanged =
118-
sortBy !== prevState.sortBy ||
119-
sortDirection !== prevState.sortDirection ||
120-
sortByCacheKey !== prevProps.sortByCacheKey;
119+
const sortChanged = sortByCacheKey !== prevProps.sortByCacheKey;
121120
const sortedData: IndexedParentRow[] = this.getData(
122121
data!,
123122
sortBy,
@@ -134,20 +133,19 @@ export class DataTable extends React.Component<DataTableProps & WithStylesProps,
134133
x.metadata.originalIndex !== oldFilteredData[i].metadata.originalIndex,
135134
));
136135

137-
if (dynamicRowHeight && (filteredDataChanged || dimensionsChanged || sortChanged)) {
138-
// We need to make sure the cache is cleared before React tries to re-render.
139-
window.setTimeout(() => {
140-
this.cache.clearAll();
141-
this.forceUpdate();
142-
}, 0);
143-
}
144-
145136
if (this.props.data !== prevProps.data) {
146137
this.keys = getKeys(this.props.keys!, this.props.data!);
147138

148139
this.setState({
149140
expandedRows: new Set(),
150141
});
142+
} else if (dynamicRowHeight && (filteredDataChanged || dimensionsChanged || sortChanged)) {
143+
// We need to make sure the cache is cleared before React tries to re-render.
144+
if (this.timeoutId) window.clearTimeout(this.timeoutId);
145+
this.timeoutId = window.setTimeout(() => {
146+
this.cache.clearAll();
147+
this.forceUpdate();
148+
});
151149
}
152150
}
153151

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

225223
if (dynamicRowHeight) {
226224
// We need to make sure the cache is cleared before React tries to re-render.
227-
window.setTimeout(() => {
225+
if (this.timeoutId) window.clearTimeout(this.timeoutId);
226+
this.timeoutId = window.setTimeout(() => {
228227
this.cache.clearAll();
229228
this.forceUpdate();
230-
}, 0);
229+
});
231230
}
232231
};
233232

packages/core/src/components/DataTable/constants.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { WidthProperties } from './types';
1+
import { WidthProperties, RowHeightOptions } from './types';
22

33
export const SELECTION_OPTIONS = {
44
ACTIVE: 'ACTIVE',
@@ -13,14 +13,15 @@ export const STATUS_OPTIONS = {
1313
};
1414

1515
type HeightMap = {
16-
[key: string]: number;
16+
[key in RowHeightOptions]: number;
1717
};
1818

1919
export const HEIGHT_TO_PX: HeightMap = {
2020
micro: 32,
2121
small: 48,
2222
regular: 56,
2323
large: 64,
24+
xLarge: 80,
2425
jumbo: 108,
2526
};
2627

packages/core/src/components/DataTable/types.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { DataTable } from './DataTable';
55

66
export type DataTableRef = (instance: DataTable) => void;
77
export type TableRef = React.RefObject<Table>;
8-
export type RowHeightOptions = string;
8+
export type RowHeightOptions = 'micro' | 'small' | 'regular' | 'large' | 'xLarge' | 'jumbo';
99
export type HeightOptions = RowHeightOptions | undefined;
1010
export type ColumnLabelCase = 'sentence' | 'title' | 'uppercase' | '';
1111

@@ -45,6 +45,8 @@ export interface DataTableProps {
4545
height?: number;
4646
/** References row fields to render as columns, infered from data if not specified. */
4747
keys?: string[];
48+
/** If dynamicRowHeight is enabled, this sets the maximum value for measured row height. */
49+
maximumDynamicRowHeight?: number;
4850
/** If dynamicRowHeight is enabled, this sets the minimum value for measured row height. */
4951
minimumDynamicRowHeight?: number;
5052
/**

packages/core/test/components/DataTable.test.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -384,14 +384,14 @@ describe('<DataTable /> does not break with weird props', () => {
384384
width: 500,
385385
tableHeaderLabel: 'My Table',
386386
zebra: true,
387-
rowHeight: 'regular',
387+
rowHeight: 'regular' as const,
388388
columnMetadata,
389389
columnToLabel: {
390390
name: 'CUSTOM NAME',
391391
},
392392
expandable: true,
393-
columnHeaderHeight: 'micro',
394-
tableHeaderHeight: 'large',
393+
columnHeaderHeight: 'micro' as const,
394+
tableHeaderHeight: 'large' as const,
395395
keys: ['name'],
396396
showRowDividers: true,
397397
};

0 commit comments

Comments
 (0)