FIX: ensures minimum tags logic is correct and shared (#14723)

Also fixes a bug where select-kit was not updating noneItem in multi-selects.
This commit is contained in:
Joffrey JAFFEUX
2021-11-12 14:04:48 +01:00
committed by GitHub
parent 362c47ce6a
commit 904d509cce
12 changed files with 124 additions and 87 deletions

View File

@@ -20,6 +20,7 @@ export default ComboBoxComponent.extend({
permissionType: PermissionType.FULL,
excludeCategoryId: null,
scopedCategoryId: null,
prioritizedCategoryId: null,
},
modifyComponentForRow() {

View File

@@ -12,6 +12,7 @@ export default SingleSelectComponent.extend({
caretUpIcon: "caret-up",
caretDownIcon: "caret-down",
showCaret: false,
customStyle: null,
},
modifyComponentForRow() {

View File

@@ -23,10 +23,8 @@ export default MultiSelectComponent.extend(TagsMixin, {
termMatchesForbidden: false,
categoryId: null,
everyTag: false,
none: "tagging.choose_for_topic",
closeOnChange: false,
maximum: "maximumSelectedTags",
minimum: "minimumSelectedTags",
maximum: "maxTagsPerTopic",
autoInsertNoneItem: false,
},
@@ -38,31 +36,20 @@ export default MultiSelectComponent.extend(TagsMixin, {
return "tag-row";
},
allowAnyTag: or("allowCreate", "site.can_create_tag"),
maximumSelectedTags: computed(function () {
return parseInt(
this.options.limit ||
this.selectKit.options.maximum ||
this.maxTagsPerTopic,
10
);
}),
minimumSelectedTags: computed(function () {
if (
this.selectKit.options.minimum ||
this.selectKit.options.requiredTagGroups
) {
const minimum = parseInt(this.selectKit.options.minimum, 10);
if (minimum > 0) {
return this.defaultItem(
null,
I18n.t("select_kit.min_content_not_reached", { count: minimum })
);
}
modifyNoSelection() {
if (this.selectKit.options.minimum > 0) {
return this.defaultItem(
null,
I18n.t("tagging.choose_for_topic_required", {
count: this.selectKit.options.minimum,
})
);
} else {
return this.defaultItem(null, I18n.t("tagging.choose_for_topic"));
}
}),
},
allowAnyTag: or("allowCreate", "site.can_create_tag"),
caretIcon: computed("value.[]", "content.[]", function () {
const maximum = this.selectKit.options.maximum;

View File

@@ -124,35 +124,40 @@ export default SelectKitComponent.extend({
}
},
selectedContent: computed("value.[]", "content.[]", function () {
const value = makeArray(this.value).map((v) =>
this.selectKit.options.castInteger && this._isNumeric(v) ? Number(v) : v
);
selectedContent: computed(
"value.[]",
"content.[]",
"selectKit.noneItem",
function () {
const value = makeArray(this.value).map((v) =>
this.selectKit.options.castInteger && this._isNumeric(v) ? Number(v) : v
);
if (value.length) {
let content = [];
if (value.length) {
let content = [];
value.forEach((v) => {
if (this.selectKit.valueProperty) {
const c = makeArray(this.content).findBy(
this.selectKit.valueProperty,
v
);
if (c) {
content.push(c);
value.forEach((v) => {
if (this.selectKit.valueProperty) {
const c = makeArray(this.content).findBy(
this.selectKit.valueProperty,
v
);
if (c) {
content.push(c);
}
} else {
if (makeArray(this.content).includes(v)) {
content.push(v);
}
}
} else {
if (makeArray(this.content).includes(v)) {
content.push(v);
}
}
});
});
return this.selectKit.modifySelection(content);
return this.selectKit.modifySelection(content);
}
return this.selectKit.noneItem;
}
return null;
}),
),
_onKeydown(event) {
if (

View File

@@ -209,8 +209,12 @@ export default Component.extend(
didReceiveAttrs() {
this._super(...arguments);
const computedOptions = {};
Object.keys(this.selectKitOptions).forEach((key) => {
if (isPresent(this.options[key])) {
this.selectKit.options.set(key, this.options[key]);
return;
}
const value = this.selectKitOptions[key];
if (
@@ -219,9 +223,9 @@ export default Component.extend(
key === "componentForCollection"
) {
if (typeof value === "string") {
computedOptions[key] = () => value;
this.selectKit.options.set(key, () => value);
} else {
computedOptions[key] = bind(this, value);
this.selectKit.options.set(key, bind(this, value));
}
return;
@@ -234,15 +238,13 @@ export default Component.extend(
) {
const computedValue = get(this, value);
if (typeof computedValue !== "function") {
computedOptions[key] = get(this, value);
this.selectKit.options.set(key, computedValue);
return;
}
}
computedOptions[key] = value;
this.selectKit.options.set(key, value);
});
this.selectKit.options.setProperties(
Object.assign(computedOptions, this.options || {})
);
this.selectKit.setProperties({
hasSelection: !isEmpty(this.value),
@@ -264,6 +266,7 @@ export default Component.extend(
},
selectKitOptions: {
allowAny: false,
showFullTitle: true,
none: null,
translatedNone: null,
@@ -277,7 +280,6 @@ export default Component.extend(
maximum: null,
maximumLabel: null,
minimum: null,
minimumLabel: null,
autoInsertNoneItem: true,
closeOnChange: true,
limitMatches: null,