Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

IBX-3922: Fix dropdown inside modal #584

Merged
merged 3 commits into from
Oct 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 50 additions & 13 deletions src/bundle/Resources/public/js/scripts/core/dropdown.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,6 @@
this.dropdown = dropdown;
}

toggle(event) {
if (event.target.classList.contains('ibexa-dropdown__remove-selection')) {
this.dropdown.deselectOption(event.target.closest('.ibexa-dropdown__selected-item'));

return;
}

super.toggle(event);
}

show() {
if (this.dropdown.container.classList.contains('ibexa-dropdown--disabled')) {
return;
Expand Down Expand Up @@ -81,13 +71,24 @@
ibexa.helpers.objectInstances.setInstance(this.container, this);
}

attachSelectedItemEvents(item) {
const removeSelectionBtn = item.querySelector('.ibexa-dropdown__remove-selection');

removeSelectionBtn.addEventListener('click', (event) => {
event.stopPropagation();
this.deselectOption(item);
});
}

createSelectedItem(value, label, icon) {
const container = doc.createElement('div');
const selectedItemRendered = this.selectedItemTemplate.replace('{{ value }}', value).replace('{{ label }}', label);

container.insertAdjacentHTML('beforeend', selectedItemRendered);

const selectedItemNode = container.querySelector('.ibexa-dropdown__selected-item');
const removeSelectionBtn = selectedItemNode.querySelector('.ibexa-dropdown__remove-selection');

if (icon) {
const iconWrapper = container.querySelector('.ibexa-dropdown__selected-item-icon');
const selectedItemIconRendered = this.selectedItemIconTemplate.replace('{{ icon }}', icon);
Expand All @@ -97,6 +98,13 @@

selectedItemNode.classList.toggle('ibexa-dropdown__selected-item--has-icon', !!icon);

this.attachSelectedItemEvents(selectedItemNode);

removeSelectionBtn.addEventListener('click', (event) => {
event.stopPropagation();
this.deselectOption(selectedItemNode);
});

return selectedItemNode;
}

Expand Down Expand Up @@ -444,16 +452,14 @@
return;
}

const modalDialog = this.container.closest('.modal-dialog');

this.itemsPopover = new DropdownPopover(
this.selectedItemsContainer,
{
html: true,
placement: 'bottom',
customClass: 'ibexa-dropdown-popover',
content: this.itemsPopoverContent,
container: modalDialog || 'body',
Copy link
Contributor Author

@tischsoic tischsoic Sep 28, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Having a dropdown popup inside a modal caused problems when the modal was embedded inside a tab, which has overflow: hidden CSS style.

container: 'body',
},
{ dropdown: this },
);
Expand All @@ -478,13 +484,44 @@
.forEach((option) => option.addEventListener('click', this.onOptionClick, false));

if (this.itemsFilterInput) {
const modal = this.container.closest('.modal');
const popupInputs = this.itemsContainer.querySelectorAll('input');

popupInputs.forEach((popupInput) =>
popupInput.addEventListener(
'focusin',
() => {
const modalInstance = bootstrap.Modal.getInstance(modal);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I get an instance every time just in case it changes etc. I also assume that this event may be in some very rare cases called before any instance was created and I don't want to create an instance inside the dropdown class.
Maybe it is too cautious.


if (modalInstance) {
modalInstance._focustrap.deactivate();
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bootstrap modal uses FocusTrap which steals focus if it is outside the modal
https://github.com/twbs/bootstrap/blob/597c4023141dc48868889b85676b2d7269411d23/js/src/modal.js#L163


this.itemsFilterInput.addEventListener(
'focusout',
() => {
modalInstance._focustrap.activate();
},
{ once: true },
);
}
},
false,
),
);

this.itemsFilterInput.addEventListener('keyup', this.filterItems, false);
this.itemsFilterInput.addEventListener('input', this.filterItems, false);
}

this.sourceOptionsObserver.observe(this.sourceInput, {
childList: true,
});

const selectedItems = this.container.querySelectorAll(
'.ibexa-dropdown__selected-item:not(.ibexa-dropdown__selected-overflow-number):not(.ibexa-dropdown__selected-placeholder)',
);

selectedItems.forEach((selectedItem) => this.attachSelectedItemEvents(selectedItem));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
{% if multiple is defined and multiple %}
<input type="checkbox" name="dropdown-checkbox" class="ibexa-input ibexa-input--checkbox" {% if is_selected %}checked{% endif %} />
{% endif %}
{% if icon is defined %}
{% if icon is defined and icon != '' %}
<svg class="ibexa-icon ibexa-icon--small {{ icon_class|default('') }}">
<use xlink:href="{{ ibexa_icon_path(icon) }}"></use>
</svg>
Expand Down