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 +1 -1
- package/src/core/component.ts +1 -7
- package/src/core/core_element.ts +4 -2
- package/src/core/core_react.ts +33 -13
- package/src/x4tsx.d.ts +4 -0
package/package.json
CHANGED
package/src/core/component.ts
CHANGED
|
@@ -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
|
|
625
|
+
d.insertBefore( fragment, d.firstChild );
|
|
632
626
|
}
|
|
633
627
|
}
|
|
634
628
|
|
package/src/core/core_element.ts
CHANGED
|
@@ -259,8 +259,10 @@ export class CoreElement<E extends EventMap = EventMap> {
|
|
|
259
259
|
|
|
260
260
|
cleanUp( ) {
|
|
261
261
|
this.clearTimeouts( );
|
|
262
|
-
this.#cleanup
|
|
263
|
-
|
|
262
|
+
if( this.#cleanup ) {
|
|
263
|
+
this.#cleanup.forEach( x => x() );
|
|
264
|
+
this.#cleanup.length = 0;
|
|
265
|
+
}
|
|
264
266
|
}
|
|
265
267
|
|
|
266
268
|
// :: EVENTS ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
|
package/src/core/core_react.ts
CHANGED
|
@@ -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"], ...
|
|
50
|
-
|
|
51
|
-
|
|
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
|
|
56
|
-
if( isString(tag) ) {
|
|
60
|
+
// --- simple html element ------------------------
|
|
61
|
+
if( isString( tag ) ) {
|
|
57
62
|
el = new Component( { tag } );
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
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
|
-
|
|
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
|
-
|
|
74
|
-
|
|
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;
|