Skip to content

Commit 5add284

Browse files
committed
fix: review fix
1 parent 81874d6 commit 5add284

File tree

3 files changed

+74
-10
lines changed

3 files changed

+74
-10
lines changed

src/components/QueryResultTable/QueryResultTable.tsx

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import {ResizeableDataTable} from '../ResizeableDataTable/ResizeableDataTable';
1313

1414
import {Cell} from './Cell';
1515
import i18n from './i18n';
16+
import {getColumnWidth} from './utils/getColumnWidth';
1617

1718
import './QueryResultTable.scss';
1819

@@ -26,7 +27,6 @@ const TABLE_SETTINGS: Settings = {
2627
export const b = cn('ydb-query-result-table');
2728

2829
const WIDTH_PREDICTION_ROWS_COUNT = 100;
29-
const MAX_COLUMN_WIDTH = 600;
3030

3131
const prepareTypedColumns = (columns: ColumnType[], data?: KeyValueRow[]) => {
3232
if (!columns.length) {
@@ -38,17 +38,9 @@ const prepareTypedColumns = (columns: ColumnType[], data?: KeyValueRow[]) => {
3838
return columns.map(({name, type}) => {
3939
const columnType = getColumnType(type);
4040

41-
const maxColumnContentLength =
42-
dataSlice?.reduce(
43-
(max, row) => Math.max(max, row[name] ? String(row[name]).length : max),
44-
name.length,
45-
) || name.length;
46-
47-
const headerPadding = columnType === 'number' ? 40 : 20;
48-
4941
const column: Column<KeyValueRow> = {
5042
name,
51-
width: Math.min(maxColumnContentLength * 10 + headerPadding, MAX_COLUMN_WIDTH),
43+
width: getColumnWidth({data: dataSlice, name, columnType}),
5244
align: columnType === 'number' ? DataTable.RIGHT : DataTable.LEFT,
5345
sortAccessor: (row) => {
5446
const value = row[name];
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import {MAX_COLUMN_WIDTH, getColumnWidth} from './getColumnWidth';
2+
3+
describe('getColumnWidth', () => {
4+
it('returns minimum width for empty data', () => {
5+
const result = getColumnWidth({data: [], name: 'test', columnType: 'string'});
6+
expect(result).toBe(20 + 'test'.length * 10);
7+
});
8+
9+
it('calculates correct width for string columns', () => {
10+
const data = [{test: 'short'}, {test: 'medium length'}, {test: 'this is a longer string'}];
11+
const result = getColumnWidth({data, name: 'test', columnType: 'string'});
12+
expect(result).toBe(20 + 'this is a longer string'.length * 10);
13+
});
14+
15+
it('calculates correct width for number columns', () => {
16+
const data = [{test: 123}, {test: 456789}, {test: 0}];
17+
const result = getColumnWidth({data, name: 'test', columnType: 'number'});
18+
expect(result).toBe(40 + '456789'.length * 10);
19+
});
20+
21+
it('returns MAX_COLUMN_WIDTH when calculated width exceeds it', () => {
22+
const data = [{test: 'a'.repeat(100)}];
23+
const result = getColumnWidth({data, name: 'test', columnType: 'string'});
24+
expect(result).toBe(MAX_COLUMN_WIDTH);
25+
});
26+
27+
it('handles undefined data correctly', () => {
28+
const result = getColumnWidth({name: 'test', columnType: 'string'});
29+
expect(result).toBe(20 + 'test'.length * 10);
30+
});
31+
32+
it('handles missing values in data correctly', () => {
33+
const data = [{test: 'short'}, {}, {test: 'longer string'}];
34+
const result = getColumnWidth({data, name: 'test', columnType: 'string'});
35+
expect(result).toBe(20 + 'longer string'.length * 10);
36+
});
37+
38+
it('uses column name length when all values are shorter', () => {
39+
const data = [{longColumnName: 'a'}, {longColumnName: 'bb'}];
40+
const result = getColumnWidth({data, name: 'longColumnName', columnType: 'string'});
41+
expect(result).toBe(20 + 'longColumnName'.length * 10);
42+
});
43+
});
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import type {KeyValueRow} from '../../../types/api/query';
2+
3+
export const MAX_COLUMN_WIDTH = 600;
4+
5+
export const getColumnWidth = ({
6+
data,
7+
name,
8+
columnType,
9+
}: {
10+
data?: KeyValueRow[];
11+
name: string;
12+
columnType?: string;
13+
}) => {
14+
let maxColumnContentLength = name.length;
15+
const headerPadding = columnType === 'number' ? 40 : 20;
16+
17+
if (data) {
18+
for (const row of data) {
19+
const cellLength = row[name] ? String(row[name]).length : 0;
20+
maxColumnContentLength = Math.max(maxColumnContentLength, cellLength);
21+
22+
if (maxColumnContentLength * 10 + headerPadding >= MAX_COLUMN_WIDTH) {
23+
return MAX_COLUMN_WIDTH;
24+
}
25+
}
26+
}
27+
28+
return Math.min(maxColumnContentLength * 10 + headerPadding, MAX_COLUMN_WIDTH);
29+
};

0 commit comments

Comments
 (0)