-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(SchemaViewer): calculate column width based on data (#1885)
- Loading branch information
Showing
8 changed files
with
180 additions
and
73 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: 0 additions & 37 deletions
37
src/components/QueryResultTable/utils/getColumnWidth.test.ts
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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
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
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
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,100 @@ | ||
import { | ||
HEADER_PADDING, | ||
MAX_COLUMN_WIDTH, | ||
PIXELS_PER_CHARACTER, | ||
SORT_ICON_TO_CHARACTERS, | ||
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 + ('test'.length + SORT_ICON_TO_CHARACTERS) * PIXELS_PER_CHARACTER, | ||
); | ||
}); | ||
it('calculates correct width for columns with sorting and column name wider than header', () => { | ||
const data = [{test: 'this is a longer string'}]; | ||
const result = getColumnWidth({data, name: 'test', sortable: true}); | ||
expect(result).toBe( | ||
HEADER_PADDING + 'this is a longer string'.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); | ||
}); | ||
}); |
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,42 @@ | ||
export const MAX_COLUMN_WIDTH = 600; | ||
export const HEADER_PADDING = 20; | ||
export const SORT_ICON_TO_CHARACTERS = 2; | ||
export const PIXELS_PER_CHARACTER = 10; | ||
|
||
export function getColumnWidth({ | ||
data, | ||
name, | ||
header, | ||
sortable, | ||
}: { | ||
data?: Record<string, unknown>[]; | ||
name: string; | ||
header?: string; | ||
sortable?: boolean; | ||
}) { | ||
const headerContentLength = typeof header === 'string' ? header.length : name.length; | ||
|
||
let maxColumnContentLength = sortable | ||
? headerContentLength + SORT_ICON_TO_CHARACTERS | ||
: headerContentLength; | ||
|
||
if (data) { | ||
for (const row of data) { | ||
let cellLength = 0; | ||
if (row[name]) { | ||
cellLength = String(row[name]).length; | ||
} | ||
|
||
maxColumnContentLength = Math.max(maxColumnContentLength, cellLength); | ||
|
||
if ( | ||
maxColumnContentLength * PIXELS_PER_CHARACTER + HEADER_PADDING >= | ||
MAX_COLUMN_WIDTH | ||
) { | ||
return MAX_COLUMN_WIDTH; | ||
} | ||
} | ||
} | ||
|
||
return maxColumnContentLength * PIXELS_PER_CHARACTER + HEADER_PADDING; | ||
} |