-
Notifications
You must be signed in to change notification settings - Fork 13
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
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
779366c
feat(SchemaViewer): calculate column width based on data
Raubzeug a86bb90
fix: tests
Raubzeug bd3a9d6
fix: remove prev version
Raubzeug c304f16
fix: typecheck and tests
Raubzeug e0f6715
fix: review
Raubzeug 2f77428
fix: review
Raubzeug File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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; | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
sortPadding
is not used here