[FIX] better control on value mutation

This commit is contained in:
Joffrey JAFFEUX 2017-10-20 13:40:56 -07:00 committed by GitHub
parent 383191cafc
commit ae1e4de286
4 changed files with 48 additions and 12 deletions

View File

@ -48,6 +48,8 @@ export default Ember.Component.extend(UtilsMixin, DomHelpersMixin, KeyboardMixin
fullWidthOnMobile: false,
castInteger: false,
allowAny: false,
allowValueMutation: true,
autoSelectFirst: true,
init() {
this._super();
@ -56,13 +58,6 @@ export default Ember.Component.extend(UtilsMixin, DomHelpersMixin, KeyboardMixin
this.setProperties({ filterable: false, autoFilterable: false });
}
if (isNone(this.get("none")) && isEmpty(this.get("value")) && !isEmpty(this.get("content"))) {
Ember.run.scheduleOnce("sync", () => {
const firstValue = this.get(`content.0.${this.get("valueAttribute")}`);
this.set("value", firstValue);
});
}
this._previousScrollParentOverflow = "auto";
this._previousCSSContext = {};
},
@ -171,12 +166,13 @@ export default Ember.Component.extend(UtilsMixin, DomHelpersMixin, KeyboardMixin
@computed("content.[]")
computedContent(content) {
this._mutateValue();
return this.formatContents(content || []);
},
@computed("value", "none", "computedContent.firstObject.value")
computedValue(value, none, firstContentValue) {
if (isNone(value) && isNone(none)) {
if (isNone(value) && isNone(none) && this.get("autoSelectFirst") === true) {
return this._castInteger(firstContentValue);
}
@ -276,10 +272,7 @@ export default Ember.Component.extend(UtilsMixin, DomHelpersMixin, KeyboardMixin
@computed("filter", "computedFilterable", "computedContent.[]", "computedValue.[]")
filteredContent(filter, computedFilterable, computedContent, computedValue) {
if (computedFilterable === false) {
return computedContent;
}
if (computedFilterable === false) { return computedContent; }
return this.filterFunction(computedContent)(this, computedValue);
},
@ -470,5 +463,23 @@ export default Ember.Component.extend(UtilsMixin, DomHelpersMixin, KeyboardMixin
width: this.$().width(),
height: headerHeight + this.$body().outerHeight(false)
});
},
@on("didReceiveAttrs")
_mutateValue() {
if (this.get("allowValueMutation") !== true) {
return;
}
const none = isNone(this.get("none"));
const emptyValue = isEmpty(this.get("value"));
const notEmptyContent = !isEmpty(this.get("content"));
if (none && emptyValue && notEmptyContent) {
Ember.run.scheduleOnce("sync", () => {
const firstValue = this.get(`content.0.${this.get("valueAttribute")}`);
this.set("value", firstValue);
});
}
}
});

View File

@ -7,6 +7,8 @@ export default ComboBoxComponent.extend({
classNames: "topic-footer-mobile-dropdown",
filterable: false,
autoFilterable: false,
allowValueMutation: false,
autoSelectFirst: false,
@on("didReceiveAttrs")
_setTopicFooterMobileDropdownOptions() {

View File

@ -275,3 +275,19 @@ componentTest('supports keyboard events', {
});
}
});
componentTest('supports mutating value when no value given', {
template: '{{select-box-kit value=value content=content}}',
beforeEach() {
this.set("value", "");
this.set("content", [{ id: "1", name: "robin"}, {id: "2", name: "régis" }]);
},
test(assert) {
andThen(() => {
assert.equal(this.get("value"), "1");
});
}
});

View File

@ -23,6 +23,13 @@ componentTest('default', {
assert.equal(selectBox().header.name(), "Topic Controls");
assert.equal(selectBox().rowByIndex(0).name(), "Bookmark");
assert.equal(selectBox().rowByIndex(1).name(), "Share");
assert.equal(selectBox().selectedRow.el.length, 0, "it doesnt preselect first row");
});
selectBoxSelectRow("share");
andThen(() => {
assert.equal(this.get("value"), null, "it resets the value");
});
}
});