Przeglądaj źródła

Merge pull request #1270 from SimoTod/bug/x-model-select-falsy-values

Fix x-model not selecting falsy values in select dropdown
Caleb Porzio 4 lat temu
rodzic
commit
7591777551
4 zmienionych plików z 21 dodań i 3 usunięć
  1. 1 1
      dist/alpine-ie11.js
  2. 1 1
      dist/alpine.js
  3. 1 1
      src/directives/bind.js
  4. 18 0
      test/model.spec.js

+ 1 - 1
dist/alpine-ie11.js

@@ -6715,7 +6715,7 @@
     Array.from(el.options).forEach(function (option) {
       _newArrowCheck(this, _this3);
 
-      option.selected = arrayWrappedValue.includes(option.value || option.text);
+      option.selected = arrayWrappedValue.includes(option.value);
     }.bind(this));
   }
 

+ 1 - 1
dist/alpine.js

@@ -752,7 +752,7 @@
       return value + '';
     });
     Array.from(el.options).forEach(option => {
-      option.selected = arrayWrappedValue.includes(option.value || option.text);
+      option.selected = arrayWrappedValue.includes(option.value);
     });
   }
 

+ 1 - 1
src/directives/bind.js

@@ -87,6 +87,6 @@ function updateSelect(el, value) {
     const arrayWrappedValue = [].concat(value).map(value => { return value + '' })
 
     Array.from(el.options).forEach(option => {
-        option.selected = arrayWrappedValue.includes(option.value || option.text)
+        option.selected = arrayWrappedValue.includes(option.value)
     })
 }

+ 18 - 0
test/model.spec.js

@@ -746,3 +746,21 @@ test('x-model sets value before x-on directive expression is processed', async (
         expect(window.selectValueB).toEqual('bar')
     })
 })
+
+test('x-model select falsy values on select dropdown', async () => {
+    document.body.innerHTML = `
+        <div x-data="{ foo: '' }">
+            <select x-model="foo">
+                <option disabled value="">Please select one</option>
+                <option>bar</option>
+                <option>baz</option>
+            </select>
+        </div>
+    `
+
+    Alpine.start()
+
+    expect(document.querySelectorAll('option')[0].selected).toEqual(true)
+    expect(document.querySelectorAll('option')[1].selected).toEqual(false)
+    expect(document.querySelectorAll('option')[2].selected).toEqual(false)
+})