@r2wc/react-to-web-component 2.0.4 → 2.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@r2wc/react-to-web-component",
3
- "version": "2.0.4",
3
+ "version": "2.1.1",
4
4
  "description": "Convert React components to native Web Components.",
5
5
  "homepage": "https://www.bitovi.com/open-source/react-to-web-component",
6
6
  "author": "Bitovi",
@@ -42,7 +42,7 @@
42
42
  "build": "vite build"
43
43
  },
44
44
  "dependencies": {
45
- "@r2wc/core": "^1.0.0"
45
+ "@r2wc/core": "^1.3.1"
46
46
  },
47
47
  "devDependencies": {
48
48
  "@types/react": "^18.0.0",
@@ -4,6 +4,8 @@ import PropTypes from "prop-types"
4
4
  import React from "react"
5
5
  import { describe, it, expect, assert } from "vitest"
6
6
 
7
+ import { R2WCElement } from "@r2wc/core"
8
+
7
9
  import r2wc from "./react-to-web-component"
8
10
 
9
11
  expect.extend(matchers)
@@ -363,4 +365,98 @@ describe("react-to-web-component 1", () => {
363
365
  button.click()
364
366
  })
365
367
  })
368
+
369
+ it.each([[undefined], ["open"], ["closed"]])(
370
+ `Supports class function to react props using method transform: (shadow: %s)`,
371
+ async (shadow) => {
372
+ const ClassGreeting: React.FC<{ name: string; sayHello: () => void }> = ({
373
+ name,
374
+ sayHello,
375
+ }) => (
376
+ <div>
377
+ <h1>Hello, {name}</h1>
378
+ <button onClick={sayHello}>Click me</button>
379
+ </div>
380
+ )
381
+
382
+ const WebClassGreeting = r2wc(ClassGreeting, {
383
+ props: {
384
+ name: "string",
385
+ sayHello: "method",
386
+ },
387
+ shadow: shadow as unknown as Exclude<
388
+ Parameters<typeof r2wc>[1],
389
+ undefined
390
+ >["shadow"],
391
+ })
392
+
393
+ const tagName = `class-greeting${shadow ? `-${shadow}` : ""}`
394
+
395
+ customElements.define(tagName, WebClassGreeting)
396
+
397
+ document.body.innerHTML = `<${tagName} name='Christopher'></class-greeting>`
398
+
399
+ const el = document.querySelector<
400
+ R2WCElement & { sayHello?: () => void }
401
+ >(tagName)
402
+
403
+ if (!el) {
404
+ throw new Error("Element not found")
405
+ }
406
+
407
+ const sayHello = function (this: R2WCElement) {
408
+ const nameElement = this.container.querySelector("h1")
409
+ if (nameElement) {
410
+ nameElement.textContent = "Hello, again"
411
+ }
412
+ }
413
+
414
+ el.sayHello = sayHello
415
+
416
+ const docRoot = el.container.getRootNode() as Document | DocumentFragment
417
+
418
+ await new Promise((resolve, reject) => {
419
+ const failIfNotClicked = setTimeout(() => {
420
+ reject()
421
+ }, 1000)
422
+
423
+ setTimeout(() => {
424
+ docRoot.querySelector<HTMLButtonElement>(`button`)?.click()
425
+
426
+ setTimeout(() => {
427
+ const element = docRoot.querySelector("h1")
428
+ expect(element?.textContent).toEqual("Hello, again")
429
+ clearTimeout(failIfNotClicked)
430
+ resolve(true)
431
+ }, 0)
432
+ }, 0)
433
+ })
434
+
435
+ const sayHelloRerendered = function (this: R2WCElement) {
436
+ const nameElement = this.container.querySelector("h1")
437
+ if (nameElement) {
438
+ nameElement.textContent = "Hello, again rerendered"
439
+ }
440
+ }
441
+
442
+ el.sayHello = sayHelloRerendered
443
+
444
+ await new Promise((resolve, reject) => {
445
+ const failIfNotClicked = setTimeout(() => {
446
+ reject()
447
+ }, 1000)
448
+
449
+ setTimeout(() => {
450
+ docRoot.querySelector<HTMLButtonElement>(`button`)?.click()
451
+
452
+ setTimeout(() => {
453
+ const element = docRoot.querySelector<HTMLHeadingElement>("h1")
454
+ expect(element?.textContent).toEqual("Hello, again rerendered")
455
+ clearTimeout(failIfNotClicked)
456
+ resolve(true)
457
+ }, 0)
458
+ }, 0)
459
+ })
460
+ },
461
+ )
366
462
  })