inertiajs / inertia

Inertia.js lets you quickly build modern single-page React, Vue and Svelte apps using classic server-side routing and controllers.
https://inertiajs.com
MIT License
6.3k stars 423 forks source link

SearchInput loses focus after performing manual visit with preserveState set to true #1800

Closed widdydev closed 7 months ago

widdydev commented 7 months ago

Version:

Describe the problem:

I have a vue page that allows the user to search through the products available on the marketplace. The search inputs data will be watched and if the value has changed, a manual visit will be performed to get the search results based on the input.

On each update the results are fetched and the list is updated, but unfortunately I'm losing the focus on the input. Did I do anything wrong? Help will be pretty much appreciated!

Steps to reproduce:

This is the vue part:

export default {
    components: {
        Head,
        SearchInput,
        ProductCard,
        Pagination,
    },
    layout: DefaultLayout,
    props: {
        products: {
            type: Object,
            required: true,
        },
        filters: {
            type: Object,
            required: true,
        },
    },
    data() {
        return {
            search: this.filters.search || '',
        };
    },
    watch: {
        search() {
            router.get(
                route('marketplace.index'),
                {
                    search: this.search,
                },
                {
                    preserveState: true,
                },
            );
        },
    },
};

And here's my controller action:

class MarketplaceController extends Controller
{
    public function index(Request $request)
    {
        $products = [];

        if ($request->input('search')) {
            $products = Product::search($request->input('search'))
                ->paginate(9);
        } else {
            $products = Product::search()
                ->orderBy('release_date', 'desc')
                ->paginate(9);
        }

        return Inertia::render('Marketplace/Index', [
            'products' => ProductResource::collection($products),
            'filters' => $request->all(['search']),
        ]);
    }
}
widdydev commented 7 months ago

There was an issue with some other component that removed the focus...