Skip to content
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

fix: colspan build #7780

Merged
merged 4 commits into from
Feb 18, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
89 changes: 89 additions & 0 deletions packages/@react-spectrum/table/test/Table.ssr.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,92 @@ describe('Table SSR', function () {
`);
});
});

describe('Table Nested Rows SSR', function () {
it('should render without errors', async function () {
await testSSR(__filename, `
import {Provider} from '@react-spectrum/provider';
import {theme} from '@react-spectrum/theme-default';
import {Cell, Column, Row, TableBody, TableHeader, TableView} from '../';
import {enableTableNestedRows} from '@react-stately/flags';
enableTableNestedRows();

let nestedItems = [
{foo: 'Lvl 1 Foo 1', bar: 'Lvl 1 Bar 1', baz: 'Lvl 1 Baz 1', childRows: [
{foo: 'Lvl 2 Foo 1', bar: 'Lvl 2 Bar 1', baz: 'Lvl 2 Baz 1', childRows: [
{foo: 'Lvl 3 Foo 1', bar: 'Lvl 3 Bar 1', baz: 'Lvl 3 Baz 1'}
]},
{foo: 'Lvl 2 Foo 2', bar: 'Lvl 2 Bar 2', baz: 'Lvl 2 Baz 2'}
]}
];

let columns = [
{name: 'Foo', key: 'foo'},
{name: 'Bar', key: 'bar'},
{name: 'Baz', key: 'baz'}
];

<Provider theme={theme}>
<TableView aria-label="example table with nested rows" UNSTABLE_allowsExpandableRows width={500} height={200} >
<TableHeader columns={columns}>
{column => <Column>{column.name}</Column>}
</TableHeader>
<TableBody items={nestedItems}>
{(item) =>
(<Row key={item.foo} UNSTABLE_childItems={item.childRows}>
{(key) => {
return <Cell>{item[key.toString()]}</Cell>;
}}
</Row>)
}
</TableBody>
</TableView>
</Provider>
`);
});
});

// TODO: selectionMode="multiple" errors
describe('Table Static SSR', function () {
it('should render without errors', async function () {
await testSSR(__filename, `
import {Provider} from '@react-spectrum/provider';
import {theme} from '@react-spectrum/theme-default';
import {Cell, Column, Row, TableBody, TableHeader, TableView} from '../';
import {enableTableNestedRows} from '@react-stately/flags';
enableTableNestedRows();

<Provider theme={theme}>
<TableView aria-label="Example table with static contents">
<TableHeader>
<Column>Name</Column>
<Column>Type</Column>
<Column>Date Modified</Column>
</TableHeader>
<TableBody>
<Row>
<Cell>Games</Cell>
<Cell>File folder</Cell>
<Cell>6/7/2020</Cell>
</Row>
<Row>
<Cell>Program Files</Cell>
<Cell>File folder</Cell>
<Cell>4/7/2021</Cell>
</Row>
<Row>
<Cell>bootmgr</Cell>
<Cell>System file</Cell>
<Cell>11/20/2010</Cell>
</Row>
<Row>
<Cell>log.txt</Cell>
<Cell>Text Document</Cell>
<Cell>1/18/2016</Cell>
</Row>
</TableBody>
</TableView>
</Provider>
`);
});
});
9 changes: 0 additions & 9 deletions packages/@react-spectrum/table/test/Table.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import {Content} from '@react-spectrum/view';
import {CRUDExample} from '../stories/CRUDExample';
import {Dialog, DialogTrigger} from '@react-spectrum/dialog';
import {Divider} from '@react-spectrum/divider';
import {enableTableNestedRows} from '@react-stately/flags';
import {getFocusableTreeWalker} from '@react-aria/focus';
import {Heading} from '@react-spectrum/text';
import {Item, Picker} from '@react-spectrum/picker';
Expand Down Expand Up @@ -5041,11 +5040,3 @@ export let tableTests = () => {
};

describe('TableView', tableTests);

describe('TableView with expandable rows flag on', function () {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've just moved these to another file so that the tests run faster

beforeAll(() => {
enableTableNestedRows();
});

tableTests();
});
83 changes: 83 additions & 0 deletions packages/@react-spectrum/table/test/TableNestedRows.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/*
* Copyright 2020 Adobe. All rights reserved.
* This file is licensed to you under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. You may obtain a copy
* of the License at http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
* OF ANY KIND, either express or implied. See the License for the specific language
* governing permissions and limitations under the License.
*/

jest.mock('@react-aria/live-announcer');
jest.mock('@react-aria/utils/src/scrollIntoView');
import {act, render as renderComponent} from '@react-spectrum/test-utils-internal';
import {Cell, Column, Row, TableBody, TableHeader, TableView} from '../';
import {enableTableNestedRows} from '@react-stately/flags';
import {Provider} from '@react-spectrum/provider';
import React from 'react';
import {tableTests} from './Table.test';
import {theme} from '@react-spectrum/theme-default';

describe('TableView with expandable rows flag on', function () {
beforeAll(() => {
enableTableNestedRows();
});

tableTests();

describe('with nested rows', function () {
let offsetWidth, offsetHeight;

beforeAll(function () {
offsetWidth = jest.spyOn(window.HTMLElement.prototype, 'clientWidth', 'get').mockImplementation(() => 1000);
offsetHeight = jest.spyOn(window.HTMLElement.prototype, 'clientHeight', 'get').mockImplementation(() => 1000);
jest.useFakeTimers();
});

afterAll(function () {
offsetWidth.mockReset();
offsetHeight.mockReset();
});

afterEach(() => {
act(() => {jest.runAllTimers();});
});
it('can render', function () {
let columns = [
{name: 'Foo', key: 'foo'},
{name: 'Bar', key: 'bar'},
{name: 'Baz', key: 'baz'}
];

let nestedItems = [
{foo: 'Lvl 1 Foo 1', bar: 'Lvl 1 Bar 1', baz: 'Lvl 1 Baz 1', childRows: [
{foo: 'Lvl 2 Foo 1', bar: 'Lvl 2 Bar 1', baz: 'Lvl 2 Baz 1', childRows: [
{foo: 'Lvl 3 Foo 1', bar: 'Lvl 3 Bar 1', baz: 'Lvl 3 Baz 1'}
]},
{foo: 'Lvl 2 Foo 2', bar: 'Lvl 2 Bar 2', baz: 'Lvl 2 Baz 2'}
]}
];

renderComponent(
<Provider theme={theme}>
<TableView aria-label="example table with nested rows" UNSTABLE_allowsExpandableRows>
<TableHeader columns={columns}>
{column => <Column>{column.name}</Column>}
</TableHeader>
<TableBody items={nestedItems}>
{(item) =>
(<Row key={item.foo} UNSTABLE_childItems={item.childRows}>
{(key) => {
return <Cell>{item[key]}</Cell>;
}}
</Row>)
}
</TableBody>
</TableView>
</Provider>
);
});
});
});
14 changes: 0 additions & 14 deletions packages/@react-stately/grid/src/GridCollection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,20 +79,6 @@ export class GridCollection<T> implements IGridCollection<T> {

if (last) {
last.nextKey = null;

if (rowHasCellWithColSpan && node.type === 'item') {
Copy link
Member Author

@snowystinger snowystinger Feb 18, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

moved this check back to Table's domain, it's old collections, so not too worried about colSpan on anything other than Tables in v3
it was counting something about expandable rows as an extra column in the dynamic rendering, though I'm not entirely sure why

let lastColIndex = last?.colIndex ?? 0 + 1; // internally colIndex is 0 based
let lastColSpan = last?.colSpan ?? 1;
let numberOfCellsInRow = lastColIndex + lastColSpan;
if (numberOfCellsInRow !== this.columnCount) {
throw new Error(`Cell count must match column count. Found ${numberOfCellsInRow} cells and ${this.columnCount} columns.`);
}
} else if (node.type === 'item') {
let numberOfCellsInRow = [...node.childNodes].length;
if (numberOfCellsInRow !== this.columnCount) {
throw new Error(`Cell count must match column count. Found ${numberOfCellsInRow} cells and ${this.columnCount} columns.`);
}
}
}

// Remove deleted nodes and their children from the key map
Expand Down
6 changes: 6 additions & 0 deletions packages/@react-stately/table/src/Row.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ Row.getCollectionNode = function* getCollectionNode<T>(props: RowProps<T>, conte
} else {
let cells: PartialNode<T>[] = [];
let childRows: PartialNode<T>[] = [];
let columnCount = 0;
React.Children.forEach(children, node => {
if (node.type === Row) {
if (cells.length < context.columns.length) {
Expand All @@ -87,9 +88,14 @@ Row.getCollectionNode = function* getCollectionNode<T>(props: RowProps<T>, conte
type: 'cell',
element: node
});
columnCount += node.props.colSpan ?? 1;
}
});

if (columnCount !== context.columns.length) {
throw new Error(`Cell count must match column count. Found ${columnCount} cells and ${context.columns.length} columns.`);
}

yield* cells;
yield* childRows;
}
Expand Down