Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #15974 from emberjs/template-only-component
Implement template-only components RFC
- Loading branch information
Showing
13 changed files
with
295 additions
and
29 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
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
44 changes: 44 additions & 0 deletions
44
packages/ember-glimmer/lib/component-managers/template-only.ts
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,44 @@ | ||
import { VersionedPathReference } from '@glimmer/reference'; | ||
import { CompiledDynamicProgram, ComponentDefinition, NULL_REFERENCE } from '@glimmer/runtime'; | ||
import { Opaque } from '@glimmer/util'; | ||
import Environment from '../environment'; | ||
import { OwnedTemplate, WrappedTemplateFactory } from '../template'; | ||
import AbstractManager from './abstract'; | ||
|
||
class TemplateOnlyComponentLayoutCompiler { | ||
static id = 'template-only'; | ||
|
||
constructor(public template: WrappedTemplateFactory) { | ||
} | ||
|
||
compile(builder: any) { | ||
// TODO: use fromLayout | ||
builder.wrapLayout(this.template); | ||
} | ||
} | ||
|
||
export default class TemplateOnlyComponentManager extends AbstractManager<null> { | ||
create(): null { | ||
return null; | ||
} | ||
|
||
layoutFor({ template }: TemplateOnlyComponentDefinition, _: null, env: Environment): CompiledDynamicProgram { | ||
return env.getCompiledBlock(TemplateOnlyComponentLayoutCompiler, template); | ||
} | ||
|
||
getSelf(): VersionedPathReference<Opaque> { | ||
return NULL_REFERENCE; | ||
} | ||
|
||
getDestructor() { | ||
return null; | ||
} | ||
} | ||
|
||
const MANAGER = new TemplateOnlyComponentManager(); | ||
|
||
export class TemplateOnlyComponentDefinition extends ComponentDefinition<null> { | ||
constructor(name: string, public template: OwnedTemplate) { | ||
super(name, MANAGER, null); | ||
} | ||
} |
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
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
196 changes: 196 additions & 0 deletions
196
packages/ember-glimmer/tests/integration/components/template-only-components-test.js
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,196 @@ | ||
import { moduleFor, RenderingTest } from '../../utils/test-case'; | ||
import { classes } from '../../utils/test-helpers'; | ||
import { EMBER_GLIMMER_TEMPLATE_ONLY_COMPONENTS } from 'ember/features'; | ||
|
||
class TemplateOnlyComponentsTest extends RenderingTest { | ||
registerComponent(name, template) { | ||
super.registerComponent(name, { template, ComponentClass: null }); | ||
} | ||
} | ||
|
||
if (EMBER_GLIMMER_TEMPLATE_ONLY_COMPONENTS) { | ||
moduleFor('Components test: template-only components (glimmer components)', class extends TemplateOnlyComponentsTest { | ||
['@test it can render a template-only component']() { | ||
this.registerComponent('foo-bar', 'hello'); | ||
|
||
this.render('{{foo-bar}}'); | ||
|
||
this.assertInnerHTML('hello'); | ||
|
||
this.assertStableRerender(); | ||
} | ||
|
||
['@feature(ember-glimmer-named-arguments) it can render named arguments']() { | ||
this.registerComponent('foo-bar', '|{{@foo}}|{{@bar}}|'); | ||
|
||
this.render('{{foo-bar foo=foo bar=bar}}', { | ||
foo: 'foo', bar: 'bar' | ||
}); | ||
|
||
this.assertInnerHTML('|foo|bar|'); | ||
|
||
this.assertStableRerender(); | ||
|
||
this.runTask(() => this.context.set('foo', 'FOO')); | ||
|
||
this.assertInnerHTML('|FOO|bar|'); | ||
|
||
this.runTask(() => this.context.set('bar', 'BAR')); | ||
|
||
this.assertInnerHTML('|FOO|BAR|'); | ||
|
||
this.runTask(() => this.context.setProperties({ foo: 'foo', bar: 'bar' })); | ||
|
||
this.assertInnerHTML('|foo|bar|'); | ||
} | ||
|
||
['@test it does not reflected arguments as properties']() { | ||
this.registerComponent('foo-bar', '|{{foo}}|{{this.bar}}|'); | ||
|
||
this.render('{{foo-bar foo=foo bar=bar}}', { | ||
foo: 'foo', bar: 'bar' | ||
}); | ||
|
||
this.assertInnerHTML('|||'); | ||
|
||
this.assertStableRerender(); | ||
|
||
this.runTask(() => this.context.set('foo', 'FOO')); | ||
|
||
this.assertInnerHTML('|||'); | ||
|
||
this.runTask(() => this.context.set('bar', null)); | ||
|
||
this.assertInnerHTML('|||'); | ||
|
||
this.runTask(() => this.context.setProperties({ foo: 'foo', bar: 'bar' })); | ||
|
||
this.assertInnerHTML('|||'); | ||
} | ||
|
||
['@test it does not have curly component features']() { | ||
this.registerComponent('foo-bar', 'hello'); | ||
|
||
this.render('{{foo-bar tagName="p" class=class}}', { | ||
class: 'foo bar' | ||
}); | ||
|
||
this.assertInnerHTML('hello'); | ||
|
||
|
||
this.assertStableRerender(); | ||
|
||
this.runTask(() => this.context.set('class', 'foo')); | ||
|
||
this.assertInnerHTML('hello'); | ||
|
||
this.runTask(() => this.context.set('class', null)); | ||
|
||
this.assertInnerHTML('hello'); | ||
|
||
this.runTask(() => this.context.set('class', 'foo bar')); | ||
|
||
this.assertInnerHTML('hello'); | ||
} | ||
}); | ||
} else { | ||
moduleFor('Components test: template-only components (curly components)', class extends TemplateOnlyComponentsTest { | ||
['@test it can render a template-only component']() { | ||
this.registerComponent('foo-bar', 'hello'); | ||
|
||
this.render('{{foo-bar}}'); | ||
|
||
this.assertComponentElement(this.firstChild, { content: 'hello' }); | ||
|
||
this.assertStableRerender(); | ||
} | ||
|
||
['@feature(ember-glimmer-named-arguments) it can render named arguments']() { | ||
this.registerComponent('foo-bar', '|{{@foo}}|{{@bar}}|'); | ||
|
||
this.render('{{foo-bar foo=foo bar=bar}}', { | ||
foo: 'foo', bar: 'bar' | ||
}); | ||
|
||
this.assertComponentElement(this.firstChild, { content: '|foo|bar|' }); | ||
|
||
this.assertStableRerender(); | ||
|
||
this.runTask(() => this.context.set('foo', 'FOO')); | ||
|
||
this.assertComponentElement(this.firstChild, { content: '|FOO|bar|' }); | ||
|
||
this.runTask(() => this.context.set('bar', 'BAR')); | ||
|
||
this.assertComponentElement(this.firstChild, { content: '|FOO|BAR|' }); | ||
|
||
this.runTask(() => this.context.setProperties({ foo: 'foo', bar: 'bar' })); | ||
|
||
this.assertComponentElement(this.firstChild, { content: '|foo|bar|' }); | ||
} | ||
|
||
['@test it renders named arguments as reflected properties']() { | ||
this.registerComponent('foo-bar', '|{{foo}}|{{this.bar}}|'); | ||
|
||
this.render('{{foo-bar foo=foo bar=bar}}', { | ||
foo: 'foo', bar: 'bar' | ||
}); | ||
|
||
this.assertComponentElement(this.firstChild, { content: '|foo|bar|' }); | ||
|
||
this.assertStableRerender(); | ||
|
||
this.runTask(() => this.context.set('foo', 'FOO')); | ||
|
||
this.assertComponentElement(this.firstChild, { content: '|FOO|bar|' }); | ||
|
||
this.runTask(() => this.context.set('bar', null)); | ||
|
||
this.assertComponentElement(this.firstChild, { content: '|FOO||' }); | ||
|
||
this.runTask(() => this.context.setProperties({ foo: 'foo', bar: 'bar' })); | ||
|
||
this.assertComponentElement(this.firstChild, { content: '|foo|bar|' }); | ||
} | ||
|
||
['@test it has curly component features']() { | ||
this.registerComponent('foo-bar', 'hello'); | ||
|
||
this.render('{{foo-bar tagName="p" class=class}}', { | ||
class: 'foo bar' | ||
}); | ||
|
||
this.assertComponentElement(this.firstChild, { | ||
tagName: 'p', | ||
attrs: { class: classes('foo bar ember-view') }, | ||
content: 'hello' | ||
}); | ||
|
||
this.assertStableRerender(); | ||
|
||
this.runTask(() => this.context.set('class', 'foo')); | ||
|
||
this.assertComponentElement(this.firstChild, { | ||
tagName: 'p', | ||
attrs: { class: classes('foo ember-view') }, | ||
content: 'hello' | ||
}); | ||
|
||
this.runTask(() => this.context.set('class', null)); | ||
|
||
this.assertComponentElement(this.firstChild, { | ||
tagName: 'p', | ||
attrs: { class: classes('ember-view') }, | ||
content: 'hello' | ||
}); | ||
|
||
this.runTask(() => this.context.set('class', 'foo bar')); | ||
|
||
this.assertComponentElement(this.firstChild, { | ||
tagName: 'p', | ||
attrs: { class: classes('foo bar ember-view') }, | ||
content: 'hello' | ||
}); | ||
} | ||
}); | ||
} |
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
Oops, something went wrong.