Repository navigation
feat(app): double-click a column's resize handle to fit it to its content #3819
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
Open
alex-fedotyev
wants to merge
4
commits into
main
Choose a base branch
from
alex/HDX-6190-column-fit
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+682
−8
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
da6217b
feat(app): double-click a column's resize handle to fit it to its con…
alex-fedotyev 9bb30ae
Merge branch 'main' into alex/HDX-6190-column-fit
alex-fedotyev 110a997
fix(app): review feedback on column fit
alex-fedotyev fbf843d
fix(app): offer column fit only in tables that save their widths
alex-fedotyev 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| --- | ||
| '@hyperdx/app': patch | ||
| --- | ||
|
|
||
| feat: double-click a column's resize handle in search results to fit the column | ||
| to its content. A fitted column stops at 600px or half the visible table width, | ||
| and the last column goes back to filling the table. |
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
151 changes: 151 additions & 0 deletions
151
packages/app/src/components/DBTable/__tests__/columnFit.test.ts
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,151 @@ | ||
| import { | ||
| getFittedColumnWidth, | ||
| measureColumnContentWidth, | ||
| } from '@/components/DBTable/columnFit'; | ||
|
|
||
| const CHAR_WIDTH = 8; | ||
|
|
||
| // jsdom does no layout. The hidden copies the helper measures report 8px per | ||
| // character of text; every other element reports the width in its inline | ||
| // style. | ||
| function mockLayout() { | ||
| return jest | ||
| .spyOn(HTMLElement.prototype, 'getBoundingClientRect') | ||
| .mockImplementation(function (this: HTMLElement) { | ||
| const width = | ||
| this.style.visibility === 'hidden' | ||
| ? (this.textContent ?? '').length * CHAR_WIDTH | ||
| : parseFloat(this.style.width) || 0; | ||
| return { | ||
| width, | ||
| height: 0, | ||
| top: 0, | ||
| left: 0, | ||
| right: width, | ||
| bottom: 0, | ||
| x: 0, | ||
| y: 0, | ||
| toJSON: () => ({}), | ||
| }; | ||
| }); | ||
| } | ||
|
|
||
| function renderTable(html: string) { | ||
| const table = document.createElement('table'); | ||
| table.innerHTML = html; | ||
| document.body.append(table); | ||
| return table; | ||
| } | ||
|
|
||
| const headerCell = (columnId: string, label: string, padding = 0) => | ||
| `<th data-column-id="${columnId}" style="width: 160px"><div style="width: ${160 - padding}px">${label}</div></th>`; | ||
|
|
||
| const bodyCell = (columnId: string, value: string) => | ||
| `<td><div data-column-id="${columnId}" style="width: 160px"><div style="width: 160px">${value}</div></div></td>`; | ||
|
|
||
| describe('measureColumnContentWidth', () => { | ||
| let rectSpy: jest.SpyInstance; | ||
|
|
||
| beforeEach(() => { | ||
| rectSpy = mockLayout(); | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| rectSpy.mockRestore(); | ||
| document.body.innerHTML = ''; | ||
| }); | ||
|
|
||
| it('returns the widest of the header and the rendered cells of the column', () => { | ||
| const table = renderTable(` | ||
| <thead><tr>${headerCell('service', 'Service')}${headerCell('body', 'Body')}</tr></thead> | ||
| <tbody> | ||
| <tr>${bodyCell('service', 'api')}${bodyCell('body', 'a much longer value in another column')}</tr> | ||
| <tr>${bodyCell('service', 'checkout-service')}${bodyCell('body', 'short')}</tr> | ||
| </tbody> | ||
| `); | ||
|
|
||
| expect(measureColumnContentWidth(table, 'service')).toBe( | ||
| 'checkout-service'.length * CHAR_WIDTH, | ||
| ); | ||
| }); | ||
|
|
||
| it('adds the padding the cell keeps around its content', () => { | ||
| const table = renderTable(` | ||
| <thead><tr>${headerCell('level', 'Level', 8)}</tr></thead> | ||
| <tbody><tr>${bodyCell('level', 'warn')}</tr></tbody> | ||
| `); | ||
|
|
||
| expect(measureColumnContentWidth(table, 'level')).toBe( | ||
| 'Level'.length * CHAR_WIDTH + 8, | ||
| ); | ||
| }); | ||
|
|
||
| it('skips cells of a table nested in an expanded row', () => { | ||
| const table = renderTable(` | ||
| <tbody> | ||
| <tr>${bodyCell('service', 'api')}</tr> | ||
| <tr><td><table><tbody><tr>${bodyCell('service', 'nested-table-service')}</tr></tbody></table></td></tr> | ||
| </tbody> | ||
| `); | ||
|
|
||
| expect(measureColumnContentWidth(table, 'service')).toBe( | ||
| 'api'.length * CHAR_WIDTH, | ||
| ); | ||
| }); | ||
|
|
||
| it('leaves the table as it found it', () => { | ||
| const table = renderTable( | ||
| `<tbody><tr>${bodyCell('service', 'api')}</tr></tbody>`, | ||
| ); | ||
| const html = table.innerHTML; | ||
|
|
||
| measureColumnContentWidth(table, 'service'); | ||
|
|
||
| expect(table.innerHTML).toBe(html); | ||
| }); | ||
|
|
||
| it('removes its copies even when reading a width throws', () => { | ||
| const table = renderTable( | ||
| `<tbody><tr>${bodyCell('service', 'api')}</tr></tbody>`, | ||
| ); | ||
| const html = table.innerHTML; | ||
| rectSpy.mockImplementation(() => { | ||
| throw new Error('layout failed'); | ||
| }); | ||
|
|
||
| expect(() => measureColumnContentWidth(table, 'service')).toThrow( | ||
| 'layout failed', | ||
| ); | ||
| expect(table.innerHTML).toBe(html); | ||
| }); | ||
|
|
||
| it('returns 0 when the column has no cells', () => { | ||
| const table = renderTable( | ||
| `<tbody><tr>${bodyCell('service', 'api')}</tr></tbody>`, | ||
| ); | ||
|
|
||
| expect(measureColumnContentWidth(table, 'body')).toBe(0); | ||
| }); | ||
| }); | ||
|
|
||
| describe('getFittedColumnWidth', () => { | ||
| it('rounds the content width up so the content is not cut off', () => { | ||
| expect(getFittedColumnWidth(101.2, 1400)).toBe(102); | ||
| }); | ||
|
|
||
| it('keeps at least the minimum column width', () => { | ||
| expect(getFittedColumnWidth(12, 1400)).toBe(50); | ||
| }); | ||
|
|
||
| it('caps the width at 600px', () => { | ||
| expect(getFittedColumnWidth(2000, 1600)).toBe(600); | ||
| }); | ||
|
|
||
| it('caps the width at half the visible table width when that is smaller', () => { | ||
| expect(getFittedColumnWidth(2000, 800)).toBe(400); | ||
| }); | ||
|
|
||
| it('never caps below the minimum column width', () => { | ||
| expect(getFittedColumnWidth(2000, 60)).toBe(50); | ||
| }); | ||
| }); |
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,76 @@ | ||
| import { MIN_COLUMN_WIDTH } from '@/tableUtils'; | ||
|
|
||
| /** The widest a column gets when it is fitted to its content. */ | ||
| const MAX_FITTED_COLUMN_WIDTH = 600; | ||
|
|
||
| /** | ||
| * Width, in px, that a column's widest cell needs to show its content without | ||
| * truncating or wrapping it. Only cells rendered in `table` count: the header | ||
| * and whichever body rows are mounted. Cells name their column in | ||
| * `data-column-id`; cells of tables nested in an expanded row are skipped. | ||
| * Returns 0 when the column has no cells. | ||
| */ | ||
| export function measureColumnContentWidth( | ||
| table: HTMLTableElement, | ||
| columnId: string, | ||
| ): number { | ||
| const probes: HTMLElement[] = []; | ||
| try { | ||
| const measured = Array.from( | ||
| table.querySelectorAll('[data-column-id]'), | ||
| ).flatMap(cell => { | ||
| const content = cell.firstElementChild; | ||
| if ( | ||
| !(cell instanceof HTMLElement) || | ||
| cell.dataset.columnId !== columnId || | ||
| cell.closest('table') !== table || | ||
| !(content instanceof HTMLElement) | ||
| ) { | ||
| return []; | ||
| } | ||
| // A hidden copy at max-content width reports the width the content | ||
| // needs, whatever width the column has now. | ||
| const probe = content.cloneNode(true); | ||
| if (!(probe instanceof HTMLElement)) return []; | ||
| probe.style.position = 'absolute'; | ||
| probe.style.visibility = 'hidden'; | ||
| probe.style.width = 'max-content'; | ||
| cell.append(probe); | ||
| probes.push(probe); | ||
| return [{ cell, content, probe }]; | ||
| }); | ||
|
|
||
| // Every copy is in place before the first read, so the browser lays the | ||
| // table out once rather than once per cell. | ||
| const widths = measured.map( | ||
| ({ cell, content, probe }) => | ||
| probe.getBoundingClientRect().width + | ||
| // The padding and border the cell keeps around its content | ||
| cell.getBoundingClientRect().width - | ||
| content.getBoundingClientRect().width, | ||
| ); | ||
| return Math.max(0, ...widths); | ||
| } finally { | ||
| probes.forEach(probe => probe.remove()); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * The width to give a column whose content is `contentWidth` px wide: no less | ||
| * than the minimum column width, and no more than 600px or half of | ||
| * `visibleTableWidth`, whichever is smaller, so one long value can't push the | ||
| * other columns out of view. | ||
| */ | ||
| export function getFittedColumnWidth( | ||
| contentWidth: number, | ||
| visibleTableWidth: number, | ||
| ): number { | ||
| const maxWidth = Math.max( | ||
| MIN_COLUMN_WIDTH, | ||
| Math.min(MAX_FITTED_COLUMN_WIDTH, visibleTableWidth / 2), | ||
| ); | ||
| return Math.min( | ||
| maxWidth, | ||
| Math.max(MIN_COLUMN_WIDTH, Math.ceil(contentWidth)), | ||
| ); | ||
| } |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.