mskocik / svelecte

Flexible autocomplete/select component written in Svelte,
https://svelecte.vercel.app
MIT License
472 stars 44 forks source link

max on creatable #238

Closed anatoliy-t7 closed 4 months ago

anatoliy-t7 commented 4 months ago

Hello.

How do I can control max for creatable items?

Thank you for the great component.

mskocik commented 4 months ago

max controls amount of selected options. There is no limit how many items can user create. It's irrelevant, because only selectied items matters (from components' point of view)

Does it answer your question?

anatoliy-t7 commented 4 months ago

Yes. Thank you. I need to find a way to limit of created items.

mskocik commented 4 months ago

You can use your own createHandler and internally count number of created items. And throw an error when new item shouldn't be created.

anatoliy-t7 commented 4 months ago

I did it. But it is still added in DOM but not in value.

function onCreateOption(option: { value: any }) {
    if (value.length < max) {
        value.push(option.value);

        value = value;
    }
}
mskocik commented 4 months ago

Actually you want to do something like in this REPL:

I found some issues with related code, so update to the latest 4.2.3 stable version. If you are using svelecte@next, I will update it in a minute...