Skip to content

Commit

Permalink
allowAdd and allowRemove settings for element selects
Browse files Browse the repository at this point in the history
Resolves #15639
  • Loading branch information
brandonkelly committed Sep 2, 2024
1 parent bf9c66a commit 0bf41d9
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 35 deletions.
1 change: 1 addition & 0 deletions CHANGELOG-WIP.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
- Deprecated `craft\helpers\ElementHelper::rootElement()`. `craft\base\ElementInterface::getRootOwner()` should be used instead.
- Added `Craft.cp.announce()`, simplifying live region announcements for screen readers. ([#15569](https://github.com/craftcms/cms/pull/15569))
- Element action menu items returned by `craft\base\Element::safeActionMenuItems()` and `destructiveActionMenuItems()` can now include a `showInChips` key to explicitly opt into/out of being shown within element chips and cards.
- Element select inputs now support `allowAdd` and `allowRemove` settings. ([#15639](https://github.com/craftcms/cms/discussions/15639))
- Control panel CSS selectors that take orientation into account now use logical properties. ([#15522](https://github.com/craftcms/cms/pull/15522))

### System
Expand Down
41 changes: 24 additions & 17 deletions src/templates/_includes/forms/elementSelect.twig
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@
{% set maintainHierarchy = maintainHierarchy ?? false %}
{% set registerJs = registerJs ?? true %}

{% set allowAdd = allowAdd ?? true %}
{% set allowRemove = allowRemove ?? true %}

{% set containerAttributes = {
id: id,
class: ['elementselect']|merge((class ?? [])|explodeClass),
Expand Down Expand Up @@ -63,23 +66,25 @@
{% endif %}

<div class="flex">
{{ tag('button', {
type: 'button',
class: [
'btn',
'add',
'icon',
'dashed',
disabled ? 'disabled',
limit and elements|length >= limit ? 'hidden',
]|filter,
text: selectionLabel ?? 'Choose'|t('app'),
disabled: disabled,
aria: {
label: selectionLabel ?? 'Choose'|t('app'),
describedby: describedBy ?? false,
}
}) }}
{% if allowAdd %}
{{ tag('button', {
type: 'button',
class: [
'btn',
'add',
'icon',
'dashed',
disabled ? 'disabled',
limit and elements|length >= limit ? 'hidden',
]|filter,
text: selectionLabel ?? 'Choose'|t('app'),
disabled: disabled,
aria: {
label: selectionLabel ?? 'Choose'|t('app'),
describedby: describedBy ?? false,
}
}) }}
{% endif %}
<div class="spinner hidden"></div>
</div>
{% endtag %}
Expand All @@ -94,6 +99,8 @@
referenceElementId: referenceElement.id ?? null,
referenceElementSiteId: referenceElement.siteId ?? null,
criteria: criteria,
allowAdd: allowAdd,
allowRemove: allowRemove,
allowSelfRelations: allowSelfRelations ?? false,
maintainHierarchy: maintainHierarchy,
branchLimit: branchLimit ?? null,
Expand Down
2 changes: 1 addition & 1 deletion src/web/assets/cp/dist/cp.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/web/assets/cp/dist/cp.js.map

Large diffs are not rendered by default.

40 changes: 24 additions & 16 deletions src/web/assets/cp/src/js/BaseElementSelectInput.js
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,8 @@ Craft.BaseElementSelectInput = Garnish.Base.extend(

canAddMoreElements: function () {
return (
!this.settings.limit || this.$elements.length < this.settings.limit
this.settings.allowAdd &&
(!this.settings.limit || this.$elements.length < this.settings.limit)
);
},

Expand All @@ -217,7 +218,7 @@ Craft.BaseElementSelectInput = Garnish.Base.extend(
},

enableAddElementsBtn: function () {
if (this.$addElementBtn.length) {
if (this.settings.allowAdd && this.$addElementBtn.length) {
this.$addElementBtn.removeClass('hidden');
}

Expand Down Expand Up @@ -263,7 +264,10 @@ Craft.BaseElementSelectInput = Garnish.Base.extend(
focusNextLogicalElement: function () {
if (this.canAddMoreElements()) {
// If can add more elements, focus ADD button
if (this.$addElementBtn.length) {
if (
this.$addElementBtn.length &&
!this.$addElementBtn.hasClass('hidden')
) {
this.$addElementBtn.get(0).focus();
}
} else {
Expand Down Expand Up @@ -436,19 +440,21 @@ Craft.BaseElementSelectInput = Garnish.Base.extend(
});
}

actions.push({
icon: 'remove',
label: Craft.t('app', 'Remove'),
callback: () => {
// If the element is selected, remove *all* the selected elements
if (this.elementSelect?.isSelected($element)) {
this.removeElement(this.elementSelect.getSelectedItems());
} else {
this.removeElement($element);
}
},
destructive: true,
});
if (this.settings.allowRemove) {
actions.push({
icon: 'remove',
label: Craft.t('app', 'Remove'),
callback: () => {
// If the element is selected, remove *all* the selected elements
if (this.elementSelect?.isSelected($element)) {
this.removeElement(this.elementSelect.getSelectedItems());
} else {
this.removeElement($element);
}
},
destructive: true,
});
}

return actions;
},
Expand Down Expand Up @@ -1033,6 +1039,8 @@ Craft.BaseElementSelectInput = Garnish.Base.extend(
referenceElementId: null,
referenceElementSiteId: null,
criteria: {},
allowAdd: true,
allowRemove: true,
allowSelfRelations: false,
sourceElementId: null,
disabledElementIds: null,
Expand Down

0 comments on commit 0bf41d9

Please sign in to comment.