Compare commits

..

2 Commits

Author SHA1 Message Date
KernelDeimos 199e2dfa59 Add ForEach to demo
3 years ago
KernelDeimos 071a32a8b4 Add ForEach construct
3 years ago

@ -1,9 +1,10 @@
// const justact = require('justact'); // const justact = require('justact');
import * as justact from 'justact'; import * as justact from 'justact';
import { ForEach } from 'justact';
class InputMultiplex extends justact.Component { class InputMultiplex extends justact.Component {
render () { render () {
console.log('InputMultiplex', this);
return this.swap ? ( return this.swap ? (
<input value={this.valueB$} type="text" /> <input value={this.valueB$} type="text" />
) : ( ) : (
@ -18,6 +19,9 @@ class App extends justact.Component {
<div> <div>
<input type="checkbox" value={this.swap$} /> <input type="checkbox" value={this.swap$} />
<InputMultiplex swap={this.swap$}></InputMultiplex> <InputMultiplex swap={this.swap$}></InputMultiplex>
<ForEach array={[1,2,3]} render={item => (
<input value={item} disabled />
)} />
</div> </div>
) )
} }

@ -134,7 +134,6 @@ export abstract class Component {
const newElements = this.createElements(); const newElements = this.createElements();
referenceElement.replaceWith(...newElements); referenceElement.replaceWith(...newElements);
// Required because it renders itself in this case
this.notifyMounted({ this.notifyMounted({
parent: this.parent_, parent: this.parent_,
}) })

@ -0,0 +1,51 @@
import { Renderable } from '../component/componentdefs.js';
import { Component } from '../component/Component.js';
import { ArrayVAO } from '../vao/ArrayVAO.js';
import { ScalarVAO } from '../vao/ScalarVAO.js';
import { TAttributes } from '../html/htmldefs.js';
// === TODO ===
// [A] Naive implementation - should be optimzied
export type TForEachRender = (item: any) => Renderable | Renderable[]
export class ForEach extends Component {
static properties = {
array: {
$: ArrayVAO
},
render: {
$: ScalarVAO
}
}
constructor ({ attributes, children }: {
attributes: TAttributes | undefined,
children: Renderable[] | undefined
}) {
super({ attributes, children });
const array$ = this.vaos_['array'] as ArrayVAO;
// TODO: [A]
((events) => {
for ( const event of events ) {
array$.sub(event, this.rerenderSelf.bind(this));
}
})(['change', 'item.insert', 'item.remove', 'item.replace']);
}
render () {
const array$ = this.vaos_['array'] as ArrayVAO;
const render$ = this.vaos_['render'];
const render = render$.get() as TForEachRender;
const outputRenderables = [];
for ( const item of array$.get() ) {
outputRenderables.push(...(result => Array.isArray(result)
? result : [result])(render(item)));
}
return outputRenderables;
}
}

@ -5,6 +5,7 @@ import { Component as ExportableComponent } from './component/Component.js';
import { ScalarVAO as ExportableScalarVAO } from './vao/ScalarVAO.js'; import { ScalarVAO as ExportableScalarVAO } from './vao/ScalarVAO.js';
import { ArrayVAO as ExportableArrayVAO } from './vao/ArrayVAO.js'; import { ArrayVAO as ExportableArrayVAO } from './vao/ArrayVAO.js';
import { AbstractVAO as ExportableAbstractVAO } from './vao/VAO.js'; import { AbstractVAO as ExportableAbstractVAO } from './vao/VAO.js';
import { ForEach as ForEachExportable } from './construct/ForEach.js';
export function createElement( export function createElement(
tag: string | RenderableConstructor, tag: string | RenderableConstructor,
@ -34,3 +35,4 @@ export const Component = ExportableComponent;
export const AbstractVAO = ExportableAbstractVAO; export const AbstractVAO = ExportableAbstractVAO;
export const ScalarVAO = ExportableScalarVAO; export const ScalarVAO = ExportableScalarVAO;
export const ArrayVAO = ExportableArrayVAO; export const ArrayVAO = ExportableArrayVAO;
export const ForEach = ForEachExportable;

@ -24,6 +24,7 @@ export class ArrayVAO extends AbstractVAO {
constructor (initialValue: any[]) { constructor (initialValue: any[]) {
super(); super();
if ( initialValue == null ) initialValue = [];
if ( ! Array.isArray(initialValue) ) { if ( ! Array.isArray(initialValue) ) {
throw new Error('ArrayVAO expects an array'); throw new Error('ArrayVAO expects an array');
} }

Loading…
Cancel
Save