FIX: forces boolean when content is only "true" && "false"

This commit is contained in:
Joffrey JAFFEUX 2018-05-24 23:41:39 +02:00 committed by GitHub
parent 30fbf6fe81
commit 1be76d066c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 45 additions and 4 deletions

View File

@ -69,7 +69,7 @@
<div class="controls">
{{combo-box valueAttribute="value" content=availableSorts value=category.sort_order none="category.sort_options.default"}}
{{#unless isDefaultSortOrder}}
{{combo-box valueAttribute="value" content=sortAscendingOptions value=category.sort_ascending none="category.sort_options.default"}}
{{combo-box castBoolean=true valueAttribute="value" content=sortAscendingOptions value=category.sort_ascending none="category.sort_options.default"}}
{{/unless}}
</div>
</section>

View File

@ -79,7 +79,7 @@ export default SelectKitComponent.extend({
shouldDisplayFilter() { return true; },
_beforeWillComputeValues(values) {
return values.map(v => this._castInteger(v === "" ? null : v));
return values.map(v => this._cast(v === "" ? null : v));
},
willComputeValues(values) { return values; },
computeValues(values) { return values; },

View File

@ -62,6 +62,7 @@ export default Ember.Component.extend(UtilsMixin, PluginApiMixin, DomHelpersMixi
horizontalOffset: 0,
fullWidthOnMobile: false,
castInteger: false,
castBoolean: false,
allowAny: false,
allowInitialValueMutation: false,
content: null,
@ -169,7 +170,7 @@ export default Ember.Component.extend(UtilsMixin, PluginApiMixin, DomHelpersMixi
}
let computedContentItem = {
value: this._castInteger(this.valueForContentItem(contentItem)),
value: this._cast(this.valueForContentItem(contentItem)),
name: name || this._nameForContent(contentItem),
locked: false,
created: options.created || false,

View File

@ -63,7 +63,7 @@ export default SelectKitComponent.extend({
switch (typeof value) {
case "string":
case "number":
return this._castInteger(value === "" ? null : value);
return this._cast(value === "" ? null : value);
default:
return value;
}

View File

@ -27,6 +27,19 @@ export default Ember.Mixin.create({
return !isNaN(parseFloat(input)) && isFinite(input);
},
_cast(value) {
if (value === this.noneValue) return value;
return this._castInteger(this._castBoolean(value));
},
_castBoolean(value) {
if (this.get("castBoolean") && Ember.isPresent(value) && typeof(value) === "string") {
return value === "true";
}
return value;
},
_castInteger(value) {
if (this.get("castInteger") && Ember.isPresent(value) && this._isNumeric(value)) {
return parseInt(value, 10);

View File

@ -232,6 +232,33 @@ componentTest('supports converting select value to integer', {
}
});
componentTest('supports converting string as boolean to boolean', {
template: '{{single-select value=value content=content castBoolean=true}}',
beforeEach() {
this.set('value', true);
this.set('content', [{ id: 'true', name: 'ASC'}, {id: 'false', name: 'DESC' }]);
},
test(assert) {
this.get('subject').expand();
andThen(() => assert.equal(this.get('subject').selectedRow().name(), 'ASC') );
andThen(() => {
this.set('value', false);
});
andThen(() => {
assert.equal(
this.get('subject').selectedRow().name(),
'DESC',
'it works with dynamic content'
);
});
}
});
componentTest('supports keyboard events', {
template: '{{single-select content=content filterable=true}}',