Skip to content

Commit 1924229

Browse files
authored
fix: generate d.ts for react wrappers (#2533)
1 parent 23fc88e commit 1924229

File tree

4 files changed

+30
-11
lines changed

4 files changed

+30
-11
lines changed

.changeset/react-dts.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@patternfly/pfe-tools": patch
3+
"@patternfly/elements": patch
4+
---
5+
Generate TypeScript typings for React wrapper components

elements/tsconfig.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88
"pf-icon/icons/**/*",
99
"*/demo/*.js",
1010
"pf-icon/test/rh-icon*.js",
11-
"custom-elements-manifest.config.js"
11+
"custom-elements-manifest.config.js",
12+
"react"
1213
],
1314
"compilerOptions": {
1415
"outDir": ".",

package-lock.json

Lines changed: 5 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tools/pfe-tools/react/generate-wrappers.ts

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type * as CEM from 'custom-elements-manifest';
2-
import javascript from 'dedent';
2+
import dedent from 'dedent';
33
import { dirname, join, relative } from 'node:path';
44
import { fileURLToPath } from 'node:url';
55
import { readFile, writeFile, mkdir } from 'node:fs/promises';
@@ -40,26 +40,39 @@ async function writeReactWrapper(
4040
if (!tagName) {
4141
throw new NonCriticalError(`declaration does not have a tag name: ${decl.name}`);
4242
} else {
43+
const javascript = dedent;
44+
const typescript = dedent;
4345
const { name: Class } = ceExport;
4446
const events = decl.events ?? [];
4547
const outDirPath =
4648
typeof outDirPathOrURL === 'string' ? outDirPathOrURL
4749
: fileURLToPath(outDirPathOrURL);
4850
const outPath = join(outDirPath, path);
4951
await mkdir(dirname(outPath), { recursive: true });
52+
const reactComponentName = getDeprefixedClassName(Class);
53+
const eventsMap = `{${events.map(event => `
54+
${getEventReactPropName(event)}: '${event.name}'`).join(',')}${events.length ? `,
55+
` : ''}}`;
56+
const eventsInterface = eventsMap.replace(/\s+/g, ' ').replaceAll(',', ';').replace('; }', ' }');
5057
await writeFile(outPath, javascript`// ${path}
5158
import { createComponent } from '@lit-labs/react';
5259
import react from 'react';
5360
import { ${Class} as elementClass } from '@patternfly/elements/${module.path}';
54-
export const ${getDeprefixedClassName(Class)} = createComponent({
61+
export const ${reactComponentName} = createComponent({
5562
tagName: '${decl.tagName}',
5663
elementClass,
5764
react,
58-
events: {${events.map(event => `
59-
${getEventReactPropName(event)}: '${event.name}'`).join(',')}${events.length ? `,
60-
` : ''}},
65+
events: ${eventsMap},
6166
});
6267
68+
`, 'utf8');
69+
await writeFile(outPath.replace('.js', '.d.ts'), typescript`// ${path}
70+
declare module '@patternfly/elements/react/pf-button/pf-button.js' {
71+
import type { ReactWebComponent } from '@lit-labs/react';
72+
import type { ${Class} } from '@patternfly/elements/${module.path}';
73+
export const ${reactComponentName}: ReactWebComponent<${Class}, ${eventsInterface}>;
74+
}
75+
6376
`, 'utf8');
6477
return { tagName, outPath };
6578
}

0 commit comments

Comments
 (0)