Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Combobox and listbox: Save optionKey in x-data to fix usage in Livewire #3990

Conversation

gdebrauwer
Copy link
Contributor

@gdebrauwer gdebrauwer commented Jan 22, 2024

I created a listbox (or combobox) with options that are present in HTML and not generated by javascript.

<!-- in the blade file of a livewire component -->

<div>
    <button wire:click="$refresh" type="button">trigger livewire update</button>

    <div x-data="{ value: null }">
        <div x-listbox x-model="value">
            <button x-listbox:button>
                Select Framework
            </button>

            <ul x-listbox:options>
                <li :class="{
                    'text-blue-100': $listboxOption.isActive,
                    'text-yellow-100': !$listboxOption.isActive,
                    '!text-gray-60 opacity-50 cursor-not-allowed': $listboxOption.isDisabled,
                }" x-listbox:option value="laravel">
                    Laravel
                </li>

                <li :class="{
                    'text-blue-100': $listboxOption.isActive,
                    'text-yellow-100': !$listboxOption.isActive,
                    '!text-gray-60 opacity-50 cursor-not-allowed': $listboxOption.isDisabled,
                }" x-listbox:option value="rails">
                    Ruby on Rails
                </li>

                <li :class="{
                    'text-blue-100': $listboxOption.isActive,
                    'text-yellow-100': !$listboxOption.isActive,
                    '!text-gray-60 opacity-50 cursor-not-allowed': $listboxOption.isDisabled,
                }" x-listbox:option value="phoenix" :disabled="true">
                    Phoenix
                </li>
            </ul>
        </div>

        Selected: <span x-text="value"></span>
    </div>
</div>

When I use the listbox/combobox in a livewire component and a livewire update is triggered (by clicking the "trigger livewire update" button in the example above), then the following javascript warnings and errors occur:

Screenshot 2024-01-22 at 08 23 49

When I debugged the issue, I discovered that the __optionKey on the option HTML element disappears after the Livewire update. To fix that, I updated the listbox/combobox code so that the "optionKey" is saved in the Alpinejs' x-data as that data will not be disappear after a Livewire update.

I did not add tests because I'm unsure how to test this as Livewire causes it.

This issue and another Livewire-related issue (livewire/livewire#7566) are currently the only 2 bugs preventing me from using the listbox/combobox in Livewire. I have tried to debug the other issue, but after many hours of searching, I have still not found what is causing the infinite loop.

gdebrauwer and others added 4 commits January 8, 2024 09:45
When optionKey is not saved in x-data, the value is lost after a Livewire update
@calebporzio
Copy link
Collaborator

Ok thanks for the extra context. I was able to reproduce this and can confirm your fix is good.

I added tests and found a couple problems with the current implementation, but a made a few tweaks and hopefully we're good now!

Maybe you can submit a failing test PR to Livewire for the other issue and we can tackle that as well? Thanks @gdebrauwer for the effort and contributions!

@calebporzio calebporzio merged commit 5fe438f into alpinejs:main Jan 22, 2024
1 check passed
@gdebrauwer
Copy link
Contributor Author

@calebporzio I created a PR with some failing tests: livewire/livewire#7776

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