Choices-js / Choices

A vanilla JS customisable select box/text input plugin ⚡️
https://choices-js.github.io/Choices/
MIT License
6.23k stars 613 forks source link

Alpine.js compatibility breaks with <select> value becoming object '{value: "x"}' instead of 'x' #1222

Open Fak3 opened 1 month ago

Fak3 commented 1 month ago

When user selects \<option value="x">, then \<select> value bound with alpinejs x-model attribute becomes '{value: "x"}' instead of 'x'

To Reproduce

<script defer src="https://cdn.jsdelivr.net/npm/alpinejs@3.14.1/dist/cdn.js"></script>
<script defer src="https://cdn.jsdelivr.net/npm/choices.js@11.0.2/public/assets/scripts/choices.min.js"></script>

<select 
  x-init="new Choices($el, {})" 
  x-data="{curvalue: null}" 
  x-model="curvalue" 
  @change='console.log(JSON.stringify(curvalue))'
>
    <option value="x">First item</option>
    <option value="y">Second item</option>
</select>

Expected behavior Expected same behavior as regular \<select>: When user selects \<option value="x">, then \<select> value becomes 'x'

Choices version and bundle

Desktop (please complete the following information):

Fak3 commented 1 month ago

My current workaround is to assign alpine data manually on change instead of using x-model:

<select 
  x-init="$el.choices = new Choices($el, {})" 
  x-data="{curvalue: null}" 
  @change='curvalue = $el.choices.getValue(true)'
>