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

fix: remove incorrect forwardRef from Preact #1170

Merged
merged 3 commits into from
May 15, 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
176 changes: 84 additions & 92 deletions packages/core/src/__tests__/__snapshots__/preact.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -1643,31 +1643,29 @@ export default MyBasicForNoTagRefComponent;
exports[`Preact > jsx > Javascript Test > basicForwardRef 1`] = `
"/** @jsx h */
import { h, Fragment } from \\"preact\\";
import { useState, forwardRef } from \\"preact/hooks\\";
import { useState } from \\"preact/hooks\\";

const MyBasicForwardRefComponent = forwardRef(
function MyBasicForwardRefComponent(props, inputRef) {
const [name, setName] = useState(() => \\"PatrickJS\\");
function MyBasicForwardRefComponent(props) {
const [name, setName] = useState(() => \\"PatrickJS\\");

return (
<Fragment>
<div>
<input
className=\\"input\\"
ref={inputRef}
value={name}
onChange={(event) => setName(event.target.value)}
/>
</div>
<style jsx>{\`
.input {
color: red;
}
\`}</style>
</Fragment>
);
}
);
return (
<Fragment>
<div>
<input
className=\\"input\\"
ref={props.inputRef}
value={name}
onChange={(event) => setName(event.target.value)}
/>
</div>
<style jsx>{\`
.input {
color: red;
}
\`}</style>
</Fragment>
);
}

export default MyBasicForwardRefComponent;
"
Expand All @@ -1676,31 +1674,29 @@ export default MyBasicForwardRefComponent;
exports[`Preact > jsx > Javascript Test > basicForwardRefMetadata 1`] = `
"/** @jsx h */
import { h, Fragment } from \\"preact\\";
import { useState, forwardRef } from \\"preact/hooks\\";
import { useState } from \\"preact/hooks\\";

const MyBasicForwardRefComponent = forwardRef(
function MyBasicForwardRefComponent(props, inputRef) {
const [name, setName] = useState(() => \\"PatrickJS\\");
function MyBasicForwardRefComponent(props) {
const [name, setName] = useState(() => \\"PatrickJS\\");

return (
<Fragment>
<div>
<input
className=\\"input\\"
ref={inputRef}
value={name}
onChange={(event) => setName(event.target.value)}
/>
</div>
<style jsx>{\`
.input {
color: red;
}
\`}</style>
</Fragment>
);
}
);
return (
<Fragment>
<div>
<input
className=\\"input\\"
ref={props.inputRef}
value={name}
onChange={(event) => setName(event.target.value)}
/>
</div>
<style jsx>{\`
.input {
color: red;
}
\`}</style>
</Fragment>
);
}

export default MyBasicForwardRefComponent;
"
Expand Down Expand Up @@ -5059,36 +5055,34 @@ export default MyBasicForNoTagRefComponent;
exports[`Preact > jsx > Typescript Test > basicForwardRef 1`] = `
"/** @jsx h */
import { h, Fragment } from \\"preact\\";
import { useState, forwardRef } from \\"preact/hooks\\";
import { useState } from \\"preact/hooks\\";

export interface Props {
showInput: boolean;
inputRef: HTMLInputElement;
}

const MyBasicForwardRefComponent = forwardRef<Props[\\"inputRef\\"]>(
function MyBasicForwardRefComponent(props: Props, inputRef) {
const [name, setName] = useState(() => \\"PatrickJS\\");
function MyBasicForwardRefComponent(props: Props) {
const [name, setName] = useState(() => \\"PatrickJS\\");

return (
<Fragment>
<div>
<input
className=\\"input\\"
ref={inputRef}
value={name}
onChange={(event) => setName(event.target.value)}
/>
</div>
<style jsx>{\`
.input {
color: red;
}
\`}</style>
</Fragment>
);
}
);
return (
<Fragment>
<div>
<input
className=\\"input\\"
ref={props.inputRef}
value={name}
onChange={(event) => setName(event.target.value)}
/>
</div>
<style jsx>{\`
.input {
color: red;
}
\`}</style>
</Fragment>
);
}

export default MyBasicForwardRefComponent;
"
Expand All @@ -5097,36 +5091,34 @@ export default MyBasicForwardRefComponent;
exports[`Preact > jsx > Typescript Test > basicForwardRefMetadata 1`] = `
"/** @jsx h */
import { h, Fragment } from \\"preact\\";
import { useState, forwardRef } from \\"preact/hooks\\";
import { useState } from \\"preact/hooks\\";

export interface Props {
showInput: boolean;
inputRef: HTMLInputElement;
}

const MyBasicForwardRefComponent = forwardRef<Props[\\"inputRef\\"]>(
function MyBasicForwardRefComponent(props: Props, inputRef) {
const [name, setName] = useState(() => \\"PatrickJS\\");
function MyBasicForwardRefComponent(props: Props) {
const [name, setName] = useState(() => \\"PatrickJS\\");

return (
<Fragment>
<div>
<input
className=\\"input\\"
ref={inputRef}
value={name}
onChange={(event) => setName(event.target.value)}
/>
</div>
<style jsx>{\`
.input {
color: red;
}
\`}</style>
</Fragment>
);
}
);
return (
<Fragment>
<div>
<input
className=\\"input\\"
ref={props.inputRef}
value={name}
onChange={(event) => setName(event.target.value)}
/>
</div>
<style jsx>{\`
.input {
color: red;
}
\`}</style>
</Fragment>
);
}

export default MyBasicForwardRefComponent;
"
Expand Down
3 changes: 3 additions & 0 deletions packages/core/src/generators/react/blocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,9 @@ const BINDING_MAPPERS: {
| ((key: string, value: string, options?: ToReactOptions) => [string, string]);
} = {
ref(ref, value, options) {
if (options?.preact) {
return [ref, value];
}
const regexp = /(.+)?props\.(.+)( |\)|;|\()?$/m;
if (regexp.test(value)) {
const match = regexp.exec(value);
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/generators/react/generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ const _componentToReact = (
let hasState = checkHasState(json);

const [forwardRef, hasPropRef] = getPropsRef(json);
const isForwardRef = Boolean(json.meta.useMetadata?.forwardRef || hasPropRef);
const isForwardRef = !options.preact && Boolean(json.meta.useMetadata?.forwardRef || hasPropRef);
if (isForwardRef) {
const meta = json.meta.useMetadata?.forwardRef as string;
options.forwardRef = meta || forwardRef;
Expand Down Expand Up @@ -308,7 +308,7 @@ const _componentToReact = (
if (allRefs.length) {
reactLibImports.add('useRef');
}
if (hasPropRef) {
if (!options.preact && hasPropRef) {
reactLibImports.add('forwardRef');
}
if (
Expand Down