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: make useRenderer hook synchronous #180

Merged
merged 3 commits into from
Dec 15, 2023
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
39 changes: 39 additions & 0 deletions dev/pages/Grid.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { Grid, type GridDataProvider } from '../../src/Grid.js';
import { GridSelectionColumn } from '../../src/GridSelectionColumn.js';
import { GridTreeColumn } from '../../src/GridTreeColumn.js';
import { GridColumn } from '../../src/GridColumn.js';
import { Tooltip } from '../../src/Tooltip.js';

type Item = {
name: string;
children: boolean;
};

const dataProvider: GridDataProvider<Item> = ({ parentItem, page, pageSize }, cb) => {
const levelSize = parentItem ? 5 : 100;

const pageItems = [...Array(Math.min(levelSize, pageSize))].map((_, i) => {
const indexInLevel = page * pageSize + i;

return {
name: `${parentItem ? parentItem.name + '-' : ''}${indexInLevel}`,
children: true,
};
});

cb(pageItems, levelSize);
};

export default function () {
return (
<Grid itemIdPath="name" dataProvider={dataProvider}>
<GridSelectionColumn frozen autoSelect dragSelect />
<GridTreeColumn frozen path="name" width="200px" />
<GridColumn path="name" width="200px" />
<GridColumn path="name" width="200px" />
<GridColumn path="name" width="200px" />

<Tooltip slot="tooltip" hoverDelay={500} hideDelay={500} />
</Grid>
);
}
4 changes: 2 additions & 2 deletions src/renderers/useRenderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
useCallback,
useReducer,
} from 'react';
import { createPortal } from 'react-dom';
import { createPortal, flushSync } from 'react-dom';
import type { Slice, WebComponentRenderer } from './renderer.js';

export type UseRendererResult<W extends WebComponentRenderer> = readonly [
Expand All @@ -34,7 +34,7 @@ export function useRenderer<P extends {}, W extends WebComponentRenderer>(
convert?: (props: Slice<Parameters<W>, 1>) => PropsWithChildren<P>,
): UseRendererResult<W> {
const [map, update] = useReducer<typeof rendererReducer<W>>(rendererReducer, initialState);
const renderer = useCallback(((...args: Parameters<W>) => update(args)) as W, []);
const renderer = useCallback(((...args: Parameters<W>) => flushSync(() => update(args))) as W, []);

return reactRendererOrNode
? [
Expand Down
14 changes: 14 additions & 0 deletions test/Grid.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { GridSortColumn } from '../src/GridSortColumn.js';
import type { GridBodyReactRendererProps } from '../src/renderers/grid.js';
import catchRender from './utils/catchRender.js';
import { GridColumnGroup } from '../src/GridColumnGroup.js';
import { findByQuerySelector } from './utils/findByQuerySelector.js';

useChaiPlugin(chaiDom);

Expand Down Expand Up @@ -120,6 +121,19 @@ describe('Grid', () => {
expect(surnameBodyCell2).to.have.text('Starr');
expect(roleBodyCell2).to.have.text('drums');
});

it('should consider custom renderer content with column auto-width', async () => {
render(
<Grid<Item> items={items}>
<GridColumn<Item> header="name" autoWidth flexGrow={0}>
{({ item }) => <button style={{ width: '300px' }}>{item.name}</button>}
</GridColumn>
</Grid>,
);

const column = await findByQuerySelector('vaadin-grid-column');
expect(parseFloat(String(column.width))).to.be.greaterThan(300);
});
});

describe('GridFilterColumn', () => {
Expand Down