1818import java .util .ArrayList ;
1919import java .util .Iterator ;
2020import java .util .List ;
21- import java .util .function .BiPredicate ;
22- import java .util .function .Function ;
2321
2422import org .gwtproject .event .shared .HandlerRegistration ;
2523import org .jboss .elemento .Attachable ;
3028import org .patternfly .component .BaseComponent ;
3129import org .patternfly .component .ComponentType ;
3230import org .patternfly .component .SelectionMode ;
33- import org .patternfly .component .textinputgroup .TextInputGroup ;
3431import org .patternfly .handler .MultiSelectHandler ;
3532import org .patternfly .handler .SelectHandler ;
3633import org .patternfly .style .Classes ;
5047import static elemental2 .dom .DomGlobal .window ;
5148import static java .util .stream .Collectors .toList ;
5249import static org .jboss .elemento .Elements .div ;
53- import static org .jboss .elemento .Elements .failSafeRemoveFromParent ;
54- import static org .jboss .elemento .Elements .setVisible ;
50+ import static org .jboss .elemento .Elements .isVisible ;
5551import static org .jboss .elemento .EventType .keydown ;
5652import static org .jboss .elemento .Key .ArrowDown ;
5753import static org .jboss .elemento .Key .ArrowLeft ;
6662import static org .patternfly .component .divider .DividerType .hr ;
6763import static org .patternfly .component .menu .MenuFooter .menuFooter ;
6864import static org .patternfly .component .menu .MenuHeader .menuHeader ;
69- import static org .patternfly .component .menu .MenuItem .menuItem ;
7065import static org .patternfly .style .Classes .component ;
7166import static org .patternfly .style .Classes .disabled ;
7267import static org .patternfly .style .Classes .divider ;
@@ -106,13 +101,9 @@ public static Menu menu(MenuType menuType, SelectionMode selectionMode) {
106101 final SelectionMode selectionMode ;
107102 final List <MenuActionHandler > actionHandler ;
108103 boolean favorites ;
109- private MenuSearch search ;
110- private MenuContent content ;
111- private MenuItem noResultsItem ;
104+ MenuContent content ;
112105 private final List <SelectHandler <MenuItem >> selectHandler ;
113106 private final List <MultiSelectHandler <Menu , MenuItem >> multiSelectHandler ;
114- private BiPredicate <MenuItem , String > searchFilter ;
115- private Function <String , MenuItem > noResultsProvider ;
116107 private HandlerRegistration keyHandler ;
117108
118109 Menu (MenuType menuType , SelectionMode selectionMode ) {
@@ -123,7 +114,6 @@ public static Menu menu(MenuType menuType, SelectionMode selectionMode) {
123114 this .actionHandler = new ArrayList <>();
124115 this .selectHandler = new ArrayList <>();
125116 this .multiSelectHandler = new ArrayList <>();
126- this .noResultsProvider = value -> menuItem (Id .unique ("no-results" ), "No results found" ).disabled ();
127117 // TODO Without this workaround the menu "flickers" when showing.
128118 // This could be solved by replacing the show/hide alg with an add/remove alg in the Popper class
129119 componentVar (component (menu ), "TransitionDuration" ).applyTo (this ).set (0 );
@@ -135,31 +125,6 @@ public static Menu menu(MenuType menuType, SelectionMode selectionMode) {
135125 public void attach (MutationRecord mutationRecord ) {
136126 allowTabFirstItem ();
137127 keyHandler = EventType .bind (window , keydown , this ::keyHandler );
138- if (searchFilter != null ) {
139- if (content == null ) {
140- logger .warn ("Menu %o has a search filter, but no content was added." , element ());
141- } else if (!content .groups .isEmpty ()) {
142- logger .warn ("Menu %o a search filter and groups. Search filters are not supported for grouped menus." ,
143- element ());
144- } else if (search == null ) {
145- logger .warn ("Menu %o has a search filter, but no search menu was added." , element ());
146- } else if (search .searchInput () == null ) {
147- logger .warn ("Menu %o has a search filter, but no search input was added." , element ());
148- } else if (search .searchInput ().textInputGroup () == null ) {
149- logger .warn ("Menu %o has a search filter, but no text input group was added." , element ());
150- } else {
151- TextInputGroup textInputGroup = search .searchInput ().textInputGroup ();
152- if (textInputGroup != null ) {
153- textInputGroup
154- .onKeyup ((event , tig , value ) -> search (value ))
155- .onChange ((event , tig , value ) -> {
156- if (value .isEmpty ()) {
157- clearSearch ();
158- }
159- });
160- }
161- }
162- }
163128 }
164129
165130 @ Override
@@ -208,13 +173,6 @@ public Menu addSearch(MenuSearch search) {
208173 return add (search );
209174 }
210175
211- // override to ensure internal wiring
212- public Menu add (MenuSearch search ) {
213- this .search = search ;
214- add (search .element ());
215- return this ;
216- }
217-
218176 public Menu addDivider () {
219177 return add (divider (hr ));
220178 }
@@ -261,28 +219,6 @@ public Menu onMultiSelect(MultiSelectHandler<Menu, MenuItem> selectHandler) {
261219 return this ;
262220 }
263221
264- /**
265- * Configures a search behavior for the menu by applying a search filter. For this to work, you need to add a
266- * {@link MenuSearch}, {@link MenuSearchInput} and {@link TextInputGroup}.
267- *
268- * <p>
269- * {@snippet class = MenuDemo region = search}
270- *
271- * @param searchFilter a {@link BiPredicate} that defines the search logic. The first parameter is a {@link MenuItem}
272- * representing a menu item, and the second parameter is a {@link String} representing the search query.
273- * The predicate should return {@code true} for items matching the search.
274- * @return the {@link Menu} instance for method chaining.
275- */
276- public Menu onSearch (BiPredicate <MenuItem , String > searchFilter ) {
277- this .searchFilter = searchFilter ;
278- return this ;
279- }
280-
281- public Menu onNoResults (Function <String , MenuItem > noResults ) {
282- this .noResultsProvider = noResults ;
283- return this ;
284- }
285-
286222 // ------------------------------------------------------ api
287223
288224 /**
@@ -443,42 +379,12 @@ private void unselectAllInGroup(MenuItem item) {
443379 }
444380 }
445381
446- private void search (String value ) {
447- int visibleItems = 0 ;
448- for (MenuItem menuItem : items ()) {
449- boolean visible = searchFilter .test (menuItem , value );
450- setVisible (menuItem , visible );
451- if (visible ) {
452- visibleItems ++;
453- }
454- }
455- failSafeRemoveFromParent (noResultsItem );
456- if (visibleItems == 0 ) {
457- if (content != null && content .list != null ) {
458- noResultsItem = noResultsProvider .apply (value );
459- // Don't use content.list.add(noResultsItem) here
460- // The no-result item should not be part of the item map
461- content .list .add (noResultsItem .element ());
462- }
463- } else {
464- allowTabFirstItem ();
465- }
466- }
467-
468- private void clearSearch () {
469- failSafeRemoveFromParent (noResultsItem );
470- for (MenuItem menuItem : items ()) {
471- setVisible (menuItem , true );
472- }
473- allowTabFirstItem ();
474- }
475-
476382 // ------------------------------------------------------ keyboard navigation
477383
478384 private void keyHandler (KeyboardEvent event ) {
479385 HTMLElement activeElement = (HTMLElement ) document .activeElement ;
480386 if (element ().contains (((Node ) event .target ))) {
481- JsArray <HTMLElement > navigableElements = getNavigableElement (element ());
387+ JsArray <HTMLElement > navigableElements = navigableElement (element ());
482388 if (navigableElements .length == 0 ) {
483389 logger .warn ("Menu %o has no navigable elements. Keyboard navigation will be ignored." , element ());
484390 }
@@ -572,10 +478,10 @@ private void handleArrows(KeyboardEvent event, HTMLElement activeElement, JsArra
572478 }
573479 }
574480
575- private JsArray <HTMLElement > getNavigableElement (HTMLElement element ) {
481+ private JsArray <HTMLElement > navigableElement (HTMLElement element ) {
576482 JsArray <HTMLElement > elements = JsArray .from (element .querySelectorAll ("li" ).values ());
577483 return elements .filter ((e , i ) ->
578- !(e .classList .contains (modifier (disabled )) || e .classList .contains (component (divider ))));
484+ isVisible ( e ) && !(e .classList .contains (modifier (disabled )) || e .classList .contains (component (divider ))));
579485 }
580486
581487 private HTMLElement getFocusableElement (HTMLElement navigableElement ) {
0 commit comments