-
Notifications
You must be signed in to change notification settings - Fork 38
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
What should bind:selected
be when maxSelect={1}
?
#86
Comments
bind:selected
give when maxSelect={1}
? Length-1 array or the value?bind:selected
be when maxSelect={1}
?
+1 for this. I need to handle single select like this at this moment: import type { DispatchEvents } from 'svelte-multiselect'
const updateSelected = ( event: CustomEvent<DispatchEvents['change']> ): string => {
const { type, option } = event.detail
if (type === 'add') return option as string
else if (type === 'remove') return ''
} <MultiSelect
required
maxSelect={1}
options={myOptionArray}
on:change={(event) => (myBindSelected = updateSelected(event))}
/> |
@davipon Good to know there's interest. For now, wouldn't this be a simpler workaround for your use case? <script>
import MultiSelect from 'svelte-multiselect'
const myOptionArray = [`...`]
let selected = []
$: value = selected[0] ?? null
</script>
<pre><code>value = {value}</code></pre>
<MultiSelect options={myOptionArray} bind:selected maxSelect={1} /> |
Yes, this was my first attempt, but I changed to use my previous approach. The reason is that I have a form that has multiple single-select and multi-select: interface myForm {
singleSelect: string
multiSelect: string[]
} We can't get selected values by submitting the form so far. I need to create an object like <MultiSelect
name="singleSelect"
required
maxSelect={1}
options={myOptionArray1}
on:change={(event) => (myForm.singleSelect = updateSelected(event))}
/>
<MultiSelect
name="multiSelect"
required
options={myOptionArray2}
bind:selected={myForm.multiSelect}
/> |
How about binding the selected values into a store? import { writable } from 'svelte/store'
const formData = writable({}) and then in your form component <script>
import MultiSelect from 'svelte-multiselect'
import { formData } from '../stores'
const myOptionArray = [`...`]
let selected = []
const fieldName = `mySingleSelect`
$: $formData[fieldName] = selected[0] ?? null
</script>
<MultiSelect options={myOptionArray} bind:selected maxSelect={1} /> |
And how would that work if I want to set values from a store? $: selected = $formData.fieldName I am currently trying to get multiple select components running on the same page but that doesn't seem to work. |
@magicbyt3 If you want to set values from a store, it's best to bind directly to the store. In that case, the store value needs to be an array. There's currently no way around that if you want to keep the ability to update the component via the store. See https://github.com/janosh/awesome-sveltekit/blob/47bad49c72/site/src/components/Filters.svelte#L26 for a real-world example. |
Thanks for that. For my purpose it was better to create an own multi select component. Not ready to publish yet but works so far. Your implementation inspired me on the way. :-) |
Length-1 array or the value? Currently gives a length-1 array.
This issue arose out of private discord communication with fnAki dated 21/06/2022. Copied here so I don't forget about it.
2 todos from this:
allowRemoveLast
propbind:selected
should return a single value (not as a length-1 array) whenmaxSelect={1}
.The text was updated successfully, but these errors were encountered: