@r2wc/react-to-web-component 2.1.0 → 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.1.0",
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.3.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)
@@ -364,90 +366,97 @@ describe("react-to-web-component 1", () => {
364
366
  })
365
367
  })
366
368
 
367
- it("Supports class function to react props using method transform", async () => {
368
- const ClassGreeting: React.FC<{ name: string; sayHello: () => void }> = ({
369
- name,
370
- sayHello,
371
- }) => (
372
- <div>
373
- <h1>Hello, {name}</h1>
374
- <button onClick={sayHello}>Click me</button>
375
- </div>
376
- )
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
+ )
377
381
 
378
- const WebClassGreeting = r2wc(ClassGreeting, {
379
- props: {
380
- name: "string",
381
- sayHello: "method",
382
- },
383
- })
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
+ })
384
392
 
385
- customElements.define("class-greeting", WebClassGreeting)
393
+ const tagName = `class-greeting${shadow ? `-${shadow}` : ""}`
386
394
 
387
- document.body.innerHTML = `<class-greeting name='Christopher'></class-greeting>`
395
+ customElements.define(tagName, WebClassGreeting)
388
396
 
389
- const el = document.querySelector<HTMLElement & { sayHello?: () => void }>(
390
- "class-greeting",
391
- )
397
+ document.body.innerHTML = `<${tagName} name='Christopher'></class-greeting>`
392
398
 
393
- if (!el) {
394
- throw new Error("Element not found")
395
- }
399
+ const el = document.querySelector<
400
+ R2WCElement & { sayHello?: () => void }
401
+ >(tagName)
396
402
 
397
- const sayHello = function (this: HTMLElement) {
398
- const nameElement = this.querySelector("h1")
399
- if (nameElement) {
400
- nameElement.textContent = "Hello, again"
403
+ if (!el) {
404
+ throw new Error("Element not found")
401
405
  }
402
- }
403
406
 
404
- el.sayHello = sayHello.bind(el)
407
+ const sayHello = function (this: R2WCElement) {
408
+ const nameElement = this.container.querySelector("h1")
409
+ if (nameElement) {
410
+ nameElement.textContent = "Hello, again"
411
+ }
412
+ }
405
413
 
406
- await new Promise((resolve, reject) => {
407
- const failIfNotClicked = setTimeout(() => {
408
- reject()
409
- }, 1000)
414
+ el.sayHello = sayHello
410
415
 
411
- setTimeout(() => {
412
- document
413
- .querySelector<HTMLButtonElement>("class-greeting button")
414
- ?.click()
416
+ const docRoot = el.container.getRootNode() as Document | DocumentFragment
417
+
418
+ await new Promise((resolve, reject) => {
419
+ const failIfNotClicked = setTimeout(() => {
420
+ reject()
421
+ }, 1000)
415
422
 
416
423
  setTimeout(() => {
417
- const element = document.querySelector("h1")
418
- expect(element?.textContent).toEqual("Hello, again")
419
- clearTimeout(failIfNotClicked)
420
- resolve(true)
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)
421
432
  }, 0)
422
- }, 0)
423
- })
433
+ })
424
434
 
425
- const sayHelloRerendered = function (this: HTMLElement) {
426
- const nameElement = this.querySelector("h1")
427
- if (nameElement) {
428
- nameElement.textContent = "Hello, again rerendered"
435
+ const sayHelloRerendered = function (this: R2WCElement) {
436
+ const nameElement = this.container.querySelector("h1")
437
+ if (nameElement) {
438
+ nameElement.textContent = "Hello, again rerendered"
439
+ }
429
440
  }
430
- }
431
-
432
- el.sayHello = sayHelloRerendered.bind(el)
433
441
 
434
- await new Promise((resolve, reject) => {
435
- const failIfNotClicked = setTimeout(() => {
436
- reject()
437
- }, 1000)
442
+ el.sayHello = sayHelloRerendered
438
443
 
439
- setTimeout(() => {
440
- document
441
- .querySelector<HTMLButtonElement>("class-greeting button")
442
- ?.click()
444
+ await new Promise((resolve, reject) => {
445
+ const failIfNotClicked = setTimeout(() => {
446
+ reject()
447
+ }, 1000)
443
448
 
444
449
  setTimeout(() => {
445
- const element = document.querySelector<HTMLHeadingElement>("h1")
446
- expect(element?.textContent).toEqual("Hello, again rerendered")
447
- clearTimeout(failIfNotClicked)
448
- resolve(true)
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)
449
458
  }, 0)
450
- }, 0)
451
- })
452
- })
459
+ })
460
+ },
461
+ )
453
462
  })