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

feat(react): allow html attrs in react renderer #3812

Merged
merged 1 commit into from
Mar 3, 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
2 changes: 2 additions & 0 deletions packages/react/src/ReactNodeViewRenderer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export interface ReactNodeViewRendererOptions extends NodeViewRendererOptions {
| null
as?: string
className?: string
attrs?: Record<string, string>
}

class ReactNodeView extends NodeView<
Expand Down Expand Up @@ -103,6 +104,7 @@ class ReactNodeView extends NodeView<
props,
as,
className: `node-${this.node.type.name} ${className}`.trim(),
attrs: this.options.attrs,
})
}

Expand Down
8 changes: 8 additions & 0 deletions packages/react/src/ReactRenderer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export interface ReactRendererOptions {
props?: Record<string, any>,
as?: string,
className?: string,
attrs?: Record<string, string>,
}

type ComponentType<R, P> =
Expand Down Expand Up @@ -50,6 +51,7 @@ export class ReactRenderer<R = unknown, P = unknown> {
props = {},
as = 'div',
className = '',
attrs,
}: ReactRendererOptions) {
this.id = Math.floor(Math.random() * 0xFFFFFFFF).toString()
this.component = component
Expand All @@ -62,6 +64,12 @@ export class ReactRenderer<R = unknown, P = unknown> {
this.element.classList.add(...className.split(' '))
}

if (attrs) {
Object.keys(attrs).forEach(key => {
this.element.setAttribute(key, attrs[key])
})
}

this.render()
}

Expand Down