| 73 | export const ReactPartContext = React.createContext<{}>({}); |
| 74 | |
| 75 | export class ReactPart< |
| 76 | P extends object, |
| 77 | C extends object = {}, |
| 78 | > implements IFrameworkPart { |
| 79 | private _initialProps: Parameters = {}; |
| 80 | private componentInstance?: IPanelWrapperRef; |
| 81 | private ref?: { |
| 82 | portal: React.ReactPortal; |
| 83 | disposable: DockviewIDisposable; |
| 84 | }; |
| 85 | private disposed = false; |
| 86 | |
| 87 | constructor( |
| 88 | private readonly parent: HTMLElement, |
| 89 | private readonly portalStore: ReactPortalStore, |
| 90 | private readonly component: React.FunctionComponent<P>, |
| 91 | private readonly parameters: P, |
| 92 | private readonly context?: C |
| 93 | ) { |
| 94 | this.createPortal(); |
| 95 | } |
| 96 | |
| 97 | public update(props: { [index: string]: any }) { |
| 98 | if (this.disposed) { |
| 99 | throw new Error('invalid operation: resource is already disposed'); |
| 100 | } |
| 101 | |
| 102 | if (!this.componentInstance) { |
| 103 | // if the component is yet to be mounted store the props |
| 104 | this._initialProps = { ...this._initialProps, ...props }; |
| 105 | } else { |
| 106 | this.componentInstance.update(props); |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | private createPortal() { |
| 111 | if (this.disposed) { |
| 112 | throw new Error('invalid operation: resource is already disposed'); |
| 113 | } |
| 114 | |
| 115 | if (!isReactComponent(this.component)) { |
| 116 | /** |
| 117 | * we know this isn't a React.FunctionComponent so throw an error here. |
| 118 | * if we do not intercept then React library will throw a very obsure error |
| 119 | * for the same reason... at least at this point we will emit a sensible stacktrace. |
| 120 | */ |
| 121 | throw new Error( |
| 122 | 'Dockview: Only React.memo(...), React.ForwardRef(...) and functional components are accepted as components' |
| 123 | ); |
| 124 | } |
| 125 | |
| 126 | const bridgeComponent = React.createElement( |
| 127 | React.forwardRef(ReactComponentBridge), |
| 128 | { |
| 129 | component: this |
| 130 | .component as unknown as React.FunctionComponent<{}>, |
| 131 | componentProps: this.parameters as unknown as {}, |
| 132 | ref: (element: IPanelWrapperRef) => { |
nothing calls this directly
no outgoing calls
no test coverage detected
searching dependent graphs…