@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.js CHANGED
@@ -516,6 +516,7 @@ var createGetState = (state2, helpers) => () => {
516
516
 
517
517
  // src/core/dom.ts
518
518
  var isFormFieldElement = (value) => value instanceof HTMLInputElement || value instanceof HTMLSelectElement || value instanceof HTMLTextAreaElement;
519
+ var isArraySyntax = (field) => typeof field.name === "string" && field.name.endsWith("[]");
519
520
  var resolveFormElement = (target) => {
520
521
  if (!target) return null;
521
522
  if (target instanceof HTMLFormElement) return target;
@@ -552,6 +553,30 @@ var readFieldValue = (fields) => {
552
553
  if (fields.length === 0) return void 0;
553
554
  const first = fields[0];
554
555
  if (!first) return void 0;
556
+ if (isArraySyntax(first) && fields.length > 1) {
557
+ if (first instanceof HTMLSelectElement || first instanceof HTMLTextAreaElement) {
558
+ return fields.map((f) => f.value);
559
+ }
560
+ if (first instanceof HTMLInputElement) {
561
+ if (first.type === "radio") {
562
+ const checked = fields.find(
563
+ (f) => f instanceof HTMLInputElement && f.checked
564
+ );
565
+ return checked?.value ?? "";
566
+ }
567
+ if (first.type === "checkbox") {
568
+ return fields.filter(
569
+ (f) => f instanceof HTMLInputElement && f.checked
570
+ ).map((f) => f.value);
571
+ }
572
+ if (first.type === "file") {
573
+ return fields.flatMap(
574
+ (f) => f instanceof HTMLInputElement && f.files ? Array.from(f.files) : []
575
+ );
576
+ }
577
+ }
578
+ return fields.map((f) => f.value);
579
+ }
555
580
  if (first instanceof HTMLSelectElement || first instanceof HTMLTextAreaElement) {
556
581
  return first.value;
557
582
  }
@@ -577,7 +602,10 @@ var readFieldValue = (fields) => {
577
602
  var writeFieldValue = (fields, value) => {
578
603
  const normalizedArray = Array.isArray(value) ? value.map((item) => String(item)) : null;
579
604
  const normalizedValue = value === void 0 || value === null ? "" : String(value);
580
- for (const field of fields) {
605
+ const distributeArray = fields.length > 0 && fields[0] !== void 0 && isArraySyntax(fields[0]) && normalizedArray !== null;
606
+ for (let index = 0; index < fields.length; index++) {
607
+ const field = fields[index];
608
+ if (!field) continue;
581
609
  if (field instanceof HTMLInputElement) {
582
610
  if (field.type === "checkbox") {
583
611
  field.checked = normalizedArray ? normalizedArray.includes(field.value) : normalizedValue === field.value;
@@ -591,9 +619,17 @@ var writeFieldValue = (fields, value) => {
591
619
  if (Array.isArray(value) && value.length === 0) field.value = "";
592
620
  continue;
593
621
  }
622
+ if (distributeArray && normalizedArray) {
623
+ field.value = normalizedArray[index] ?? "";
624
+ continue;
625
+ }
594
626
  field.value = normalizedValue;
595
627
  continue;
596
628
  }
629
+ if (distributeArray && normalizedArray) {
630
+ field.value = normalizedArray[index] ?? "";
631
+ continue;
632
+ }
597
633
  field.value = normalizedValue;
598
634
  }
599
635
  };