Skip to content

Commit

Permalink
fix: forwardref 组件不能选中的问题
Browse files Browse the repository at this point in the history
  • Loading branch information
rainke committed Oct 17, 2023
1 parent a96961f commit d4822af
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
23 changes: 21 additions & 2 deletions packages/renderer-core/src/hoc/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,25 @@ import { cloneEnumerableProperty } from '@alilc/lowcode-utils';
import adapter from '../adapter';
import { IBaseRendererInstance, IRendererProps } from '../types';

function patchDidCatch(Comp: any, { baseRenderer }: { baseRenderer: IBaseRendererInstance }) {
interface Options {
baseRenderer: IBaseRendererInstance;
schema: any;
}

function patchDidMount(Comp: any, { baseRenderer, schema }: Options) {
if (Comp.patchedMount) {
return;
}
Comp.patchedMount = true;
const { engine } = baseRenderer.context;
const originalDidMount = Comp.prototype.componentDidMount;
Comp.prototype.componentDidMount = function () {
engine.props?.onCompGetRef(schema, this);
return originalDidMount && originalDidMount.call(this);
};
}

function patchDidCatch(Comp: any, { baseRenderer }: Options) {
if (Comp.patchedCatch) {
return;
}
Expand Down Expand Up @@ -44,7 +62,7 @@ function patchDidCatch(Comp: any, { baseRenderer }: { baseRenderer: IBaseRendere
}
}

export function compWrapper(Comp: any, options: { baseRenderer: IBaseRendererInstance }) {
export function compWrapper(Comp: any, options: Options) {
const { createElement, Component, forwardRef } = adapter.getRuntime();
if (
Comp?.prototype?.isReactComponent || // react
Expand All @@ -62,6 +80,7 @@ export function compWrapper(Comp: any, options: { baseRenderer: IBaseRendererIns
(Wrapper as any).displayName = Comp.displayName;

patchDidCatch(Wrapper, options);
patchDidMount(Wrapper, options);

return cloneEnumerableProperty(
forwardRef((props: any, ref: any) => {
Expand Down
2 changes: 1 addition & 1 deletion packages/renderer-core/src/renderer/base.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -615,7 +615,7 @@ export default function baseRendererFactory(): IBaseRenderComponent {
});
});

Comp = compWrapper(Comp, { baseRenderer: this });
Comp = compWrapper(Comp, { baseRenderer: this, schema });
components[schema.componentName] = Comp;

otherProps.ref = (ref: any) => {
Expand Down

0 comments on commit d4822af

Please sign in to comment.