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(react-board): allow mutable react board renderable #1304

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 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
12 changes: 6 additions & 6 deletions packages/react-board/src/create-board.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,18 @@ import { reactErrorHandledRendering } from './react-error-handled-render';
import type { IReactBoard, OmitReactBoard } from './types';

export function createBoard(input: OmitReactBoard<IReactBoard>): IReactBoard {
const res: IReactBoard = createRenderableBase<IReactBoard>({
return createRenderableBase<IReactBoard>({
...input,
render(target) {
const renderable = this as IReactBoard;
Copy link
Contributor

Choose a reason for hiding this comment

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

why the cast? why not type render to have this with a correct type?
https://www.typescriptlang.org/docs/handbook/2/functions.html#declaring-this-in-a-function

return baseRender(
res,
renderable,
async () => {
let element = <res.Board />;
const wrapRenderPlugins = getPluginsWithHooks(res, 'wrapRender');
let element = <renderable.Board />;
const wrapRenderPlugins = getPluginsWithHooks(renderable, 'wrapRender');
for (const plugin of wrapRenderPlugins) {
if (plugin.key.plugin?.wrapRender) {
const el = plugin.key.plugin.wrapRender(plugin.props as never, res, element, target);
const el = plugin.key.plugin.wrapRender(plugin.props as never, renderable, element, target);
element = el || element;
}
}
Expand All @@ -23,5 +24,4 @@ export function createBoard(input: OmitReactBoard<IReactBoard>): IReactBoard {
);
},
});
return res;
}
12 changes: 12 additions & 0 deletions packages/react-board/test/create-board.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,16 @@ describe('create board', () => {
// await board.render(canvas);
await expect(board.render(canvas)).not.to.be.rejected;
});

it('should allow render of renderable to access state in case of mutation', async () => {
const { canvas, cleanup } = board.setupStage();
disposables.add(cleanup);

const mutatedBoard = { ...board, Board: () => 'I was mutated' };

const cleanupRender = await mutatedBoard.render(canvas);
disposables.add(cleanupRender);

expect(canvas.innerHTML).to.include('I was mutated');
});
});
Loading