Skip to content

Commit

Permalink
[BootstrapAdminUi] Fix missing menu search (#112)
Browse files Browse the repository at this point in the history
  • Loading branch information
loic425 authored Oct 15, 2024
2 parents 73b79e6 + 599fc87 commit 907272e
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/BootstrapAdminUi/assets/entrypoint.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,6 @@ import './app';

import './scripts/bulk-delete';
import './scripts/check-all';
import './scripts/menu-search';

import './scripts/bootstrap';
70 changes: 70 additions & 0 deletions src/BootstrapAdminUi/assets/scripts/menu-search.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/*
* This file is part of the Sylius package.
*
* (c) Sylius Sp. z o.o.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

/* global document */

(function() {
const menuSearchInput = document.querySelector('[data-menu-search]');
const menuClearButton = document.querySelector('[data-menu-search-clear]');

const clearInput = () => {
menuSearchInput.value = '';
menuSearchInput.dispatchEvent(new Event('input'));
};

if (menuSearchInput) {
menuSearchInput.addEventListener('input', function(e) {
const query = e.target.value.toLowerCase();
const navItems = document.querySelectorAll('.sidebar .nav-item');

navItems.forEach(navItem => {
const navLink = navItem.querySelector('.nav-link');
const dropdownMenu = navItem.querySelector('.dropdown-menu');
const dropdownItems = navItem.querySelectorAll('.dropdown-item');
let matchFound = false;

dropdownItems.forEach(item => {
const text = item.textContent.toLowerCase();
if (query === '' || text.includes(query)) {
item.style.display = '';
matchFound = true;
} else {
item.style.display = 'none';
}
});

if (matchFound || query === '') {
navItem.style.display = '';
} else {
navItem.style.display = 'none';
}

if (query !== '') {
if (navLink) navLink.classList.add('d-flex');
if (dropdownMenu) dropdownMenu.classList.add('d-flex');
} else {
if (navLink) navLink.classList.remove('d-flex');
if (dropdownMenu) dropdownMenu.classList.remove('d-flex');
}
});
});

menuSearchInput.addEventListener('keydown', function(event) {
if (event.key === 'Escape') {
clearInput();
}
});
}

if (menuClearButton) {
menuClearButton.addEventListener('click', function() {
clearInput();
});
}
})();
2 changes: 1 addition & 1 deletion src/BootstrapAdminUi/public/app.js

Large diffs are not rendered by default.

0 comments on commit 907272e

Please sign in to comment.