-
Notifications
You must be signed in to change notification settings - Fork 17
feat(SchemaViewer): calculate column width based on data #1885
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
Changes from 4 commits
779366c
a86bb90
bd3a9d6
c304f16
e0f6715
2f77428
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,7 @@ | ||
| import DataTable from '@gravity-ui/react-data-table'; | ||
|
|
||
| import {getColumnWidth} from '../../../../utils/getColumnWidth'; | ||
|
|
||
| import i18n from './i18n'; | ||
| import type {SchemaColumn, SchemaData} from './types'; | ||
|
|
||
|
|
@@ -108,16 +110,37 @@ const compressionColumn: SchemaColumn = { | |
| render: ({row}) => row.columnCodec, | ||
| }; | ||
|
|
||
| export function getViewColumns(): SchemaColumn[] { | ||
| return [nameColumn, typeColumn]; | ||
| const WIDTH_PREDICTION_ROWS_COUNT = 100; | ||
|
|
||
| function normalizeColumns(columns: SchemaColumn[], data?: SchemaData[]) { | ||
| if (!data) { | ||
| return columns; | ||
| } | ||
| const dataSlice = data.slice(0, WIDTH_PREDICTION_ROWS_COUNT); | ||
| return columns.map((column) => { | ||
| return { | ||
| ...column, | ||
| width: getColumnWidth({ | ||
| data: dataSlice, | ||
| name: column.name, | ||
| header: typeof column.header === 'string' ? column.header : undefined, | ||
| sortable: column.sortable === undefined, | ||
| }), | ||
| }; | ||
| }); | ||
| } | ||
|
|
||
| export function getViewColumns(data?: SchemaData[]): SchemaColumn[] { | ||
| return normalizeColumns([nameColumn, typeColumn], data); | ||
| } | ||
| export function getExternalTableColumns(): SchemaColumn[] { | ||
| return [idColumn, nameColumn, typeColumn, notNullColumn]; | ||
| export function getExternalTableColumns(data?: SchemaData[]): SchemaColumn[] { | ||
| return normalizeColumns([idColumn, nameColumn, typeColumn, notNullColumn], data); | ||
| } | ||
| export function getColumnTableColumns(): SchemaColumn[] { | ||
| return [idColumn, nameColumn, typeColumn, notNullColumn]; | ||
| export function getColumnTableColumns(data?: SchemaData[]): SchemaColumn[] { | ||
| return normalizeColumns([idColumn, nameColumn, typeColumn, notNullColumn], data); | ||
| } | ||
| export function getRowTableColumns( | ||
| data: SchemaData[] | undefined, | ||
| extended: boolean, | ||
| hasAutoIncrement: boolean, | ||
| hasDefaultValue: boolean, | ||
|
|
@@ -136,5 +159,7 @@ export function getRowTableColumns( | |
| rowTableColumns.push(autoIncrementColumn); | ||
| } | ||
|
|
||
| return rowTableColumns; | ||
| console.log(normalizeColumns(rowTableColumns, data)); | ||
|
||
|
|
||
| return normalizeColumns(rowTableColumns, data); | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,93 @@ | ||
| import { | ||
| HEADER_PADDING, | ||
| MAX_COLUMN_WIDTH, | ||
| PIXELS_PER_CHARACTER, | ||
| SORT_ICON_PADDING, | ||
| getColumnWidth, | ||
| } from '../getColumnWidth'; | ||
|
|
||
| describe('getColumnWidth', () => { | ||
| it('returns minimum width for empty data', () => { | ||
| const result = getColumnWidth({data: [], name: 'test'}); | ||
| expect(result).toBe(HEADER_PADDING + 'test'.length * PIXELS_PER_CHARACTER); | ||
| }); | ||
|
|
||
| it('calculates correct width for string columns', () => { | ||
| const data = [{test: 'short'}, {test: 'medium length'}, {test: 'this is a longer string'}]; | ||
| const result = getColumnWidth({data, name: 'test'}); | ||
| expect(result).toBe( | ||
| HEADER_PADDING + 'this is a longer string'.length * PIXELS_PER_CHARACTER, | ||
| ); | ||
| }); | ||
|
|
||
| it('calculates correct width for columns with sorting', () => { | ||
| const result = getColumnWidth({data: [], name: 'test', sortable: true}); | ||
| expect(result).toBe( | ||
| HEADER_PADDING + SORT_ICON_PADDING + 'test'.length * PIXELS_PER_CHARACTER, | ||
| ); | ||
| }); | ||
|
|
||
| it('calculates correct width for columns with header', () => { | ||
| const result = getColumnWidth({data: [], name: 'test', header: 'a'}); | ||
| expect(result).toBe(HEADER_PADDING + 'a'.length * PIXELS_PER_CHARACTER); | ||
| }); | ||
|
|
||
| it('returns MAX_COLUMN_WIDTH when calculated width exceeds it', () => { | ||
| const data = [{test: 'a'.repeat(100)}]; | ||
| const result = getColumnWidth({data, name: 'test'}); | ||
| expect(result).toBe(MAX_COLUMN_WIDTH); | ||
| }); | ||
|
|
||
| it('handles undefined data correctly', () => { | ||
| const result = getColumnWidth({name: 'test'}); | ||
| expect(result).toBe(HEADER_PADDING + 'test'.length * PIXELS_PER_CHARACTER); | ||
| }); | ||
|
|
||
| it('handles missing values in data correctly', () => { | ||
| const data = [{test: 'short'}, {}, {test: 'longer string'}]; | ||
| const result = getColumnWidth({data, name: 'test'}); | ||
| expect(result).toBe(HEADER_PADDING + 'longer string'.length * PIXELS_PER_CHARACTER); | ||
| }); | ||
|
|
||
| it('uses column name length when all values are shorter', () => { | ||
| const data = [{longColumnName: 'a'}, {longColumnName: 'bb'}]; | ||
| const result = getColumnWidth({data, name: 'longColumnName'}); | ||
| expect(result).toBe(HEADER_PADDING + 'longColumnName'.length * PIXELS_PER_CHARACTER); | ||
| }); | ||
|
|
||
| it('handles null values in data correctly', () => { | ||
| const data = [{test: 'a'}, {test: null}]; | ||
| const result = getColumnWidth({data, name: 'test'}); | ||
| expect(result).toBe(HEADER_PADDING + 'test'.length * PIXELS_PER_CHARACTER); | ||
| }); | ||
|
|
||
| it('handles undefined values in data correctly', () => { | ||
| const data = [{test: 'a'}, {test: undefined}]; | ||
| const result = getColumnWidth({data, name: 'test'}); | ||
| expect(result).toBe(HEADER_PADDING + 'test'.length * PIXELS_PER_CHARACTER); | ||
| }); | ||
|
|
||
| it('handles empty string values in data correctly', () => { | ||
| const data = [{test: 'short'}, {test: ''}, {test: 'longer string'}]; | ||
| const result = getColumnWidth({data, name: 'test'}); | ||
| expect(result).toBe(HEADER_PADDING + 'longer string'.length * PIXELS_PER_CHARACTER); | ||
| }); | ||
|
|
||
| it('handles an array of numbers correctly', () => { | ||
| const data = [{test: 1}, {test: 123}, {test: 12345}]; | ||
| const result = getColumnWidth({data, name: 'test'}); | ||
| expect(result).toBe(HEADER_PADDING + '12345'.length * PIXELS_PER_CHARACTER); | ||
| }); | ||
|
|
||
| it('handles an array of mixed data types correctly', () => { | ||
| const data = [{test: 'short'}, {test: 123}, {test: null}, {test: 'longer string'}]; | ||
| const result = getColumnWidth({data, name: 'test'}); | ||
| expect(result).toBe(HEADER_PADDING + 'longer string'.length * PIXELS_PER_CHARACTER); | ||
| }); | ||
|
|
||
| it('handles empty name correctly', () => { | ||
| const data = [{test: 'test'}]; | ||
| const result = getColumnWidth({data, name: ''}); | ||
| expect(result).toBe(HEADER_PADDING); | ||
| }); | ||
| }); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| import type {SchemaData} from '../containers/Tenant/Schema/SchemaViewer/types'; | ||
| import type {KeyValueRow} from '../types/api/query'; | ||
|
|
||
| export const MAX_COLUMN_WIDTH = 600; | ||
| export const HEADER_PADDING = 20; | ||
| export const SORT_ICON_PADDING = 18; | ||
| export const PIXELS_PER_CHARACTER = 10; | ||
|
|
||
| export function getColumnWidth<T extends KeyValueRow | SchemaData>({ | ||
| data, | ||
| name, | ||
| header, | ||
| sortable, | ||
| }: { | ||
| data?: T[]; | ||
| name: string; | ||
| header?: string; | ||
| sortable?: boolean; | ||
| }) { | ||
| const sortPadding = sortable ? SORT_ICON_PADDING : 0; | ||
| let maxColumnContentLength = typeof header === 'string' ? header.length : name.length; | ||
|
|
||
| if (data) { | ||
| for (const row of data) { | ||
| let cellLength = 0; | ||
| if (hasProperty(row, name) && row[name]) { | ||
| cellLength = String(row[name]).length; | ||
| } | ||
|
|
||
| maxColumnContentLength = Math.max(maxColumnContentLength, cellLength); | ||
|
|
||
| if ( | ||
| maxColumnContentLength * PIXELS_PER_CHARACTER + HEADER_PADDING >= | ||
| MAX_COLUMN_WIDTH | ||
|
Comment on lines
+32
to
+34
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| ) { | ||
| return MAX_COLUMN_WIDTH; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return maxColumnContentLength * PIXELS_PER_CHARACTER + HEADER_PADDING + sortPadding; | ||
| } | ||
|
|
||
| function hasProperty(obj: KeyValueRow | SchemaData, key: string): obj is KeyValueRow { | ||
| return key in obj; | ||
| } | ||
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sortablecould betrue, I'd just passcolumn.sortablehere