Skip to content

Commit e0f6715

Browse files
committed
fix: review
1 parent c304f16 commit e0f6715

File tree

4 files changed

+23
-18
lines changed

4 files changed

+23
-18
lines changed

src/containers/Tenant/Schema/SchemaViewer/columns.tsx

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ function normalizeColumns(columns: SchemaColumn[], data?: SchemaData[]) {
124124
data: dataSlice,
125125
name: column.name,
126126
header: typeof column.header === 'string' ? column.header : undefined,
127-
sortable: column.sortable === undefined,
127+
sortable: column.sortable || column.sortable === undefined,
128128
}),
129129
};
130130
});
@@ -159,7 +159,5 @@ export function getRowTableColumns(
159159
rowTableColumns.push(autoIncrementColumn);
160160
}
161161

162-
console.log(normalizeColumns(rowTableColumns, data));
163-
164162
return normalizeColumns(rowTableColumns, data);
165163
}

src/containers/Tenant/Schema/SchemaViewer/types.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type {Column} from '@gravity-ui/react-data-table';
22

3-
export interface SchemaData {
3+
export type SchemaData = {
44
id?: number;
55
name?: string;
66
keyColumnIndex?: number;
@@ -12,7 +12,7 @@ export interface SchemaData {
1212
prefferedPoolKind?: string;
1313
columnCodec?: string;
1414
defaultValue?: string | number | boolean;
15-
}
15+
};
1616

1717
export interface SchemaColumn extends Column<SchemaData> {
1818
name: keyof SchemaData;

src/utils/__test__/getColumnWidth.test.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,13 @@ describe('getColumnWidth', () => {
2626
HEADER_PADDING + SORT_ICON_PADDING + 'test'.length * PIXELS_PER_CHARACTER,
2727
);
2828
});
29+
it('calculates correct width for columns with sorting and column name wider than header', () => {
30+
const data = [{test: 'this is a longer string'}];
31+
const result = getColumnWidth({data, name: 'test', sortable: true});
32+
expect(result).toBe(
33+
HEADER_PADDING + 'this is a longer string'.length * PIXELS_PER_CHARACTER,
34+
);
35+
});
2936

3037
it('calculates correct width for columns with header', () => {
3138
const result = getColumnWidth({data: [], name: 'test', header: 'a'});

src/utils/getColumnWidth.ts

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,46 @@
1-
import type {SchemaData} from '../containers/Tenant/Schema/SchemaViewer/types';
2-
import type {KeyValueRow} from '../types/api/query';
3-
41
export const MAX_COLUMN_WIDTH = 600;
52
export const HEADER_PADDING = 20;
63
export const SORT_ICON_PADDING = 18;
74
export const PIXELS_PER_CHARACTER = 10;
85

9-
export function getColumnWidth<T extends KeyValueRow | SchemaData>({
6+
export function getColumnWidth({
107
data,
118
name,
129
header,
1310
sortable,
1411
}: {
15-
data?: T[];
12+
data?: Record<string, unknown>[];
1613
name: string;
1714
header?: string;
1815
sortable?: boolean;
1916
}) {
20-
const sortPadding = sortable ? SORT_ICON_PADDING : 0;
21-
let maxColumnContentLength = typeof header === 'string' ? header.length : name.length;
17+
const headerContentLength = typeof header === 'string' ? header.length : name.length;
18+
19+
let maxColumnContentLength = headerContentLength;
2220

2321
if (data) {
2422
for (const row of data) {
2523
let cellLength = 0;
26-
if (hasProperty(row, name) && row[name]) {
24+
if (row[name]) {
2725
cellLength = String(row[name]).length;
2826
}
2927

3028
maxColumnContentLength = Math.max(maxColumnContentLength, cellLength);
3129

30+
const sortPadding =
31+
sortable && maxColumnContentLength <= headerContentLength ? SORT_ICON_PADDING : 0;
32+
3233
if (
33-
maxColumnContentLength * PIXELS_PER_CHARACTER + HEADER_PADDING >=
34+
maxColumnContentLength * PIXELS_PER_CHARACTER + HEADER_PADDING + sortPadding >=
3435
MAX_COLUMN_WIDTH
3536
) {
3637
return MAX_COLUMN_WIDTH;
3738
}
3839
}
3940
}
4041

41-
return maxColumnContentLength * PIXELS_PER_CHARACTER + HEADER_PADDING + sortPadding;
42-
}
42+
const sortPadding =
43+
sortable && maxColumnContentLength <= headerContentLength ? SORT_ICON_PADDING : 0;
4344

44-
function hasProperty(obj: KeyValueRow | SchemaData, key: string): obj is KeyValueRow {
45-
return key in obj;
45+
return maxColumnContentLength * PIXELS_PER_CHARACTER + HEADER_PADDING + sortPadding;
4646
}

0 commit comments

Comments
 (0)