@samline/forms 2.3.3 → 2.3.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.cjs CHANGED
@@ -557,6 +557,7 @@ var createGetState = (state2, helpers) => () => {
557
557
 
558
558
  // src/core/dom.ts
559
559
  var isFormFieldElement = (value) => value instanceof HTMLInputElement || value instanceof HTMLSelectElement || value instanceof HTMLTextAreaElement;
560
+ var isArraySyntax = (field) => typeof field.name === "string" && field.name.endsWith("[]");
560
561
  var resolveFormElement = (target) => {
561
562
  if (!target) return null;
562
563
  if (target instanceof HTMLFormElement) return target;
@@ -593,6 +594,30 @@ var readFieldValue = (fields) => {
593
594
  if (fields.length === 0) return void 0;
594
595
  const first = fields[0];
595
596
  if (!first) return void 0;
597
+ if (isArraySyntax(first) && fields.length > 1) {
598
+ if (first instanceof HTMLSelectElement || first instanceof HTMLTextAreaElement) {
599
+ return fields.map((f) => f.value);
600
+ }
601
+ if (first instanceof HTMLInputElement) {
602
+ if (first.type === "radio") {
603
+ const checked = fields.find(
604
+ (f) => f instanceof HTMLInputElement && f.checked
605
+ );
606
+ return checked?.value ?? "";
607
+ }
608
+ if (first.type === "checkbox") {
609
+ return fields.filter(
610
+ (f) => f instanceof HTMLInputElement && f.checked
611
+ ).map((f) => f.value);
612
+ }
613
+ if (first.type === "file") {
614
+ return fields.flatMap(
615
+ (f) => f instanceof HTMLInputElement && f.files ? Array.from(f.files) : []
616
+ );
617
+ }
618
+ }
619
+ return fields.map((f) => f.value);
620
+ }
596
621
  if (first instanceof HTMLSelectElement || first instanceof HTMLTextAreaElement) {
597
622
  return first.value;
598
623
  }
@@ -618,7 +643,10 @@ var readFieldValue = (fields) => {
618
643
  var writeFieldValue = (fields, value) => {
619
644
  const normalizedArray = Array.isArray(value) ? value.map((item) => String(item)) : null;
620
645
  const normalizedValue = value === void 0 || value === null ? "" : String(value);
621
- for (const field of fields) {
646
+ const distributeArray = fields.length > 0 && fields[0] !== void 0 && isArraySyntax(fields[0]) && normalizedArray !== null;
647
+ for (let index = 0; index < fields.length; index++) {
648
+ const field = fields[index];
649
+ if (!field) continue;
622
650
  if (field instanceof HTMLInputElement) {
623
651
  if (field.type === "checkbox") {
624
652
  field.checked = normalizedArray ? normalizedArray.includes(field.value) : normalizedValue === field.value;
@@ -632,9 +660,17 @@ var writeFieldValue = (fields, value) => {
632
660
  if (Array.isArray(value) && value.length === 0) field.value = "";
633
661
  continue;
634
662
  }
663
+ if (distributeArray && normalizedArray) {
664
+ field.value = normalizedArray[index] ?? "";
665
+ continue;
666
+ }
635
667
  field.value = normalizedValue;
636
668
  continue;
637
669
  }
670
+ if (distributeArray && normalizedArray) {
671
+ field.value = normalizedArray[index] ?? "";
672
+ continue;
673
+ }
638
674
  field.value = normalizedValue;
639
675
  }
640
676
  };