@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.
|
|
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.
|
|
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(
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
sayHello
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
<
|
|
375
|
-
|
|
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
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
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
|
-
|
|
393
|
+
const tagName = `class-greeting${shadow ? `-${shadow}` : ""}`
|
|
386
394
|
|
|
387
|
-
|
|
395
|
+
customElements.define(tagName, WebClassGreeting)
|
|
388
396
|
|
|
389
|
-
|
|
390
|
-
"class-greeting",
|
|
391
|
-
)
|
|
397
|
+
document.body.innerHTML = `<${tagName} name='Christopher'></class-greeting>`
|
|
392
398
|
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
399
|
+
const el = document.querySelector<
|
|
400
|
+
R2WCElement & { sayHello?: () => void }
|
|
401
|
+
>(tagName)
|
|
396
402
|
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
if (nameElement) {
|
|
400
|
-
nameElement.textContent = "Hello, again"
|
|
403
|
+
if (!el) {
|
|
404
|
+
throw new Error("Element not found")
|
|
401
405
|
}
|
|
402
|
-
}
|
|
403
406
|
|
|
404
|
-
|
|
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
|
-
|
|
407
|
-
const failIfNotClicked = setTimeout(() => {
|
|
408
|
-
reject()
|
|
409
|
-
}, 1000)
|
|
414
|
+
el.sayHello = sayHello
|
|
410
415
|
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
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
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
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
|
-
}
|
|
423
|
-
})
|
|
433
|
+
})
|
|
424
434
|
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
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
|
-
|
|
435
|
-
const failIfNotClicked = setTimeout(() => {
|
|
436
|
-
reject()
|
|
437
|
-
}, 1000)
|
|
442
|
+
el.sayHello = sayHelloRerendered
|
|
438
443
|
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
444
|
+
await new Promise((resolve, reject) => {
|
|
445
|
+
const failIfNotClicked = setTimeout(() => {
|
|
446
|
+
reject()
|
|
447
|
+
}, 1000)
|
|
443
448
|
|
|
444
449
|
setTimeout(() => {
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
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
|
-
}
|
|
451
|
-
}
|
|
452
|
-
|
|
459
|
+
})
|
|
460
|
+
},
|
|
461
|
+
)
|
|
453
462
|
})
|