|
@@ -1,4 +1,5 @@
|
|
import { registerListener } from './on'
|
|
import { registerListener } from './on'
|
|
|
|
+import { isNumeric } from '../utils'
|
|
|
|
|
|
export function registerModelListener(component, el, modifiers, expression, extraVars) {
|
|
export function registerModelListener(component, el, modifiers, expression, extraVars) {
|
|
// If the element we are binding to is a select, a radio, or checkbox
|
|
// If the element we are binding to is a select, a radio, or checkbox
|
|
@@ -31,9 +32,10 @@ function generateModelAssignmentFunction(el, modifiers, expression) {
|
|
if (event instanceof CustomEvent && event.detail) {
|
|
if (event instanceof CustomEvent && event.detail) {
|
|
return event.detail
|
|
return event.detail
|
|
} else if (el.type === 'checkbox') {
|
|
} else if (el.type === 'checkbox') {
|
|
- // If the data we are binding to is an array, toggle it's value inside the array.
|
|
|
|
|
|
+ // If the data we are binding to is an array, toggle its value inside the array.
|
|
if (Array.isArray(currentValue)) {
|
|
if (Array.isArray(currentValue)) {
|
|
- return event.target.checked ? currentValue.concat([event.target.value]) : currentValue.filter(i => i !== event.target.value)
|
|
|
|
|
|
+ const newValue = modifiers.includes('number') ? safeParseNumber(event.target.value) : event.target.value
|
|
|
|
+ return event.target.checked ? currentValue.concat([newValue]) : currentValue.filter(i => i !== newValue)
|
|
} else {
|
|
} else {
|
|
return event.target.checked
|
|
return event.target.checked
|
|
}
|
|
}
|
|
@@ -41,18 +43,21 @@ function generateModelAssignmentFunction(el, modifiers, expression) {
|
|
return modifiers.includes('number')
|
|
return modifiers.includes('number')
|
|
? Array.from(event.target.selectedOptions).map(option => {
|
|
? Array.from(event.target.selectedOptions).map(option => {
|
|
const rawValue = option.value || option.text
|
|
const rawValue = option.value || option.text
|
|
- const number = rawValue ? parseFloat(rawValue) : null
|
|
|
|
- return isNaN(number) ? rawValue : number
|
|
|
|
|
|
+ return safeParseNumber(rawValue)
|
|
})
|
|
})
|
|
: Array.from(event.target.selectedOptions).map(option => {
|
|
: Array.from(event.target.selectedOptions).map(option => {
|
|
return option.value || option.text
|
|
return option.value || option.text
|
|
})
|
|
})
|
|
} else {
|
|
} else {
|
|
const rawValue = event.target.value
|
|
const rawValue = event.target.value
|
|
- const number = rawValue ? parseFloat(rawValue) : null
|
|
|
|
return modifiers.includes('number')
|
|
return modifiers.includes('number')
|
|
- ? (isNaN(number) ? rawValue : number)
|
|
|
|
|
|
+ ? safeParseNumber(rawValue)
|
|
: (modifiers.includes('trim') ? rawValue.trim() : rawValue)
|
|
: (modifiers.includes('trim') ? rawValue.trim() : rawValue)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
+
|
|
|
|
+function safeParseNumber(rawValue) {
|
|
|
|
+ const number = rawValue ? parseFloat(rawValue) : null
|
|
|
|
+ return isNumeric(number) ? number : rawValue
|
|
|
|
+}
|