FIX: simplify maximum/minimum logic in sk2 to avoid chicken/egg (#8868)

This commit is contained in:
Joffrey JAFFEUX 2020-02-05 16:01:58 +01:00 committed by GitHub
parent be42b87338
commit 98303ee645
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 10 additions and 49 deletions

View File

@ -80,14 +80,16 @@ export default ComboBox.extend(TagsMixin, {
this.insertAfterCollection(ERRORS_COLLECTION, SELECTED_TAGS_COLLECTION); this.insertAfterCollection(ERRORS_COLLECTION, SELECTED_TAGS_COLLECTION);
}, },
caretIcon: computed("selectKit.hasReachedMaximum", function() { caretIcon: computed("value.[]", function() {
return this.selectKit.hasReachedMaximum ? null : "plus"; return this.selectKit.options.maximum >= makeArray(this.value).length
? null
: "plus";
}), }),
modifySelection(content) { modifySelection(content) {
let joinedTags = this.value.join(", "); let joinedTags = makeArray(this.value).join(", ");
if (!this.selectKit.hasReachedMinimum) { if (!this.selectKit.options.maximum >= makeArray(this.value).length) {
const key = const key =
this.selectKit.options.minimumLabel || this.selectKit.options.minimumLabel ||
"select_kit.min_content_not_reached"; "select_kit.min_content_not_reached";

View File

@ -44,9 +44,7 @@ export default Component.extend(
"selectKit.isExpanded:is-expanded", "selectKit.isExpanded:is-expanded",
"selectKit.isDisabled:is-disabled", "selectKit.isDisabled:is-disabled",
"selectKit.isHidden:is-hidden", "selectKit.isHidden:is-hidden",
"selectKit.hasSelection:has-selection", "selectKit.hasSelection:has-selection"
"selectKit.hasReachedMaximum:has-reached-maximum",
"selectKit.hasReachedMinimum:has-reached-minimum"
], ],
tabindex: 0, tabindex: 0,
content: null, content: null,
@ -263,9 +261,7 @@ export default Component.extend(
limitMatches: null, limitMatches: null,
placement: "bottom-start", placement: "bottom-start",
filterComponent: "select-kit/select-kit-filter", filterComponent: "select-kit/select-kit-filter",
selectedNameComponent: "selected-name", selectedNameComponent: "selected-name"
hasReachedMaximum: "hasReachedMaximum",
hasReachedMinimum: "hasReachedMinimum"
}, },
autoFilterable: computed("content.[]", "selectKit.filter", function() { autoFilterable: computed("content.[]", "selectKit.filter", function() {
@ -296,34 +292,6 @@ export default Component.extend(
} }
), ),
hasReachedMaximum: computed(
"selectKit.options.maximum",
"value",
function() {
const maximum = parseInt(this.selectKit.options.maximum, 10);
if (maximum && makeArray(this.value).length >= maximum) {
return true;
}
return false;
}
),
hasReachedMinimum: computed(
"selectKit.options.minimum",
"value",
function() {
const minimum = parseInt(this.selectKit.options.minimum, 10);
if (!minimum || makeArray(this.value).length >= minimum) {
return true;
}
return false;
}
),
createContentFromInput(input) { createContentFromInput(input) {
return input; return input;
}, },
@ -345,7 +313,6 @@ export default Component.extend(
const selection = Ember.makeArray(this.value); const selection = Ember.makeArray(this.value);
const maximum = this.selectKit.options.maximum; const maximum = this.selectKit.options.maximum;
if (maximum && selection.length >= maximum) { if (maximum && selection.length >= maximum) {
const key = const key =
this.selectKit.options.maximumLabel || this.selectKit.options.maximumLabel ||
@ -354,15 +321,6 @@ export default Component.extend(
return false; return false;
} }
const minimum = this.selectKit.options.minimum;
if (minimum && selection.length <= minimum) {
const key =
this.selectKit.options.minimumLabel ||
"select_kit.min_content_not_reached";
this.addError(I18n.t(key, { count: minimum }));
return false;
}
return true; return true;
}, },

View File

@ -2,6 +2,7 @@ import { reads } from "@ember/object/computed";
import { ajax } from "discourse/lib/ajax"; import { ajax } from "discourse/lib/ajax";
import { popupAjaxError } from "discourse/lib/ajax-error"; import { popupAjaxError } from "discourse/lib/ajax-error";
import Mixin from "@ember/object/mixin"; import Mixin from "@ember/object/mixin";
import { makeArray } from "discourse-common/lib/helpers";
export default Mixin.create({ export default Mixin.create({
searchTags(url, data, callback) { searchTags(url, data, callback) {
@ -22,7 +23,7 @@ export default Mixin.create({
allowAnyTag: reads("site.can_create_tag"), allowAnyTag: reads("site.can_create_tag"),
validateCreate(filter, content) { validateCreate(filter, content) {
if (this.selectKit.hasReachedMaximum) { if (this.selectKit.options.maximum >= makeArray(this.value).length) {
this.addError( this.addError(
I18n.t("select_kit.max_content_reached", { I18n.t("select_kit.max_content_reached", {
count: this.selectKit.limit count: this.selectKit.limit