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

Create Item for multi select. #1114

Closed
razvanFrandes opened this issue Apr 17, 2023 · 2 comments
Closed

Create Item for multi select. #1114

razvanFrandes opened this issue Apr 17, 2023 · 2 comments

Comments

@razvanFrandes
Copy link

razvanFrandes commented Apr 17, 2023

I didn't seem to find any option to create a item, when I'm using the <select multiselect></select>, it is possible using <input /> but there is no dropdown where I can select the values.

@sahapranta
Copy link

I am managing this somehow, at least working. I can add custom tags

    const tagsElement = document.querySelector('#input-tags');

    tagsElement.addEventListener('keydown', (e) => {
        if (e.key === 'Enter') {
            e.preventDefault();
            e.stopPropagation();
        }
    });

    const inputTags = new Choices(tagsElement, {
        allowHTML: true,        
        shouldSort: false,
        searchEnabled: true,
        removeItemButton: true,
        maxItemCount: 10,
        placeholder: true,
        placeholderValue: "{{ __('Type or Select skills') }}",
    });

    tagsElement.addEventListener('search', debounce(e => searchTags(e)));
    let enterKeyHandlerAdded = false;
    let currentValue = '';

    async function searchTags(event) {
        let {
            value,
            resultCount
        } = event.detail;

        if (value && value.length > 1 && resultCount === 0) {
            let element = document.querySelector(".has-no-results");
            if (element) {
                element.innerHTML = `Press enter to add <b>"${value}"</b>`;
            }

            if (!enterKeyHandlerAdded) {
                enterKeyHandlerAdded = true;
                document.body.addEventListener('keyup', setCustomTag);
            }

            currentValue = value;
        } else if (enterKeyHandlerAdded) {
            removeEnterListenerFromInput();
        }
    }

    function setCustomTag(e) {
        e.preventDefault();
        e.stopPropagation();
        if (e.key === 'Enter') {
            if (currentValue) {
                inputTags.setValue([{
                    label: currentValue,
                    value: currentValue
                }]);
                inputTags.clearInput();
            }
            removeEnterListenerFromInput();
        }
    }

    function removeEnterListenerFromInput() {
        document.body.removeEventListener('keyup', setCustomTag);
        enterKeyHandlerAdded = false;
        currentValue = '';
    }

    function debounce(fn, t = 600) {
        let timer;
        return (...args) => {
            clearTimeout(timer);
            timer = setTimeout(() => fn.apply(this, args), t);
        };
    }

@Xon Xon mentioned this issue Aug 6, 2024
9 tasks
@Xon
Copy link
Collaborator

Xon commented Aug 22, 2024

Implemented as part of #1166

@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

3 participants