Skip to content

Add searchQuery() and autoSearch() methods to Combobox component#4

Merged
munezaclovis merged 4 commits into
mainfrom
copilot/add-default-search-query-method
Oct 22, 2025
Merged

Add searchQuery() and autoSearch() methods to Combobox component#4
munezaclovis merged 4 commits into
mainfrom
copilot/add-default-search-query-method

Conversation

Copilot AI commented Oct 17, 2025

Copy link
Copy Markdown
Contributor

Overview

This PR adds two new methods to the Combobox component that enable setting search queries and controlling automatic search triggering on component initialization.

Problem

Previously, there was no way to:

  1. Set a default value in the search input field
  2. Automatically trigger a search when the component loads

This limitation made it difficult to pre-filter options based on context, such as showing products from a specific category or filtering items based on user permissions.

Solution

Added two new fluent methods to the Combobox class:

1. searchQuery(string | Closure | null $query)

Sets the search query value in the search input without triggering a search by default. This is useful for setting initial values in the search field.

Combobox::make('product')
    ->searchable()
    ->options([/* options */])
    ->searchQuery('electronics')

2. autoSearch(bool | Closure $condition = true)

Controls whether to automatically trigger a search when the component boots. When combined with searchQuery(), this enables pre-filtering options based on context.

Combobox::make('product')
    ->searchable()
    ->getSearchResultsUsing(function (string $search) {
        return Product::where('name', 'like', "%{$search}%")
            ->limit(50)
            ->pluck('name', 'id')
            ->toArray();
    })
    ->searchQuery('electronics')
    ->autoSearch()

You can also conditionally enable auto-search:

Combobox::make('product')
    ->searchQuery($categoryName)
    ->autoSearch(filled($categoryName))

Implementation Details

Backend (PHP)

  • Added searchQuery() method to set the search query value
  • Added autoSearch() method to control automatic search behavior
  • Added corresponding getter methods with proper type hints
  • Both methods support Closure for dynamic values
  • Follows Filament's fluent interface pattern

Frontend (JavaScript)

  • Updated Combobox constructor to accept searchQuery and autoSearch parameters
  • Modified search query initialization to use the provided search query value
  • Added performInitialSearch() method that:
    • Sets the search input value
    • Performs dynamic search via getSearchResultsUsing or local filtering
    • Handles loading states and errors gracefully
    • Only executes when component is searchable and autoSearch is true

Assets

  • Built and minified JavaScript assets included
  • Updated Blade view to pass new parameters to Alpine component

Use Cases

  • Category filtering: ->searchQuery($categoryName)->autoSearch() to show products from a specific category
  • Permission-based filtering: ->searchQuery(auth()->user()->department)->autoSearch() to filter items by user context
  • Related field filtering: ->searchQuery($this->related_field)->autoSearch() to pre-filter based on another field's value
  • Search input pre-population: ->searchQuery('electronics') to set initial search text without triggering search

Documentation

Added comprehensive usage examples and explanations to the README.md showing how to use both methods and their practical applications.

Testing

  • Verified PHP method signatures and return types
  • Built JavaScript assets successfully
  • Confirmed minified JavaScript includes all new functionality
  • No linting or code style issues
Original prompt

I want to be able to pass the default searchQuery. I aee that we set it as empty string "" when creating the combobox. Could u add a method to the class defaultSearchQuery() that accespts a string ofcourse. This should not a search. Its sinply setting the default value.

Then add a searchQuery() method. This ine should trigger a search after booting the conponent. Its kinda like setting the options using options() but this time we use the searching handle it.


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Copilot AI and others added 2 commits October 17, 2025 00:18
Co-authored-by: munezaclovis <51137458+munezaclovis@users.noreply.github.com>
Co-authored-by: munezaclovis <51137458+munezaclovis@users.noreply.github.com>
Copilot AI changed the title [WIP] Add defaultSearchQuery method and searchQuery trigger Add defaultSearchQuery() and searchQuery() methods to Combobox component Oct 17, 2025
Copilot AI requested a review from munezaclovis October 17, 2025 00:23
@munezaclovis munezaclovis marked this pull request as ready for review October 21, 2025 23:36
@munezaclovis

Copy link
Copy Markdown
Contributor

@copilot Lets delete the defaultSearchQuery() and $defaultSearchQuery variable. instead use the searchQuery() function and add a "autoSearch" method instead.

instead of automatically searching when we pass a value to searchQuery() we use the "autoSearch" to indicate that behaviour.

…h() pattern

Co-authored-by: munezaclovis <51137458+munezaclovis@users.noreply.github.com>

Copilot AI commented Oct 21, 2025

Copy link
Copy Markdown
Contributor Author

@copilot Lets delete the defaultSearchQuery() and $defaultSearchQuery variable. instead use the searchQuery() function and add a "autoSearch" method instead.

instead of automatically searching when...

Done! I've refactored the implementation as requested:

Changes made (commit 72c9c38):

  1. ✅ Removed defaultSearchQuery() method and $defaultSearchQuery variable
  2. ✅ Modified searchQuery() to only set the search query value without automatically triggering a search
  3. ✅ Added new autoSearch(bool | Closure $condition = true) method to control whether the search should be triggered automatically

New API:

// Set a search query without auto-search
Combobox::make('product')
    ->searchable()
    ->searchQuery('electronics')

// Set a search query with auto-search
Combobox::make('product')
    ->searchable()
    ->getSearchResultsUsing(fn($search) => Product::search($search))
    ->searchQuery('electronics')
    ->autoSearch()

// Conditional auto-search
Combobox::make('product')
    ->searchQuery($categoryName)
    ->autoSearch(filled($categoryName))

The separation provides better control - searchQuery() sets the value, and autoSearch() determines the behavior.

Copilot AI changed the title Add defaultSearchQuery() and searchQuery() methods to Combobox component Add searchQuery() and autoSearch() methods to Combobox component Oct 21, 2025
@munezaclovis munezaclovis merged commit 40ac75e into main Oct 22, 2025
5 checks passed
@munezaclovis munezaclovis deleted the copilot/add-default-search-query-method branch October 22, 2025 01:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants