x4js 2.3.7 → 2.3.8

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": "x4js",
3
- "version": "2.3.7",
3
+ "version": "2.3.8",
4
4
  "type": "module",
5
5
  "main": "src/x4.ts",
6
6
  "module": "src/x4.ts",
@@ -22,10 +22,6 @@ import { addEvent, DOMEventHandler, GlobalDOMEvents, COMPONENT } from './core_do
22
22
  import { Application, EvMessage } from './core_application';
23
23
  import { makeState } from './core_state.js';
24
24
 
25
- interface RefType<T extends Component> {
26
- dom: T;
27
- }
28
-
29
25
  type ComponentAttributes = Record<string,string|number|boolean>;
30
26
  type CreateComponentCallBack = ( attrs: Record<string,string> ) => ComponentContent;
31
27
 
@@ -95,8 +91,6 @@ export interface ComponentProps {
95
91
  cls?: string;
96
92
  /** Element ID. */
97
93
  id?: string;
98
- /** Reference to the component instance. */
99
- ref?: RefType<any>;
100
94
 
101
95
  // shortcuts
102
96
  /** Width (px or string like `"50%"` or `"3em"`). */
@@ -628,7 +622,7 @@ export class Component<P extends ComponentProps = ComponentProps, E extends Comp
628
622
  set( child );
629
623
  }
630
624
 
631
- d.insertBefore( d.firstChild, fragment );
625
+ d.insertBefore( fragment, d.firstChild );
632
626
  }
633
627
  }
634
628
 
@@ -259,8 +259,10 @@ export class CoreElement<E extends EventMap = EventMap> {
259
259
 
260
260
  cleanUp( ) {
261
261
  this.clearTimeouts( );
262
- this.#cleanup?.forEach( x => x() );
263
- this.#cleanup.length = 0;
262
+ if( this.#cleanup ) {
263
+ this.#cleanup.forEach( x => x() );
264
+ this.#cleanup.length = 0;
265
+ }
264
266
  }
265
267
 
266
268
  // :: EVENTS ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
@@ -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
@@ -21,6 +21,10 @@ declare global {
21
21
  interface IntrinsicElements {
22
22
  [ name: string ]: any;
23
23
  }
24
+
25
+ interface IntrinsicClassAttributes<T> {
26
+ ref?: [ owner: object, name: string ];
27
+ }
24
28
  }
25
29
  }
26
30