Skip to content

Commit 7ccac2d

Browse files
committed
Allow Flux checkbox/radio/switch custom elements to use x-model like native inputs - ref livewire/flux#336
1 parent b52577b commit 7ccac2d

File tree

2 files changed

+15
-7
lines changed

2 files changed

+15
-7
lines changed

packages/alpinejs/src/directives/x-model.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1+
import bind, { isCheckbox, isRadio, safeParseBoolean } from '../utils/bind'
12
import { evaluateLater } from '../evaluator'
23
import { directive } from '../directives'
34
import { mutateDom } from '../mutation'
45
import { nextTick } from '../nextTick'
5-
import bind, { safeParseBoolean } from '../utils/bind'
6-
import on from '../utils/on'
76
import { isCloning } from '../clone'
7+
import on from '../utils/on'
88

99
directive('model', (el, { modifiers, expression }, { effect, cleanup }) => {
1010
let scopeTarget = el
@@ -71,7 +71,7 @@ directive('model', (el, { modifiers, expression }, { effect, cleanup }) => {
7171

7272
if (modifiers.includes('fill'))
7373
if ([undefined, null, ''].includes(getValue())
74-
|| (el.type === 'checkbox' && Array.isArray(getValue()))
74+
|| (isCheckbox(el) && Array.isArray(getValue()))
7575
|| (el.tagName.toLowerCase() === 'select' && el.multiple)) {
7676
setValue(
7777
getInputValue(el, modifiers, { target: el }, getValue())
@@ -138,7 +138,7 @@ function getInputValue(el, modifiers, event, currentValue) {
138138
// so we return event.target.value instead of event.detail
139139
if (event instanceof CustomEvent && event.detail !== undefined)
140140
return event.detail !== null && event.detail !== undefined ? event.detail : event.target.value
141-
else if (el.type === 'checkbox') {
141+
else if (isCheckbox(el)) {
142142
// If the data we are binding to is an array, toggle its value inside the array.
143143
if (Array.isArray(currentValue)) {
144144
let newValue = null;
@@ -176,7 +176,7 @@ function getInputValue(el, modifiers, event, currentValue) {
176176
} else {
177177
let newValue
178178

179-
if (el.type === 'radio') {
179+
if (isRadio(el)) {
180180
if (event.target.checked) {
181181
newValue = event.target.value
182182
} else {

packages/alpinejs/src/utils/bind.js

+10-2
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ export default function bind(el, name, value, modifiers = []) {
3939
}
4040

4141
function bindInputValue(el, value) {
42-
if (el.type === 'radio') {
42+
if (isRadio(el)) {
4343
// Set radio value from x-bind:value, if no "value" attribute exists.
4444
// If there are any initial state values, radio will have a correct
4545
// "checked" value since x-bind:value is processed before x-model.
@@ -55,7 +55,7 @@ function bindInputValue(el, value) {
5555
el.checked = checkedAttrLooseCompare(el.value, value)
5656
}
5757
}
58-
} else if (el.type === 'checkbox') {
58+
} else if (isCheckbox(el)) {
5959
// If we are explicitly binding a string to the :value, set the string,
6060
// If the value is a boolean/array/number/null/undefined, leave it alone, it will be set to "on"
6161
// automatically.
@@ -225,3 +225,11 @@ function getAttributeBinding(el, name, fallback) {
225225

226226
return attr
227227
}
228+
229+
export function isCheckbox(el) {
230+
return el.type === 'checkbox' || el.localName === 'ui-checkbox' || el.localName === 'ui-switch'
231+
}
232+
233+
export function isRadio(el) {
234+
return el.type === 'radio' || el.localName === 'ui-radio'
235+
}

0 commit comments

Comments
 (0)