Skip to content

Enhance Search: Search All Nodes (Incl. Collapsed) & Auto-Expand — True Filter, Not Just Highlight #123

@tearf001

Description

@tearf001

Is your feature request related to a problem? Please describe.

My understanding is that headless-tree's search currently acts more like highlighting within visible items rather than a true filter across the entire tree. It filters based on tree.getItems(), meaning nodes in collapsed folders are undiscoverable. This can be frustrating, as users might incorrectly assume an item doesn't exist when it's merely hidden.

Describe the solution you'd like

I would like the library to provide built-in support for a search mechanism that comprehensively scans all nodes (both visible and collapsed) and, upon finding matches, automatically expands the necessary parent paths to reveal these matches. This would align the search behavior more closely with a true "filtering" paradigm rather than just "highlighting."

This could be implemented as:

  1. Comprehensive Node Scanning: Extend the existing search feature (or introduce a mode) to traverse the entire data source, regardless of the current expansion state of nodes.
  2. Automatic Path Expansion: When matches are found within collapsed nodes, automatically expand all parent nodes in the path leading to the matched item(s), making them visible to the user.
  3. Configuration Option: Introduce a configuration option, perhaps like searchMode: 'filter-all' | 'highlight-visible' or a boolean searchCollapsedNodes: boolean, to control this behavior. This would allow developers to choose the search paradigm that best suits their application. The default could remain the current behavior for backward compatibility.

Describe alternatives you've considered

Currently, achieving this comprehensive search behavior requires developers to implement custom solutions, which typically involve:

  • Overriding the default getSearchMatchingItems method or implementing a custom search feature.
  • Manually traversing the entire data source, potentially using methods like tree.retrieveChildrenIds() recursively.
  • Managing the expansion state of parent paths using tree.applySubStateUpdate("expandedItems", ...).
  • Potentially leveraging the plugin pattern for more complex integrations.

While these approaches are feasible, they demand significant boilerplate code, a deep understanding of the library's internal workings, and shift the burden of implementing a common and expected UX pattern onto the developer.

Additional context

This "search-and-reveal" or "filter-and-expand" functionality is a standard user experience pattern in most tree components. Users generally expect a search operation to be exhaustive, typically expand collapsed directories/folders to show search results contained within them.

The implementation could potentially leverage aspects of existing functionalities (like expandAll) but would need to be more selective, only expanding paths that directly lead to search matches.

Proposed API (Example)

const tree = useTree({
  // ... other config
  searchMode: 'filter-all', // 'filter-all' (searches all, expands) vs 'highlight-visible' (current default)
  features: [
    syncDataLoaderFeature,
    searchFeature, // Enhanced to support the new searchMode or searchCollapsedNodes
    // ... other features
  ],
});

This approach would ideally maintain backward compatibility while offering this enhanced, more intuitive search functionality as an opt-in feature or a configurable mode.

Metadata

Metadata

Assignees

Labels

No labels
No labels

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions