x4js 2.3.7 → 2.3.9

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.
@@ -46,32 +46,52 @@ import { Constructor, isString } from './core_tools';
46
46
  export class x4_react {
47
47
 
48
48
  static create_element( tag: string, props: any, ...content: any[] ): Component;
49
- static create_element<X extends Component>( tag: string | Constructor<X>, props: X["props"], ...content: any[] ) {
50
-
51
- props = props || {};
49
+ static create_element<X extends Component>( tag: string | Constructor<X>, props: X["props"], ...children: any[] ) {
50
+
51
+ // ref is a JSX-only attribute: never passed to the component
52
+ const { ref, ...attrs } = ( props ?? {} ) as any;
53
+
54
+ // flatten {items.map(...)} and drop null / false from conditional children
55
+ const content = children.flat( Infinity )
56
+ .filter( c => c !== null && c !== undefined && c !== false );
52
57
 
53
58
  let el: Component;
54
59
 
55
- // --- simple div ------------------------
56
- if( isString(tag) ) {
60
+ // --- simple html element ------------------------
61
+ if( isString( tag ) ) {
57
62
  el = new Component( { tag } );
58
- Object.entries( props )
59
- .forEach(([name, value]) => {
60
- if (name.startsWith('on') && name.toLowerCase() in window) {
61
- el.dom.addEventListener(name.toLowerCase().substring(2), value)
63
+
64
+ Object.entries( attrs )
65
+ .forEach( ( [name, value]: [string, any] ) => {
66
+ if( name.startsWith( 'on' ) && name.toLowerCase( ) in window ) {
67
+ el.dom.addEventListener( name.toLowerCase( ).substring( 2 ), value );
62
68
  }
63
69
  else {
64
- el.setAttribute(name, value )
70
+ el.setAttribute( name, value );
65
71
  }
66
72
  });
73
+
74
+ if( content.length ) {
75
+ el.setContent( content );
76
+ }
67
77
  }
68
78
  // --- Component ------------------------
69
79
  else {
70
- el = new tag( props );
80
+ // same path as object construction: <X a={1}>{kids}</X> === new X({ a: 1, content: kids })
81
+ el = new tag( content.length ? { ...attrs, content } : attrs );
71
82
  }
72
83
 
73
- if( content && content.length ) {
74
- el.appendContent( content );
84
+ // ref={[owner, "name"]} stores the element in owner.refs.name
85
+ if( ref ) {
86
+ const [owner, name] = ref;
87
+
88
+ // the owner must be a Box: only Box has refs
89
+ if( !owner?.refs || typeof name !== "string" ) {
90
+ console.error( "invalid JSX ref, expected [ownerBox, \"name\"]:", ref );
91
+ }
92
+ else {
93
+ owner.refs[name] = el;
94
+ }
75
95
  }
76
96
 
77
97
  return el;
package/src/x4tsx.d.ts CHANGED
@@ -14,6 +14,7 @@
14
14
  * that can be found in the LICENSE file or at https://opensource.org/licenses/MIT.
15
15
  **/
16
16
 
17
+ import { Box } from './components/boxes/boxes.js';
17
18
  export { }
18
19
 
19
20
  declare global {
@@ -21,6 +22,10 @@ declare global {
21
22
  interface IntrinsicElements {
22
23
  [ name: string ]: any;
23
24
  }
25
+
26
+ interface IntrinsicClassAttributes<T> {
27
+ ref?: [ owner: Box<any, any>, name: string ];
28
+ }
24
29
  }
25
30
  }
26
31