Skip to content

Commit 3d33589

Browse files
committed
Merge branch 'dutchcelt-feature/toggle-the-use-of-useImperativeHandle'
2 parents bfa6b12 + a6d7cec commit 3d33589

File tree

1 file changed

+24
-44
lines changed

1 file changed

+24
-44
lines changed

packages/react-wrappers/src/wrapper-generator.ts

Lines changed: 24 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -332,14 +332,14 @@ function getReactComponentTemplate(
332332

333333
return `
334334
${config.ssrSafe ? '"use client"' : ""}
335-
import React, { forwardRef, useImperativeHandle ${
335+
import React, { forwardRef ${
336336
useEffect ? ", useRef, useEffect" : ""
337337
} ${config.scopedTags ? ", useContext" : ""} } from "react";
338338
${!config.ssrSafe ? `import '${modulePath}';` : ""}
339339
${
340340
has(eventTemplates) || has(propTemplates)
341-
? `import {
342-
${has(eventTemplates) ? "useEventListener," : ""}
341+
? `import {
342+
${has(eventTemplates) ? "useEventListener," : ""}
343343
${has(propTemplates) ? "useProperties" : ""}
344344
} from './react-utils.js';`
345345
: ""
@@ -370,7 +370,6 @@ function getReactComponentTemplate(
370370
: ""
371371
}
372372
373-
374373
${has(eventTemplates) ? "/** Event listeners - run once */" : ""}
375374
${eventTemplates?.join("") || ""}
376375
@@ -381,27 +380,29 @@ function getReactComponentTemplate(
381380
}
382381
${propTemplates?.join("") || ""}
383382
384-
${
385-
has(methods)
386-
? "/** Methods - uses `useImperativeHandle` hook to pass ref to component */"
387-
: ""
388-
}
389-
useImperativeHandle(forwardedRef, () => ({
390-
${getPublicMethodsForRef(methods)}
391-
}));
392-
393383
return React.createElement(
394384
${getTagName(component)},
395-
{
396-
${useEffect ? "ref," : ""}
385+
{
386+
${
387+
useEffect
388+
? `ref: (node) => {
389+
ref.current = node;
390+
if (typeof forwardedRef === "function") {
391+
forwardedRef(node);
392+
} else if (forwardedRef) {
393+
forwardedRef.current = node;
394+
}
395+
},`
396+
: ""
397+
}
397398
${has(unusedProps) ? "...filteredProps" : "...props"},
398399
${[...attrTemplates, ...booleanAttrTemplates].join(",")},
399400
style: {...props.style},
400401
${globalEvents.map((x) => `${x.event}: props.${x.event}`).join(", ")}
401402
},
402403
props.children
403404
);
404-
});
405+
});
405406
`;
406407
}
407408

@@ -424,20 +425,20 @@ function getTypeDefinitionTemplate(
424425

425426
return `
426427
import React from "react";
427-
import {
428+
import {
428429
${config.defaultExport ? "default" : component.name} as ${
429430
component.name
430431
}Element
431432
${eventTypes?.length ? `, ${eventTypes}` : ""}
432433
} from '${modulePath}';
433434
434-
export type {
435-
${component.name}Element
436-
${eventTypes?.length ? `, ${eventTypes}` : ""}
435+
export type {
436+
${component.name}Element
437+
${eventTypes?.length ? `, ${eventTypes}` : ""}
437438
};
438-
439-
export interface ${component.name}Props ${getExtendedProps()} {
440-
${props}
439+
440+
export interface ${component.name}Props ${getExtendedProps()} {
441+
${props}
441442
}
442443
443444
/**
@@ -460,12 +461,6 @@ function getExtendedProps() {
460461
.join(" | ")}>`;
461462
}
462463

463-
function getMethodParameters(parameters?: Parameter[]) {
464-
return parameters
465-
? "(" + parameters.map((x) => `${x.name}`).join(", ") + ")"
466-
: "()";
467-
}
468-
469464
function getPropsInterface(
470465
componentName: string,
471466
booleanAttributes: MappedAttribute[],
@@ -508,21 +503,6 @@ function getTagName(component: Component) {
508503
: `"${component.tagName}"`;
509504
}
510505

511-
function getPublicMethodsForRef(methods: ClassMethod[]) {
512-
return (
513-
methods
514-
?.map(
515-
(method) =>
516-
`${method.name}: ${getMethodParameters(
517-
method.parameters,
518-
)} => ref.current.${method.name}${getMethodParameters(
519-
method.parameters,
520-
)}`,
521-
)
522-
.join(",\n") || ""
523-
);
524-
}
525-
526506
function getBooleanPropsTemplate(booleanAttributes: MappedAttribute[]) {
527507
return (
528508
booleanAttributes?.map(

0 commit comments

Comments
 (0)