@pepperi-addons/ngx-composite-lib-react 0.0.2 → 0.0.3

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": "@pepperi-addons/ngx-composite-lib-react",
3
- "version": "0.0.2",
3
+ "version": "0.0.3",
4
4
  "description": "React wrappers for @pepperi-addons/ngx-composite-lib web components, plus registration bundles.",
5
5
  "type": "module",
6
6
  "main": "index.js",
@@ -11,6 +11,8 @@
11
11
  "*.d.ts",
12
12
  "components/*.js",
13
13
  "components/*.d.ts",
14
+ "utils/*.js",
15
+ "utils/*.d.ts",
14
16
  "elements/*",
15
17
  "README.md"
16
18
  ],
@@ -19,6 +21,7 @@
19
21
  "types": "./index.d.ts",
20
22
  "default": "./index.js"
21
23
  },
24
+ "./components/PepGroupButtonsSettingsReact.js": "./components/PepGroupButtonsSettingsReact.js",
22
25
  "./elements/register.js": "./elements/register.js",
23
26
  "./elements/register.auto.js": "./elements/register.auto.js",
24
27
  "./elements/styles.css": "./elements/styles.css",
@@ -0,0 +1,7 @@
1
+ import React from 'react';
2
+ declare type AnyProps = Record<string, any> & {
3
+ children?: React.ReactNode;
4
+ };
5
+ declare type WebComponent = HTMLElement & Record<string, any>;
6
+ export declare function createPepWrapper<T extends WebComponent = WebComponent>(tagName: string, displayName: string): React.ForwardRefExoticComponent<Pick<AnyProps, string> & React.RefAttributes<T>>;
7
+ export {};
@@ -0,0 +1,46 @@
1
+ import React, { useEffect, useRef, forwardRef } from 'react';
2
+ export function createPepWrapper(tagName, displayName) {
3
+ const Cmp = forwardRef((props, ref) => {
4
+ const localRef = useRef(null);
5
+ const setRef = (el) => {
6
+ localRef.current = el;
7
+ if (typeof ref === 'function')
8
+ ref(el);
9
+ else if (ref && 'current' in ref)
10
+ ref.current = el;
11
+ };
12
+ useEffect(() => {
13
+ const el = localRef.current;
14
+ const listeners = [];
15
+ if (el) {
16
+ Object.entries(props).forEach(([key, value]) => {
17
+ if (key === 'children' || key === 'style' || key === 'className')
18
+ return;
19
+ if (key.startsWith('on') && typeof value === 'function') {
20
+ const eventName = key.slice(2, 3).toLowerCase() + key.slice(3);
21
+ const handler = (e) => value(e);
22
+ el.addEventListener(eventName, handler);
23
+ listeners.push([eventName, handler]);
24
+ }
25
+ else {
26
+ try {
27
+ el[key] = value;
28
+ }
29
+ catch {
30
+ // ignore set errors
31
+ }
32
+ }
33
+ });
34
+ }
35
+ return () => {
36
+ const current = localRef.current;
37
+ listeners.forEach(([n, h]) => current?.removeEventListener(n, h));
38
+ };
39
+ }, [props]);
40
+ const { children, className, style } = props;
41
+ return React.createElement(tagName, { ref: setRef, className, style }, children);
42
+ });
43
+ Cmp.displayName = displayName;
44
+ return Cmp;
45
+ }
46
+ //# sourceMappingURL=create-wrapper.js.map