Skip to content

Commit

Permalink
fixup! feat(menu): add search functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
BjornOstberg committed Oct 6, 2023
1 parent 9f32f9d commit c8ca354
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 10 deletions.
8 changes: 4 additions & 4 deletions src/components/menu/examples/menu-searchable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export class MenuSubItemsExample {
private items: Array<MenuItem | ListSeparator> = [
{
text: 'Format',
subItems: [
items: [
{
text: 'Bold',
icon: 'bold',
Expand All @@ -30,7 +30,7 @@ export class MenuSubItemsExample {
{
text: 'Bullets and numbering',
icon: 'bulleted_list',
subItems: [
items: [
{
text: 'Numbered list',
icon: 'numbered_list',
Expand All @@ -49,7 +49,7 @@ export class MenuSubItemsExample {
},
{
text: 'Edit',
subItems: [
items: [
{
text: 'Copy',
icon: 'copy',
Expand All @@ -67,7 +67,7 @@ export class MenuSubItemsExample {
},
{
text: 'Long sub list',
subItems: Array.from(Array(50), (_value, index) => {
items: Array.from(Array(50), (_value, index) => {
return {
text: `Item ${index + 1}`,
};
Expand Down
19 changes: 13 additions & 6 deletions src/components/menu/examples/subitems-search.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,25 +12,32 @@ export function SearchMenuItems(
const flattenedItems = flattenMenuItems(menuItems);

return flattenedItems.filter(
(i) => !i.separator && i.text?.toLowerCase().includes(searchValue)
(i) =>
!('separator' in i) && i.text?.toLowerCase().includes(searchValue)
);
}

function flattenMenuItems(menuItems: MenuItem[]): MenuItem[] {
function flattenMenuItems(
menuItems: Array<MenuItem | ListSeparator>
): MenuItem[] {
const flattenedItems: MenuItem[] = [];

function flatten(menuItem: MenuItem) {
flattenedItems.push(menuItem);

if (menuItem.subItems) {
for (const subItem of menuItem.subItems) {
flatten(subItem);
if (Array.isArray(menuItem.items)) {
for (const subItem of menuItem.items) {
if (!('separator' in subItem)) {
flatten(subItem as MenuItem);
}
}
}
}

for (const menuItem of menuItems) {
flatten(menuItem);
if (!('separator' in menuItem)) {
flatten(menuItem as MenuItem);
}
}

return flattenedItems;
Expand Down

0 comments on commit c8ca354

Please sign in to comment.