UNPKG

piral-react

Version:

Plugin for integrating React 16+ components in Piral.

63 lines (50 loc) 1.29 kB
import type { ExtensionSlotProps, PiletApi } from 'piral-core'; import { createElement, Component } from 'react'; import { anyPropType } from './mount'; function compareObjects(a: any, b: any) { for (const i in a) { if (!(i in b)) { return false; } } for (const i in b) { if (!compare(a[i], b[i])) { return false; } } return true; } function compare<T>(a: T, b: T) { if (a !== b) { const ta = typeof a; const tb = typeof b; if (ta === tb && ta === 'object' && a && b) { return compareObjects(a, b); } return false; } return true; } export function createExtension(rootName: string) { const ReactExtension: any = class extends Component<ExtensionSlotProps> { static contextTypes = { piral: anyPropType, }; private onRefChange = (element: HTMLElement) => { if (element) { const { piral } = this.context as { piral: PiletApi }; element.innerHTML = ''; piral.renderHtmlExtension(element, this.props); } }; shouldComponentUpdate(nextProps: ExtensionSlotProps) { return !compare(this.props, nextProps); } render() { return createElement(rootName, { ref: this.onRefChange, }); } }; return ReactExtension; }