@felte/common
Advanced tools
Comparing version 1.0.0 to 1.0.1
@@ -1,2 +0,2 @@ | ||
import { isFormControl, isFieldSetElement, isInputElement } from './typeGuards.js'; | ||
import { isFormControl, isFieldSetElement, isInputElement, isSelectElement } from './typeGuards.js'; | ||
import { _get } from './get.js'; | ||
@@ -103,2 +103,16 @@ import { _set } from './set.js'; | ||
} | ||
else if (isSelectElement(el)) { | ||
const multiple = el.multiple; | ||
if (!multiple) { | ||
defaultData = _set(defaultData, elName, el.value); | ||
} | ||
else { | ||
const selectedOptions = Array.from(el.options) | ||
.filter((opt) => opt.selected) | ||
.map((opt) => opt.value); | ||
defaultData = _set(defaultData, elName, selectedOptions); | ||
} | ||
defaultTouched = _set(defaultTouched, elName, false); | ||
continue; | ||
} | ||
const inputValue = getInputTextOrNumber(el); | ||
@@ -111,2 +125,3 @@ defaultData = _set(defaultData, elName, inputValue); | ||
function setControlValue(el, value) { | ||
var _a; | ||
if (!isFormControl(el)) | ||
@@ -147,2 +162,28 @@ return; | ||
} | ||
else if (isSelectElement(el)) { | ||
const multiple = el.multiple; | ||
if (!multiple) { | ||
el.value = String(fieldValue !== null && fieldValue !== void 0 ? fieldValue : ''); | ||
for (const option of el.options) { | ||
if (option.value === fieldValue) { | ||
option.selected = true; | ||
} | ||
else { | ||
option.selected = false; | ||
} | ||
} | ||
} | ||
else if (Array.isArray(fieldValue)) { | ||
el.value = String((_a = fieldValue[0]) !== null && _a !== void 0 ? _a : ''); | ||
for (const option of el.options) { | ||
if (fieldValue.includes(option.value)) { | ||
option.selected = true; | ||
} | ||
else { | ||
option.selected = false; | ||
} | ||
} | ||
} | ||
return; | ||
} | ||
el.value = String(fieldValue !== null && fieldValue !== void 0 ? fieldValue : ''); | ||
@@ -149,0 +190,0 @@ } |
{ | ||
"name": "@felte/common", | ||
"version": "1.0.0", | ||
"version": "1.0.1", | ||
"description": "Common utilities for Felte packages", | ||
@@ -5,0 +5,0 @@ "author": "Pablo Berganza <pablo@berganza.dev>", |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
203377
2101