Skip to content

Conversation

@janosh
Copy link
Owner

@janosh janosh commented Oct 3, 2022

Closes #86.

@davipon I'd be curious to hear your opinion on this approach (since you expressed interest in #86).

The approach being to define a new internal _selected variable and making the exported prop selected reactive to it conditional on the value of maxSelect:

let _selected = selected as Option[]
$: selected = maxSelect === 1 ? _selected[0] ?? null : _selected

And the same for selected{Labels,Values}

  $: _selectedLabels = _selected?.map(get_label) ?? []
  $: selectedLabels = maxSelect === 1 ? _selectedLabels[0] ?? null : _selectedLabels
  $: _selectedValues = _selected?.map(get_value) ?? []
  $: selectedValues = maxSelect === 1 ? _selectedValues[0] ?? null : _selectedValues

@wd-David
Copy link
Contributor

wd-David commented Oct 3, 2022

@janosh That's a brilliant way! Can't wait to reduce my boilerplates for single select.

@janosh janosh changed the title Make selected{,Labels,Values} a single value (not a length-1 array) Make selected a single value (not a length-1 array) if maxSelect=1 Oct 3, 2022
@janosh
Copy link
Owner Author

janosh commented Oct 3, 2022

Gotta love Svelte! ❤️ I expected the fix for #86 to take more work and break several tests. 😄

@janosh janosh merged commit cb0f8be into main Oct 3, 2022
@janosh janosh deleted the fix-gh-86 branch October 3, 2022 16:08
@janosh
Copy link
Owner Author

janosh commented Oct 3, 2022

Released as v7.0.0. 🎉

janosh referenced this pull request in sbsev/site Oct 6, 2022
@janosh janosh mentioned this pull request Oct 22, 2022
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.

What should bind:selected be when maxSelect={1}?

3 participants