-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: fix columns width on long rowset (#1384)
- Loading branch information
Showing
4 changed files
with
72 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
src/components/QueryResultTable/utils/getColumnWidth.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import {HEADER_PADDING, MAX_COLUMN_WIDTH, 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 * 10); | ||
}); | ||
|
||
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 * 10); | ||
}); | ||
|
||
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 * 10); | ||
}); | ||
|
||
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 * 10); | ||
}); | ||
|
||
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 * 10); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import type {KeyValueRow} from '../../../types/api/query'; | ||
|
||
export const MAX_COLUMN_WIDTH = 600; | ||
export const HEADER_PADDING = 20; | ||
|
||
export const getColumnWidth = ({data, name}: {data?: KeyValueRow[]; name: string}) => { | ||
let maxColumnContentLength = name.length; | ||
|
||
if (data) { | ||
for (const row of data) { | ||
const cellLength = row[name] ? String(row[name]).length : 0; | ||
maxColumnContentLength = Math.max(maxColumnContentLength, cellLength); | ||
|
||
if (maxColumnContentLength * 10 + HEADER_PADDING >= MAX_COLUMN_WIDTH) { | ||
return MAX_COLUMN_WIDTH; | ||
} | ||
} | ||
} | ||
|
||
return maxColumnContentLength * 10 + HEADER_PADDING; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters