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

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.0",
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.0"
46
46
  },
47
47
  "devDependencies": {
48
48
  "@types/react": "^18.0.0",
@@ -363,4 +363,91 @@ describe("react-to-web-component 1", () => {
363
363
  button.click()
364
364
  })
365
365
  })
366
+
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
+ )
377
+
378
+ const WebClassGreeting = r2wc(ClassGreeting, {
379
+ props: {
380
+ name: "string",
381
+ sayHello: "method",
382
+ },
383
+ })
384
+
385
+ customElements.define("class-greeting", WebClassGreeting)
386
+
387
+ document.body.innerHTML = `<class-greeting name='Christopher'></class-greeting>`
388
+
389
+ const el = document.querySelector<HTMLElement & { sayHello?: () => void }>(
390
+ "class-greeting",
391
+ )
392
+
393
+ if (!el) {
394
+ throw new Error("Element not found")
395
+ }
396
+
397
+ const sayHello = function (this: HTMLElement) {
398
+ const nameElement = this.querySelector("h1")
399
+ if (nameElement) {
400
+ nameElement.textContent = "Hello, again"
401
+ }
402
+ }
403
+
404
+ el.sayHello = sayHello.bind(el)
405
+
406
+ await new Promise((resolve, reject) => {
407
+ const failIfNotClicked = setTimeout(() => {
408
+ reject()
409
+ }, 1000)
410
+
411
+ setTimeout(() => {
412
+ document
413
+ .querySelector<HTMLButtonElement>("class-greeting button")
414
+ ?.click()
415
+
416
+ setTimeout(() => {
417
+ const element = document.querySelector("h1")
418
+ expect(element?.textContent).toEqual("Hello, again")
419
+ clearTimeout(failIfNotClicked)
420
+ resolve(true)
421
+ }, 0)
422
+ }, 0)
423
+ })
424
+
425
+ const sayHelloRerendered = function (this: HTMLElement) {
426
+ const nameElement = this.querySelector("h1")
427
+ if (nameElement) {
428
+ nameElement.textContent = "Hello, again rerendered"
429
+ }
430
+ }
431
+
432
+ el.sayHello = sayHelloRerendered.bind(el)
433
+
434
+ await new Promise((resolve, reject) => {
435
+ const failIfNotClicked = setTimeout(() => {
436
+ reject()
437
+ }, 1000)
438
+
439
+ setTimeout(() => {
440
+ document
441
+ .querySelector<HTMLButtonElement>("class-greeting button")
442
+ ?.click()
443
+
444
+ setTimeout(() => {
445
+ const element = document.querySelector<HTMLHeadingElement>("h1")
446
+ expect(element?.textContent).toEqual("Hello, again rerendered")
447
+ clearTimeout(failIfNotClicked)
448
+ resolve(true)
449
+ }, 0)
450
+ }, 0)
451
+ })
452
+ })
366
453
  })