Skip to content

Commit 2111e70

Browse files
committed
feat: Dynamically set widget height based on initial content
1 parent 4143a8f commit 2111e70

File tree

3 files changed

+55
-16
lines changed

3 files changed

+55
-16
lines changed

bigframes/display/table_widget.css

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ body[data-theme='dark'] .bigframes-widget.bigframes-widget {
8181
.bigframes-widget .table-container {
8282
background-color: var(--bf-bg);
8383
margin: 0;
84-
height: 400px;
84+
max-height: 620px;
8585
overflow: auto;
8686
padding: 0;
8787
}

bigframes/display/table_widget.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,9 +170,27 @@ function render({ model, el }) {
170170
model.save_changes();
171171
}
172172

173+
let isHeightInitialized = false;
174+
173175
function handleTableHTMLChange() {
174176
tableContainer.innerHTML = model.get(ModelProperty.TABLE_HTML);
175177

178+
// After the first render, dynamically set the container height to fit the
179+
// initial page (usually 10 rows) and then lock it.
180+
setTimeout(() => {
181+
if (!isHeightInitialized) {
182+
const table = tableContainer.querySelector('table');
183+
if (table) {
184+
const tableHeight = table.offsetHeight;
185+
// Add a small buffer(e.g. 2px) for borders to avoid scrollbars.
186+
if (tableHeight > 0) {
187+
tableContainer.style.height = `${tableHeight + 2}px`;
188+
isHeightInitialized = true;
189+
}
190+
}
191+
}
192+
}, 0);
193+
176194
const sortableColumns = model.get(ModelProperty.ORDERABLE_COLUMNS);
177195
const currentSortContext = model.get(ModelProperty.SORT_CONTEXT) || [];
178196

tests/js/table_widget.test.js

Lines changed: 36 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -340,31 +340,52 @@ describe('TableWidget', () => {
340340
* Tests that the widget correctly renders HTML with truncated columns (ellipsis)
341341
* and ensures that the ellipsis column is not treated as a sortable column.
342342
*/
343-
it('should have a fixed height for the table container', () => {
344-
// Mock the initial state
343+
it('should set height dynamically on first load and remain fixed', () => {
344+
jest.useFakeTimers();
345+
346+
// Mock the table's offsetHeight
347+
let mockHeight = 150;
348+
Object.defineProperty(HTMLElement.prototype, 'offsetHeight', {
349+
configurable: true,
350+
get: () => mockHeight,
351+
});
352+
353+
// Mock model properties
345354
model.get.mockImplementation((property) => {
346355
if (property === 'table_html') {
347356
return '<table>...</table>';
348357
}
349358
return null;
350359
});
351360

352-
// Load the CSS
353-
const fs = require('fs');
354-
const path = require('path');
355-
const css = fs.readFileSync(
356-
path.resolve(__dirname, '../../bigframes/display/table_widget.css'),
357-
'utf8',
358-
);
359-
const style = document.createElement('style');
360-
style.textContent = css;
361-
document.head.appendChild(style);
362-
363361
render({ model, el });
364362

365363
const tableContainer = el.querySelector('.table-container');
366-
const styles = window.getComputedStyle(tableContainer);
367-
expect(styles.height).toBe('400px');
364+
365+
// --- First render ---
366+
const tableHtmlChangeHandler = model.on.mock.calls.find(
367+
(call) => call[0] === 'change:table_html',
368+
)[1];
369+
tableHtmlChangeHandler();
370+
jest.runAllTimers();
371+
372+
// Height should be set to the mocked offsetHeight + 2px buffer
373+
expect(tableContainer.style.height).toBe('152px');
374+
375+
// --- Second render (e.g., page size change) ---
376+
// Simulate the new content being taller
377+
mockHeight = 350;
378+
tableHtmlChangeHandler();
379+
jest.runAllTimers();
380+
381+
// Height should NOT change
382+
expect(tableContainer.style.height).toBe('152px');
383+
384+
// Restore original implementation
385+
Object.defineProperty(HTMLElement.prototype, 'offsetHeight', {
386+
value: 0,
387+
});
388+
jest.useRealTimers();
368389
});
369390

370391
it('should render truncated columns with ellipsis and not make ellipsis sortable', () => {

0 commit comments

Comments
 (0)