This repository has been archived by the owner on Jul 30, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* add initial tsx support * tweak tsx for consumer usage * add test for tsx * add unit tests * readme for tsx * review feedback
- Loading branch information
Showing
6 changed files
with
222 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import { v, w } from './d'; | ||
import { Constructor, DNode } from './interfaces'; | ||
import { WNode, VirtualDomProperties } from './interfaces'; | ||
|
||
declare global { | ||
namespace JSX { | ||
type Element = WNode; | ||
interface ElementAttributesProperty { | ||
properties: {}; | ||
} | ||
interface IntrinsicElements { | ||
[key: string]: VirtualDomProperties; | ||
} | ||
} | ||
} | ||
|
||
export const REGISTRY_ITEM = Symbol('Identifier for an item from the Widget Registry.'); | ||
|
||
export class FromRegistry<P> { | ||
static type = REGISTRY_ITEM; | ||
properties: P; | ||
name: string; | ||
} | ||
|
||
export function fromRegistry<P>(tag: string): Constructor<FromRegistry<P>> { | ||
return class extends FromRegistry<P> { | ||
properties: P; | ||
static type = REGISTRY_ITEM; | ||
name = tag; | ||
}; | ||
} | ||
|
||
function spreadChildren(children: any[], child: any): any[] { | ||
if (Array.isArray(child)) { | ||
return child.reduce(spreadChildren, children); | ||
} | ||
else { | ||
return [ ...children, child ]; | ||
} | ||
} | ||
|
||
export function tsx(tag: any, properties = {}, ...children: any[]): DNode { | ||
children = children.reduce(spreadChildren, []); | ||
properties = properties === null ? {} : properties; | ||
if (typeof tag === 'string') { | ||
return v(tag, properties, children); | ||
} | ||
else if (tag.type === REGISTRY_ITEM) { | ||
const registryItem = new tag(); | ||
return w(registryItem.name, properties, children); | ||
} | ||
else { | ||
return w(tag, properties, children); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,3 +10,4 @@ import './main'; | |
import './diff'; | ||
import './RegistryHandler'; | ||
import './Injector'; | ||
import './tsx'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import * as registerSuite from 'intern!object'; | ||
import * as assert from 'intern/chai!assert'; | ||
import { WidgetBase } from '../../src/WidgetBase'; | ||
import { fromRegistry, registry } from '../../src/d'; | ||
import { WidgetProperties } from '../../src/interfaces'; | ||
import { VNode } from '@dojo/interfaces/vdom'; | ||
import * as tsx from './../../src/tsx'; | ||
|
||
registerSuite({ | ||
name: 'tsx', | ||
'can use tsx'() { | ||
interface FooProperties extends WidgetProperties { | ||
hello: string; | ||
} | ||
class Foo extends WidgetBase<FooProperties> { | ||
render() { | ||
const { hello } = this.properties; | ||
return ( | ||
<header classes={{ background: true }} > | ||
<div>{ hello }</div> | ||
</header> | ||
); | ||
} | ||
} | ||
class Bar extends WidgetBase<any> { | ||
render() { | ||
return <Foo hello='world' />; | ||
} | ||
} | ||
|
||
class Qux extends WidgetBase<any> { | ||
render() { | ||
const LazyFoo = fromRegistry<FooProperties>('LazyFoo'); | ||
return <LazyFoo hello='cool' />; | ||
} | ||
} | ||
|
||
const bar = new Bar(); | ||
const barRender = bar.__render__() as VNode; | ||
const barChild = barRender.children![0]; | ||
assert.equal(barRender.vnodeSelector, 'header'); | ||
assert.equal(barChild.text, 'world'); | ||
|
||
const qux = new Qux(); | ||
const firstQuxRender = qux.__render__(); | ||
assert.equal(firstQuxRender, null); | ||
|
||
registry.define('LazyFoo', Foo); | ||
const secondQuxRender = qux.__render__() as VNode; | ||
const secondQuxChild = secondQuxRender.children![0]; | ||
assert.equal(secondQuxRender.vnodeSelector, 'header'); | ||
assert.equal(secondQuxChild.text, 'cool'); | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import * as registerSuite from 'intern!object'; | ||
import * as assert from 'intern/chai!assert'; | ||
import { WidgetBase } from './../../src/WidgetBase'; | ||
import { HNode, WidgetProperties, WNode } from '../../src/interfaces'; | ||
import { tsx, fromRegistry, REGISTRY_ITEM } from '../../src/tsx'; | ||
import { HNODE } from './../../src/d'; | ||
|
||
registerSuite({ | ||
name: 'tsx', | ||
'create a registry wrapper'() { | ||
const RegistryWrapper = fromRegistry<WidgetProperties>('tag'); | ||
assert.strictEqual((<any> RegistryWrapper).type, REGISTRY_ITEM); | ||
const registryWrapper = new RegistryWrapper(); | ||
assert.strictEqual(registryWrapper.name, 'tag'); | ||
// These will always be undefined but show the type inference of properties. | ||
registryWrapper.properties = {}; | ||
assert.isUndefined(registryWrapper.properties.key); | ||
assert.isUndefined(registryWrapper.properties.bind); | ||
}, | ||
tsx: { | ||
'tsx generate a HNode'() { | ||
const node: HNode = <HNode> tsx('div', { hello: 'world' }, [ 'child' ]); | ||
assert.deepEqual(node.tag, 'div'); | ||
assert.deepEqual(node.properties, { hello: 'world' }); | ||
assert.deepEqual(node.children, [ 'child' ]); | ||
assert.strictEqual(node.type, HNODE); | ||
}, | ||
'tsx generate a WNode'() { | ||
const node: WNode = <WNode> tsx(WidgetBase, { hello: 'world' }, [ 'child' ]); | ||
assert.deepEqual(node.widgetConstructor, WidgetBase); | ||
assert.deepEqual(node.properties, { hello: 'world' }); | ||
assert.deepEqual(node.children, [ 'child' ]); | ||
}, | ||
'tsx generate a WNode from a RegistryWrapper'() { | ||
const RegistryWrapper = fromRegistry<WidgetProperties>('tag'); | ||
const node: WNode = <WNode> tsx(RegistryWrapper, { hello: 'world' }, [ 'child' ]); | ||
assert.deepEqual(node.widgetConstructor, 'tag'); | ||
assert.deepEqual(node.properties, { hello: 'world' }); | ||
assert.deepEqual(node.children, [ 'child' ]); | ||
}, | ||
'children arrays are spread correctly'() { | ||
const node: HNode = <HNode> tsx('div', { hello: 'world' }, [ 'child', [ 'child-2', [ 'child-3' ] ] ]); | ||
assert.deepEqual(node.tag, 'div'); | ||
assert.deepEqual(node.properties, { hello: 'world' }); | ||
assert.deepEqual(node.children, [ 'child', 'child-2', 'child-3' ]); | ||
assert.strictEqual(node.type, HNODE); | ||
}, | ||
'defaults properties to empty object'() { | ||
const node: HNode = <HNode> tsx('div'); | ||
assert.deepEqual(node.tag, 'div'); | ||
assert.deepEqual(node.properties, {}); | ||
assert.deepEqual(node.children, []); | ||
assert.strictEqual(node.type, HNODE); | ||
}, | ||
'defaults `null` properties to empty object'() { | ||
const node: HNode = <HNode> tsx('div', <any> null); | ||
assert.deepEqual(node.tag, 'div'); | ||
assert.deepEqual(node.properties, {}); | ||
assert.deepEqual(node.children, []); | ||
assert.strictEqual(node.type, HNODE); | ||
} | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters