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

close by click on open dropdown #1012

Closed
rene-mueller opened this issue Feb 14, 2022 · 6 comments
Closed

close by click on open dropdown #1012

rene-mueller opened this issue Feb 14, 2022 · 6 comments

Comments

@rene-mueller
Copy link

Is your feature request related to a problem? Please describe.
In order for users to be able to use the dropdown even better (especially on the mobile view), a feature is needed that closes the dropdown when clicked.

Describe alternatives you've considered
https://react-select.com/home

@mtriff
Copy link
Member

mtriff commented Feb 15, 2022

You can implement this using hideDropdown.

@rene-mueller
Copy link
Author

i have already tried this. unfortunately it did not work, because it is not possible to check from the outside if the dropdown is open or not.

@mtriff
Copy link
Member

mtriff commented Feb 16, 2022

Why is that necessary? You can run hideDropdown whether or not the dropdown is open or not.

Regardless, you can check whether it's open or not by accessing choicesInstance.dropdown.isActive.

@rene-mueller
Copy link
Author

In order to close the dropdown menu, a listener must be made on the click event of the input. Then this is triggered not only when closing but also when opening. Thus, a check must be made so that it can be decided what happens.

In my first attempt, I have checked for css classes, then the dropdown menu would be opened and instantly closed again (by the double click event). Thus, with their property will also not be possible.

@lieto-dk
Copy link

Hi,

I use this setup as workaround - it closes on change and makes it possible to use choices well with a mobile device.

<script>
    const country_select = document.querySelector('.country_select');
    const country_choices = new Choices(country_select, {
        itemSelectText: "+",
        removeItemButton: true,
        position: "bottom",
        resetScrollPosition:false,
        noResultsText: '{{ __('front.searchform_select2_no_results')}}',
        noChoicesText: '{{ __('front.searchform_select2_no_choices')}}',
    });
    country_select.addEventListener(
        'change',
        function(event) {
            country_choices.hideDropdown();
        },
        false,
    );
</script>

@Sysix
Copy link

Sysix commented Aug 4, 2022

I facing the same problem. Here my code:

        const event = () => {
            if (api.dropdown.isActive) {
                api.hideDropdown();
            }
        }

        api.*.element.addEventListener('pointerdown', event);

The problem is, that Choices-Js will close the dropdown and then reopen it. Tried to wait with setTimeout(.., 0) and requestAnimationFrame(...). Sadly the change didn't had a effect.

On Touch Devices it is frustrating to not know how to close the select. @lieto-dk solution is not ok for me because i'm handling a multi select. Everytime reopening it is bad.

Xon added a commit to Xon/Choices.js that referenced this issue Aug 16, 2024
@Xon Xon mentioned this issue Aug 22, 2024
9 tasks
@Xon Xon closed this as completed Aug 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants