ui: Adds a `default` view helper for providing a default value (#4650)

If the first value passed to the helper is an empty string or undefined
then return the second value
This commit is contained in:
John Cowen 2018-09-12 20:38:57 +01:00 committed by GitHub
parent b279f23372
commit ab568f6b94
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 44 additions and 2 deletions

View File

@ -0,0 +1,10 @@
import { helper } from '@ember/component/helper';
export function _default(params, hash) {
if (params[0] === '' || typeof params[0] === 'undefined') {
return params[1];
}
return params[0];
}
export default helper(_default);

View File

@ -38,7 +38,7 @@
href=(href-to 'dc.nodes.show' item.Node.Node)
name=item.Node.Node
service=item.Service.ID
address=(concat item.Service.Address ':' item.Service.Port)
address=(concat (default item.Service.Address item.Node.Address) ':' item.Service.Port)
checks=item.Checks
}}
{{/each}}
@ -58,7 +58,7 @@
data-test-node=item.Node.Node
name=item.Node.Node
service=item.Service.ID
address=(concat item.Service.Address ':' item.Service.Port)
address=(concat (default item.Service.Address item.Node.Address) ':' item.Service.Port)
checks=item.Checks
status=item.Checks.[0].Status
}}

View File

@ -0,0 +1,32 @@
import { moduleForComponent, test } from 'ember-qunit';
import hbs from 'htmlbars-inline-precompile';
moduleForComponent('default', 'helper:default', {
integration: true,
});
// Replace this with your real tests.
test('it renders', function(assert) {
this.set('inputValue', '1234');
this.render(hbs`{{default inputValue}}`);
assert.equal(
this.$()
.text()
.trim(),
'1234'
);
});
test('it renders the default value', function(assert) {
this.set('inputValue', '');
this.render(hbs`{{default inputValue '1234'}}`);
assert.equal(
this.$()
.text()
.trim(),
'1234'
);
});