Skip to content

Commit b78a048

Browse files
authored
Merge branch 'main' into externaldimensions
2 parents cae48cf + e533510 commit b78a048

16 files changed

Lines changed: 116 additions & 153 deletions

src/Cell.tsx

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,7 @@ function Cell<R, SR>({
3939
const { cellClass } = column;
4040
className = getCellClassname(
4141
column,
42-
{
43-
[cellDraggedOverClassname]: isDraggedOver
44-
},
42+
isDraggedOver && cellDraggedOverClassname,
4543
typeof cellClass === 'function' ? cellClass(row) : cellClass,
4644
className
4745
);

src/DataGrid.tsx

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1170,13 +1170,7 @@ export function DataGrid<R, SR = unknown, K extends Key = Key>(props: DataGridPr
11701170
// Scrollable containers without tabIndex are keyboard focusable in Chrome only if there is no focusable element inside
11711171
// whereas they are always focusable in Firefox. We need to set tabIndex to have a consistent behavior across browsers.
11721172
tabIndex={-1}
1173-
className={classnames(
1174-
rootClassname,
1175-
{
1176-
[viewportDraggingClassname]: isDragging
1177-
},
1178-
className
1179-
)}
1173+
className={classnames(rootClassname, isDragging && viewportDraggingClassname, className)}
11801174
style={{
11811175
...style,
11821176
// set scrollPadding to correctly scroll to non-sticky cells/rows

src/HeaderCell.tsx

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -116,13 +116,15 @@ export default function HeaderCell<R, SR>({
116116
sortDirection && !priority ? (sortDirection === 'ASC' ? 'ascending' : 'descending') : undefined;
117117
const { sortable, resizable, draggable } = column;
118118

119-
const className = getCellClassname(column, column.headerCellClass, {
120-
[cellSortableClassname]: sortable,
121-
[cellResizableClassname]: resizable,
122-
[cellDraggableClassname]: draggable,
123-
[cellDraggingClassname]: isDragging,
124-
[cellOverClassname]: isOver
125-
});
119+
const className = getCellClassname(
120+
column,
121+
column.headerCellClass,
122+
sortable && cellSortableClassname,
123+
resizable && cellResizableClassname,
124+
draggable && cellDraggableClassname,
125+
isDragging && cellDraggingClassname,
126+
isOver && cellOverClassname
127+
);
126128

127129
function onSort(ctrlClick: boolean) {
128130
if (onSortColumnsChange == null) return;

src/cellRenderers/renderValue.tsx

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
11
import type { RenderCellProps } from '../types';
22

33
export function renderValue<R, SR>(props: RenderCellProps<R, SR>) {
4-
try {
5-
return props.row[props.column.key as keyof R] as React.ReactNode;
6-
} catch {
7-
return null;
8-
}
4+
return props.row?.[props.column.key as keyof R] as React.ReactNode;
95
}

src/utils/styleUtils.ts

Lines changed: 5 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -38,37 +38,23 @@ export function getCellStyle<R, SR>(
3838
};
3939
}
4040

41-
type ClassValue = Maybe<string> | Record<string, boolean> | false;
41+
type ClassValue = Maybe<string | false>;
4242

4343
export function classnames(...args: readonly ClassValue[]) {
4444
let classname = '';
4545

4646
for (const arg of args) {
47-
if (arg) {
48-
if (typeof arg === 'string') {
49-
classname += ` ${arg}`;
50-
} else if (typeof arg === 'object') {
51-
for (const key in arg) {
52-
if (arg[key]) {
53-
classname += ` ${key}`;
54-
}
55-
}
56-
}
47+
if (typeof arg === 'string') {
48+
classname += ` ${arg}`;
5749
}
5850
}
5951

60-
return classname.trimStart();
52+
return classname.slice(1);
6153
}
6254

6355
export function getCellClassname<R, SR>(
6456
column: CalculatedColumn<R, SR>,
6557
...extraClasses: readonly ClassValue[]
6658
): string {
67-
return classnames(
68-
cellClassname,
69-
{
70-
[cellFrozenClassname]: column.frozen
71-
},
72-
...extraClasses
73-
);
59+
return classnames(cellClassname, column.frozen && cellFrozenClassname, ...extraClasses);
7460
}

test/browser/column/grouping.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { page, userEvent } from 'vitest/browser';
22

33
import type { ColumnOrColumnGroup } from '../../../src';
4-
import { safeTab, setup, tabIntoGrid, testCount, validateCellPosition } from '../utils';
4+
import { safeTab, setup, testCount, validateCellPosition } from '../utils';
55

66
const grid = page.getGrid();
77
const headerRows = grid.getHeaderRow();
@@ -252,12 +252,12 @@ test('grouping', async () => {
252252
});
253253

254254
test('keyboard navigation', async () => {
255-
await setup({ columns, rows: [{}] }, true);
255+
await setup({ columns, rows: [{}] });
256256

257257
// no initial active position
258258
await expect.element(grid.getActiveCell()).not.toBeInTheDocument();
259259

260-
await tabIntoGrid();
260+
await safeTab();
261261
await validateCellPosition(0, 3);
262262

263263
// arrow navigation

test/browser/direction.test.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { page, userEvent } from 'vitest/browser';
22

33
import type { Column } from '../../src';
4-
import { setup, tabIntoGrid } from './utils';
4+
import { safeTab, setup } from './utils';
55

66
const grid = page.getGrid();
77
const activeCell = grid.getActiveCell();
@@ -25,27 +25,27 @@ const columns: readonly Column<Row>[] = [
2525
const rows: readonly Row[] = [];
2626

2727
test('should use left to right direction by default', async () => {
28-
await setup({ rows, columns }, true);
28+
await setup({ rows, columns });
2929
await expect.element(grid).toHaveAttribute('dir', 'ltr');
30-
await tabIntoGrid();
30+
await safeTab();
3131
await expect.element(activeCell).toHaveTextContent('ID');
3232
await userEvent.keyboard('{ArrowRight}');
3333
await expect.element(activeCell).toHaveTextContent('Name');
3434
});
3535

3636
test('should use left to right direction if direction prop is set to ltr', async () => {
37-
await setup({ rows, columns, direction: 'ltr' }, true);
37+
await setup({ rows, columns, direction: 'ltr' });
3838
await expect.element(grid).toHaveAttribute('dir', 'ltr');
39-
await tabIntoGrid();
39+
await safeTab();
4040
await expect.element(activeCell).toHaveTextContent('ID');
4141
await userEvent.keyboard('{ArrowRight}');
4242
await expect.element(activeCell).toHaveTextContent('Name');
4343
});
4444

4545
test('should use right to left direction if direction prop is set to rtl', async () => {
46-
await setup({ rows, columns, direction: 'rtl' }, true);
46+
await setup({ rows, columns, direction: 'rtl' });
4747
await expect.element(grid).toHaveAttribute('dir', 'rtl');
48-
await tabIntoGrid();
48+
await safeTab();
4949
await expect.element(activeCell).toHaveTextContent('ID');
5050
await userEvent.keyboard('{ArrowLeft}');
5151
await expect.element(activeCell).toHaveTextContent('Name');

test/browser/dragFill.test.tsx

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -88,9 +88,14 @@ test('should allow drag up using mouse', async () => {
8888

8989
test('should focus the cell when drag handle is clicked', async () => {
9090
await setup();
91-
await userEvent.click(getCellsAtRowIndex(0).nth(0));
92-
await userEvent.click(document.body);
93-
await expect.element(document.body).toHaveFocus();
91+
const cell = getCellsAtRowIndex(0).nth(0);
92+
93+
await userEvent.click(cell);
94+
await expect.element(cell).toHaveFocus();
95+
96+
cell.element().blur();
97+
await expect.element(cell).not.toHaveFocus();
98+
9499
await userEvent.click(dragHandle);
95-
await expect.element(getCellsAtRowIndex(0).nth(0)).toHaveFocus();
100+
await expect.element(cell).toHaveFocus();
96101
});

test/browser/events.test.tsx

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { page, userEvent } from 'vitest/browser';
22

33
import { DataGrid } from '../../src';
4-
import type { Column, DataGridProps } from '../../src';
4+
import type { Column } from '../../src';
55
import { safeTab } from './utils';
66

77
interface Row {
@@ -55,7 +55,9 @@ const rows: readonly Row[] = [
5555
describe('Events', () => {
5656
it('should not select cell if onCellMouseDown prevents grid default', async () => {
5757
await page.render(
58-
<EventTest
58+
<DataGrid
59+
columns={columns}
60+
rows={rows}
5961
onCellMouseDown={(args, event) => {
6062
if (args.column.key === 'col1') {
6163
event.preventGridDefault();
@@ -71,7 +73,9 @@ describe('Events', () => {
7173

7274
it('should be able to open editor editor on single click using onCellClick', async () => {
7375
await page.render(
74-
<EventTest
76+
<DataGrid
77+
columns={columns}
78+
rows={rows}
7579
onCellClick={(args, event) => {
7680
if (args.column.key === 'col2') {
7781
event.preventGridDefault();
@@ -88,7 +92,9 @@ describe('Events', () => {
8892

8993
it('should not open editor editor on double click if onCellDoubleClick prevents default', async () => {
9094
await page.render(
91-
<EventTest
95+
<DataGrid
96+
columns={columns}
97+
rows={rows}
9298
onCellDoubleClick={(args, event) => {
9399
if (args.column.key === 'col1') {
94100
event.preventGridDefault();
@@ -104,7 +110,9 @@ describe('Events', () => {
104110

105111
it('should call onCellContextMenu when cell is right clicked', async () => {
106112
const onCellContextMenu = vi.fn();
107-
await page.render(<EventTest onCellContextMenu={onCellContextMenu} />);
113+
await page.render(
114+
<DataGrid columns={columns} rows={rows} onCellContextMenu={onCellContextMenu} />
115+
);
108116
expect(onCellContextMenu).not.toHaveBeenCalled();
109117
await userEvent.click(page.getCell({ name: '1' }), { button: 'right' });
110118
expect(onCellContextMenu).toHaveBeenCalledExactlyOnceWith(
@@ -122,7 +130,9 @@ describe('Events', () => {
122130
it('should call onActivePositionChange when cell selection is changed', async () => {
123131
const onActivePositionChange = vi.fn();
124132

125-
await page.render(<EventTest onActivePositionChange={onActivePositionChange} />);
133+
await page.render(
134+
<DataGrid columns={columns} rows={rows} onActivePositionChange={onActivePositionChange} />
135+
);
126136

127137
expect(onActivePositionChange).not.toHaveBeenCalled();
128138

@@ -181,16 +191,3 @@ describe('Events', () => {
181191
expect(onActivePositionChange).toHaveBeenCalledTimes(6);
182192
});
183193
});
184-
185-
type EventProps = Pick<
186-
DataGridProps<Row>,
187-
| 'onCellMouseDown'
188-
| 'onCellClick'
189-
| 'onCellDoubleClick'
190-
| 'onCellContextMenu'
191-
| 'onActivePositionChange'
192-
>;
193-
194-
function EventTest(props: EventProps) {
195-
return <DataGrid columns={columns} rows={rows} {...props} />;
196-
}

0 commit comments

Comments
 (0)